分享
 
 
 

java sorting

王朝java/jsp·作者佚名  2006-03-30
窄屏简体版  字體: |||超大  

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.util.Arrays;

import java.util.Random;

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 = false;

/**

* 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] = old;

}

/**

* Fills an array with random values between 0 and MAX_VALUE.

*/

private void reload(int[] a) {

for (int i = 0; i < a.length; 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 = i;

for (int j = i + 1; j < a.length; j++) {

if (a[j] < a[small]) small = j;

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.length; i++) {

int current = a[i];

int j = i;

for (; j > 0 && current < a[j-1]; j--) {

a[j] = a[j-1];

PAUSE();

}

a[j] = current;

}

}

/**

* 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 < i; 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.length;) {

PAUSE();

if (i == 0 || a[i-1] <= a[i]) {

i++;

} else {

swap(a, i, i - 1);

i--;

}

}

}

/**

* 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 = false;

for (int i = 0; i < a.length - distance; i++) {

if (a[i] > a[i + distance]) {

swap(a, i, i + distance);

changed = true;

}

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.length; 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 = left;

int j = 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 = i; j * 2 + 1 < a.length;) {

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); else break;

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 < i;) {

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); else break;

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) return;

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 = lo; k <= hi; 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 = lo; k <= hi; 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.length; i++) {

max = Math.max(max, a[i]);

PAUSE();

}

System.out.println(max);

int[] counts = new int[max + 1];

Arrays.fill(counts, 0);

for (int i = 0; i < a.length; i++) {

counts[a[i]]++;

PAUSE();

}

for (int i = 0, j = 0; j < counts.length; j++) {

for (int k = 0; k < counts[j]; k++) {

a[i++] = j;

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);

try {

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 = true;

}

};

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.length; i < n; 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() {

try {

Thread.sleep(1);

if (shouldStop) {

shouldStop = false;

// Can't think of a better way to stop than this

throw new RuntimeException();

}

} catch (InterruptedException e) {

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有