java sorting - 王朝网络 -
分享&&&&&当前位置: &&&&&&&&java sortingjava sorting上一篇下一篇字体: ||
| 來源: 互联网&& import java.awt.D import java.awt.G import java.awt.event.ActionE import java.util.A import java.util.R import javax.swing.*; /** * A class containing a number of classic sorting algorithms, * implemented from scratch.
The algorithms each operate on arrays * of ints only, and are animated. */ public class Sorter extends JPanel {
private static Random randomizer = new Random();
private static final int ARRAY_LENGTH = 600;
private static final int MAX_VALUE = 400;
private final int[] array = new int[ARRAY_LENGTH];
private transient boolean shouldStop =
* Swaps a[i] and a[j].
private void swap(int[] a, int i, int j) {
int old = a[i];
a[i] = a[j];
a[j] =
* Fills an array with random values between 0 and MAX_VALUE.
private void reload(int[] a) {
for (int i = 0; i & a. i++) {
a[i] = randomizer.nextInt(MAX_VALUE);
* Sorts an array using Selection Sort.
The algorithm is to first
* put the smallest item in the first position, then the next
* smallest in the second position, and so on.
public void selectionSort(int[] a) {
for (int i = 0; i & a.length - 1; i++) {
int small =
for (int j = i + 1; j & a. j++) {
if (a[j] & a[small]) small =
PAUSE();
swap(a, i, small);
* Sorts an array using Insertion Sort.
The algorithm is to first
* slide the second element back as far as it should go, then slide
* the third back, and so on.
public void insertionSort(int[] a) {
for (int i = 1; i & a. i++) {
int current = a[i];
int j =
for (; j & 0 && current & a[j-1]; j--) {
a[j] = a[j-1];
PAUSE();
a[j] =
* Sorts an array using Bubble Sort.
public void bubbleSort(int[] a) {
for (int i = a.length - 1; i & 0; i--) {
for (int j = 0; j & j++) {
if (a[j] & a[j + 1]) swap(a, j, j + 1);
PAUSE();
* Sorts an array using Gnome Sort.
public void gnomeSort(int[] a) {
for (int i = 0; i & a.) {
PAUSE();
if (i == 0 || a[i-1] &= a[i]) {
} else {
swap(a, i, i - 1);
* Sorts an array using Shell Sort.
This is a lousy Shell Sort.
* I need to make a new one.
public void shellSort(int[] a) {
int distance = a.length / 2;
while (distance & 0) {
boolean changed =
for (int i = 0; i & a.length - i++) {
if (a[i] & a[i + distance]) {
swap(a, i, i + distance);
changed =
PAUSE();
if (!changed) distance /= 2;
* Sorts an array using Quick Sort.
This version of quicksort uses
* the leftmost item as the pivot, but since this gives disastrous
* performance on sorted and nearly sorted arrays, we scramble the
* array first.
public void quickSort(int[] a) {
// First shuffle (permute) the array
for (int i = 0; i & a. i++) {
swap(a, i, randomizer.nextInt(ARRAY_LENGTH));
// Call the recursive helper
quickSort(a, 0, a.length - 1);
private void quickSort(int[] a, int left, int right) {
if (left & right) {
int i =
int j =
while (i & j) {
while (a[j] & a[left]) {j--; PAUSE();}
while (i & j && a[i] &= a[left]) {i++; PAUSE();}
if (i & j) swap(a, i, j);
swap(a, left, j);
quickSort(a, left, j-1);
quickSort(a, j+1, right);
* Sorts an array using Heap Sort.
public void heapSort(int[] a) {
// Phase 1: make a heap by sifting down all non-leaf
// elements, one after another, starting with the last
// non-leaf element and going backwards.
for (int i = a.length / 2 - 1; i &= 0; i--) {
for (int j = j * 2 + 1 & a.) {
PAUSE();
int k = j * 2 + 1;
if (k + 1 & a.length && a[k] & a[k + 1]) k++;
if (a[j] & a[k]) swap(a, j, k);
// Phase 2: Successively place the biggest, then next biggest
// items at the end of the array. each time reconstructing the
// heap in the slots of the array not yet sorted.
for (int i = a.length - 1; i & 0; i--) {
swap(a, 0, i);
for (int j = 0; j * 2 + 1 &) {
PAUSE();
int k = j * 2 + 1;
if (k + 1 & i && a[k] & a[k + 1]) k++;
if (a[j] & a[k]) swap(a, j, k);
* Sorts an array using merge sort, the classic version with
* the extra storage.
public void mergeSort(int[] a) {
int[] scratch = new int[a.length];
mergeSort(a, 0, a.length - 1, scratch);
private void mergeSort(int[] a, int lo, int hi, int[] scratch) {
if (lo &= hi)
int mid = (lo + hi) / 2;
mergeSort(a, lo, mid, scratch);
mergeSort(a, mid + 1, hi, scratch);
// Merge sorted sublists into temporary storage
for (int i = lo, j = mid + 1, k = k &= k++) {
if ((i &= mid) && ((j & hi) || (a[i] & a[j]))) {
scratch[k] = a[i++]; PAUSE();
} else {
scratch[k] = a[j++]; PAUSE();
// Copy back from temporary storage
for (int k = k &= k++) {
a[k] = scratch[k]; PAUSE();
* Sorts an array using counting sort, provided all values in the
* array are non-negative.
If there are negative values in the array,
* the method will not sort but rather leave the array undefined.
* Furthermore, the method will likely throw an OutOfMemoryError
* if there are large integers in the array.
public void countingSort(int[] a) {
int max = 0;
for (int i = 0; i & a. i++) {
max = Math.max(max, a[i]);
System.out.println(max);
int[] counts = new int[max + 1];
Arrays.fill(counts, 0);
for (int i = 0; i & a. i++) {
counts[a[i]]++;
PAUSE();
for (int i = 0, j = 0; j & counts. j++) {
for (int k = 0; k & counts[j]; k++) {
a[i++] =
PAUSE();
public static void main(String[] args) {
Sorter sorter = new Sorter();
JFrame frame = new JFrame(&Sorting&);
frame.getContentPane().add(sorter.toolbar, &North&);
frame.getContentPane().add(sorter, &Center&);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
sorter.runAnimation();
Action startAction = new AbstractAction(&Start&) {
public void actionPerformed(ActionEvent e) {
final String methodName = (String)comboBox.getSelectedItem();
new Thread(new Runnable() {
public void run() {
startButton.setEnabled(false);
stopButton.setEnabled(true);
reload(array);
Sorter.class.getMethod(methodName,
new Class[]{array.getClass()})
.invoke(Sorter.this, new Object[]{array});
} catch (Exception e) {
stopButton.setEnabled(false);
startButton.setEnabled(true);
).start();
Action stopAction = new AbstractAction(&Stop&) {
public void actionPerformed(ActionEvent e) {
shouldStop =
private JToolBar toolbar = new JToolBar();
private JButton startButton = new JButton(startAction);
private JButton stopButton = new JButton(stopAction);
JComboBox comboBox = new JComboBox(new String[]{
&selectionSort&,
&insertionSort&,
&bubbleSort&,
&gnomeSort&,
&shellSort&,
&quickSort&,
&mergeSort&,
&heapSort&,
&countingSort&
public Sorter() {
setPreferredSize(new Dimension(ARRAY_LENGTH, MAX_VALUE));
setBorder(BorderFactory.createEtchedBorder());
toolbar.add(comboBox);
toolbar.add(startButton);
toolbar.add(stopButton);
comboBox.setMaximumRowCount(12);
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0, n = array. i & i++) {
g.drawLine(i, MAX_VALUE, i, MAX_VALUE - array[i]);
* Causes the screen to be repainted every 30 ms or so.
private void runAnimation() {
while (true) {
repaint();
try {Thread.sleep(30);} catch (InterruptedException e) {}
* Something to call periodically during sorting.
private void PAUSE() {
Thread.sleep(1);
if (shouldStop) {
shouldStop =
// Can't think of a better way to stop than this
throw new RuntimeException();
} catch (InterruptedException e) {
} }相似文章&今日推荐&&&幽默笑话百态军事探索娱乐女性健康旅游互联网··············&import java.awt.D
import java.awt.G
import java.awt.event.ActionE
import java.util.A
import java.util.R
import javax.swing.*;
* A class containing a number of classic sorting algorithms,
* implemented from scratch.
The algorithms each operate on arrays
* of ints only, and are animated.
public class Sorter extends JPanel {
private static Random randomizer = new Random();
private static final int ARRAY_LENGTH = 600;
private static final int MAX_VALUE = 400;
private final int[] array = new int[ARRAY_LENGTH];
private transient boolean shouldStop =
* Swaps a[i] and a[j].
private void swap(int[] a, int i, int j) {
int old = a[i];
a[i] = a[j];
* Fills an array with random values between 0 and MAX_VALUE.
private void reload(int[] a) {
for (int i = 0; i & a. i++) {
a[i] = randomizer.nextInt(MAX_VALUE);
* Sorts an array using Selection Sort.
The algorithm is to first
* put the smallest item in the first position, then the next
* smallest in the second position, and so on.
public void selectionSort(int[] a) {
for (int i = 0; i & a.length - 1; i++) {
int small =
for (int j = i + 1; j & a. j++) {
if (a[j] & a[small]) small =
swap(a, i, small);
* Sorts an array using Insertion Sort.
The algorithm is to first
* slide the second element back as far as it should go, then slide
* the third back, and so on.
public void insertionSort(int[] a) {
for (int i = 1; i & a. i++) {
int current = a[i];
for (; j & 0 && current & a[j-1]; j--) {
a[j] = a[j-1];
* Sorts an array using Bubble Sort.
public void bubbleSort(int[] a) {
for (int i = a.length - 1; i & 0; i--) {
for (int j = 0; j & j++) {
if (a[j] & a[j + 1]) swap(a, j, j + 1);
* Sorts an array using Gnome Sort.
public void gnomeSort(int[] a) {
for (int i = 0; i & a.) {
if (i == 0 || a[i-1] &= a[i]) {
swap(a, i, i - 1);
* Sorts an array using Shell Sort.
This is a lousy Shell Sort.
* I need to make a new one.
public void shellSort(int[] a) {
int distance = a.length / 2;
while (distance & 0) {
boolean changed =
for (int i = 0; i & a.length - i++) {
if (a[i] & a[i + distance]) {
swap(a, i, i + distance);
if (!changed) distance /= 2;
* Sorts an array using Quick Sort.
This version of quicksort uses
* the leftmost item as the pivot, but since this gives disastrous
* performance on sorted and nearly sorted arrays, we scramble the
* array first.
public void quickSort(int[] a) {
// First shuffle (permute) the array
for (int i = 0; i & a. i++) {
swap(a, i, randomizer.nextInt(ARRAY_LENGTH));
// Call the recursive helper
quickSort(a, 0, a.length - 1);
private void quickSort(int[] a, int left, int right) {
if (left & right) {
while (i & j) {
while (a[j] & a[left]) {j--; PAUSE();}
while (i & j && a[i] &= a[left]) {i++; PAUSE();}
if (i & j) swap(a, i, j);
swap(a, left, j);
quickSort(a, left, j-1);
quickSort(a, j+1, right);
* Sorts an array using Heap Sort.
public void heapSort(int[] a) {
// Phase 1: make a heap by sifting down all non-leaf
// elements, one after another, starting with the last
// non-leaf element and going backwards.
for (int i = a.length / 2 - 1; i &= 0; i--) {
for (int j = j * 2 + 1 & a.) {
int k = j * 2 + 1;
if (k + 1 & a.length && a[k] & a[k + 1]) k++;
if (a[j] & a[k]) swap(a, j, k);
// Phase 2: Successively place the biggest, then next biggest
// items at the end of the array. each time reconstructing the
// heap in the slots of the array not yet sorted.
for (int i = a.length - 1; i & 0; i--) {
swap(a, 0, i);
for (int j = 0; j * 2 + 1 &) {
int k = j * 2 + 1;
if (k + 1 & i && a[k] & a[k + 1]) k++;
if (a[j] & a[k]) swap(a, j, k);
* Sorts an array using merge sort, the classic version with
* the extra storage.
public void mergeSort(int[] a) {
int[] scratch = new int[a.length];
mergeSort(a, 0, a.length - 1, scratch);
private void mergeSort(int[] a, int lo, int hi, int[] scratch) {
if (lo &= hi)
int mid = (lo + hi) / 2;
mergeSort(a, lo, mid, scratch);
mergeSort(a, mid + 1, hi, scratch);
// Merge sorted sublists into temporary storage
for (int i = lo, j = mid + 1, k = k &= k++) {
if ((i &= mid) && ((j & hi) || (a[i] & a[j]))) {
scratch[k] = a[i++]; PAUSE();
scratch[k] = a[j++]; PAUSE();
// Copy back from temporary storage
for (int k = k &= k++) {
a[k] = scratch[k]; PAUSE();
* Sorts an array using counting sort, provided all values in the
* array are non-negative.
If there are negative values in the array,
* the method will not sort but rather leave the array undefined.
* Furthermore, the method will likely throw an OutOfMemoryError
* if there are large integers in the array.
public void countingSort(int[] a) {
int max = 0;
for (int i = 0; i & a. i++) {
max = Math.max(max, a[i]);
System.out.println(max);
int[] counts = new int[max + 1];
Arrays.fill(counts, 0);
for (int i = 0; i & a. i++) {
counts[a[i]]++;
for (int i = 0, j = 0; j & counts. j++) {
for (int k = 0; k & counts[j]; k++) {
public static void main(String[] args) {
Sorter sorter = new Sorter();
JFrame frame = new JFrame(&Sorting&);
frame.getContentPane().add(sorter.toolbar, &North&);
frame.getContentPane().add(sorter, &Center&);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
sorter.runAnimation();
Action startAction = new AbstractAction(&Start&) {
public void actionPerformed(ActionEvent e) {
final String methodName = (String)comboBox.getSelectedItem();
new Thread(new Runnable() {
public void run() {
startButton.setEnabled(false);
stopButton.setEnabled(true);
reload(array);
Sorter.class.getMethod(methodName,
new Class[]{array.getClass()})
.invoke(Sorter.this, new Object[]{array});
} catch (Exception e) {
stopButton.setEnabled(false);
startButton.setEnabled(true);
).start();
Action stopAction = new AbstractAction(&Stop&) {
public void actionPerformed(ActionEvent e) {
shouldStop =
private JToolBar toolbar = new JToolBar();
private JButton startButton = new JButton(startAction);
private JButton stopButton = new JButton(stopAction);
JComboBox comboBox = new JComboBox(new String[]{
&selectionSort&,
&insertionSort&,
&bubbleSort&,
&gnomeSort&,
&shellSort&,
&quickSort&,
&mergeSort&,
&heapSort&,
&countingSort&
public Sorter() {
setPreferredSize(new Dimension(ARRAY_LENGTH, MAX_VALUE));
setBorder(BorderFactory.createEtchedBorder());
toolbar.add(comboBox);
toolbar.add(startButton);
toolbar.add(stopButton);
comboBox.setMaximumRowCount(12);
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0, n = array. i & i++) {
g.drawLine(i, MAX_VALUE, i, MAX_VALUE - array[i]);
* Causes the screen to be repainted every 30 ms or so.
private void runAnimation() {
while (true) {
repaint();
try {Thread.sleep(30);} catch (InterruptedException e) {}
* Something to call periodically during sorting.
private void PAUSE() {
Thread.sleep(1);
if (shouldStop) {
shouldStop =
// Can't think of a better way to stop than this
throw new RuntimeException();
} catch (InterruptedException e) {
}& 免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。&&&&&&&王朝美图& 06:15:34&&&&&&&转载本文&UBB代码&HTML代码复制到剪贴板...&更多内容··········&&&&频道精选&最新评论&&网友关注··········&&热点推荐&01&&02&&03&&04&&05&&06&&07&&08&&09&&10&&&&王朝女性&&|&&|&&|&&|&&|&&|&&|&&|&&|&&|&&|&&|&王朝分栏&&|&&|&&|&&|&&|&&|&&|&&|&&|&&|&王朝编程&&|&&|&&|&&|&&|&&|&&|&&|&&|&&|&王朝导购&&|&&|&&|&&|&&|&&|&&|&&|&&|&&|&王朝其他&&|&&|&&|&&|&&|&&|&&&&2005-&&版权所有& 上传我的文档
 下载
 收藏
该文档贡献者很忙,什么也没留下。
 下载此文档
正在努力加载中...
植物进化树
下载积分:840
内容提示:精心收集的各类精品文档,值得下载: 植物进化树
文档格式:PDF|
浏览次数:38|
上传日期: 00:43:27|
文档星级:
该用户还上传了这些文档
植物进化树
官方公共微信