Java学习-Day23

冒泡排序

一、描述

冒泡排序(Bubble Sort)也是一种简单直观的排序算法.

它重复地走访过要排序的数列, 一次比较两个元素, 如果他们的顺序错误就把他们交换过来.

走访数列的工作是重复地进行直到没有再需要交换, 也就是说该数列已经排序完成.

这个算法的名字由来是因为越小的元素会经由交换慢慢 "浮" 到数列的顶端.

冒泡排序还有一种优化算法, 就是立一个 flag , 当在一趟序列遍历中元素没有发生交换, 则证明该序列已经有序. 但这种改进对于提升性能来说并没有什么太大作用.

二、具体代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
*********************
* Bubble sort.
*********************
*/
public void bubbleSort() {
boolean tempSwapped;
DataNode tempNode;
for (int i = length - 1; i > 0; i--) {
tempSwapped = false;
for (int j = 0; j < i; j++) {
if (data[j].key > data[j + 1].key) {
// Swap.
tempNode = data[j + 1];
data[j + 1] = data[j];
data[j] = tempNode;

tempSwapped = true;
} // Of if
} // Of for j

// No swap in this round. The data are already sorted.
if (!tempSwapped) {
System.out.println("Premature");
break;
} // Of if

System.out.println("Round " + (length - i));
System.out.println(this);
} // Of for i
}// Of bubbleSort

/**
*********************
* Test the method.
*********************
*/
public static void bubbleSortTest() {
int[] tempUnsortedKeys = { 1, 3, 6, 10, 7, 5, 9 };
String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };
DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents);

System.out.println(tempDataArray);

tempDataArray.bubbleSort();
System.out.println("Result\r\n" + tempDataArray);
}// Of bubbleSortTest

三、运行截图

快速排序

一、描述

从数列中挑出一个元素,称为 "基准" (pivot)

重新排序数列, 所有元素比基准值小的摆放在基准前面, 所有元素比基准值大的摆在基准的后面 (相同的数可以到任一边). 在这个分区退出之后, 该基准就处于数列的中间位置.这个称为分区 (partition) 操作

递归地 (recursive) 把小于基准值元素的子数列和大于基准值元素的子数列排序

平均时间复杂度为 \(O(n\log{n})\), 但最坏情况还是 \(O(n^2)\)

二、具体代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
*********************
* Quick sort recursively.
*
* @param paraStart The start index.
* @param paraEnd The end index.
*********************
*/
public void quickSortRecursive(int paraStart, int paraEnd) {
// Nothing to sort.
if (paraStart >= paraEnd) {
return;
} // Of if

int tempPivot = data[paraEnd].key;
DataNode tempNodeForSwap;

int tempLeft = paraStart;
int tempRight = paraEnd - 1;

// Find the position for the pivot.
// At the same time move smaller elements to the left and bigger one to the
// right.
while (true) {
while ((data[tempLeft].key < tempPivot) && (tempLeft < tempRight)) {
tempLeft++;
} // Of while

while ((data[tempRight].key >= tempPivot) && (tempLeft < tempRight)) {
tempRight--;
} // Of while

if (tempLeft < tempRight) {
// Swap.
System.out.println("Swapping " + tempLeft + " and " + tempRight);
tempNodeForSwap = data[tempLeft];
data[tempLeft] = data[tempRight];
data[tempRight] = tempNodeForSwap;
} else {
break;
} // Of if
} // Of while

// Swap
if (data[tempLeft].key > tempPivot) {
tempNodeForSwap = data[paraEnd];
data[paraEnd] = data[tempLeft];
data[tempLeft] = tempNodeForSwap;
} else {
tempLeft++;
} // Of if

System.out.print("From " + paraStart + " to " + paraEnd + ": ");
System.out.println(this);

quickSortRecursive(paraStart, tempLeft - 1);
quickSortRecursive(tempLeft + 1, paraEnd);
}// Of quickSortRecursive

/**
*********************
* Quick sort.
*********************
*/
public void quickSort() {
quickSortRecursive(0, length - 1);
}// Of quickSort

/**
*********************
* Test the method.
*********************
*/
public static void quickSortTest() {
int[] tempUnsortedKeys = { 1, 3, 12, 10, 5, 7, 9 };
String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };
DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents);

System.out.println(tempDataArray);

tempDataArray.quickSort();
System.out.println("Result\r\n" + tempDataArray);
}// Of quickSortTest

三、运行截图

总结

作为最简单的排序算法之一, 冒泡排序给我的感觉就像 Abandon 在单词书里出现的感觉一样, 每次都在第一页第一位, 所以最熟悉.

然后就是快排, 这是许多语言内置排序的默认算法. 然后我惊奇地发现 Java 的默认排序算法竟然也是快排序.