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
84
85
86
87
88 |
public class ExchangeSortUtils { // 冒泡 public
static void bubbleSort( int [] array) { int
length = array.length; int
temp; boolean
isSort; for
( int i = 1 ; i < length; i++) { isSort = false ; for
( int j = 0 ; j < length - i; j++) { if
(array[j] > array[j + 1 ]) { // 交换 temp = array[j]; array[j] = array[j + 1 ]; array[j + 1 ] = temp; isSort = true ; } } if
(!isSort) break ; // 如果没有发生交换,则退出循环 } } public
static int getMiddle( int [] arr, int
low, int
high) { if
(low < high) { int
tmp = arr[low]; // 数组的第一个作为中轴 while
(low < high && arr[high] > tmp) { high--; } arr[low] = arr[high]; // 比中轴小的记录移到低端 while
(low < high && arr[low] < tmp) { low++; } arr[high] = arr[low]; // 比中轴大的记录移到高端 arr[low] = tmp; // 中轴记录到尾 } return
low; // 返回中轴的位置 } public
static int getMiddle2( int [] arr, int
low, int
high) { int
tmp = arr[low]; // 数组的第一个作为中轴 while
(low < high) { while
(low < high && arr[high] > tmp) { high--; } arr[low] = arr[high]; // 比中轴小的记录移到低端 while
(low < high && arr[low] < tmp) { low++; } arr[high] = arr[low]; // 比中轴大的记录移到高端 } arr[low] = tmp; // 中轴记录到尾 return
low; // 返回中轴的位置 } //快速排序 public
static void quickSort( int [] arr, int
low, int
high) { if
(low < high) { int
middle = getMiddle(arr, low, high); // 将arr数组进行一分为二 quickSort(arr, low, middle - 1 ); // 对低字表进行递归排序 quickSort(arr, middle + 1 , high); // 对高字表进行递归排序 } } public
static void main(String[] args) { quickSortTest(); bubbleSortTest(); } private
static void quickSortTest() { int [] sortArray = { 5 , 2 , 4 , 1 , 3
}; System.out.print( "快速排序前: " ); Utils.printArray(sortArray); quickSort(sortArray, 0 , sortArray.length - 1 ); System.out.print( "快速排序后: " ); Utils.printArray(sortArray); } private
static void bubbleSortTest() { int [] sortArray = { 5 , 2 , 4 , 1 , 3
}; System.out.print( "冒泡排序前: " ); Utils.printArray(sortArray); bubbleSort(sortArray); System.out.print( "冒泡排序后: " ); Utils.printArray(sortArray); } } |
原文:http://www.cnblogs.com/zhaofeng555/p/3594871.html