|
2 | 2 |
|
3 | 3 | public class Sort { |
4 | 4 |
|
5 | | - public static void main(String[] args) { |
6 | | - int[] testArr = {12,27,19,39,16,49,61,40,20,94,63,13,27,31,38,43,11,44,81,29,33,88,60,80,51,4,0,1,22,9,8}; |
7 | | - Sort.insertSort(testArr); |
| 5 | + public static void main(String[] args) { |
| 6 | + int[] testArr = {12, 27, 19, 39, 16, 49, 61, 40, 20, 94, 63, 13, 27, 31, 38, 43, 11, 44, 81, 29, 33, 88, 60, 80, 51, 4, 0, 1, 22, 9, 8}; |
| 7 | + Sort.insertSort(testArr); |
8 | 8 | // Sort.bubbleSort(testArr); |
9 | 9 | // Sort.quickSort(testArr, 0, testArr.length-1); |
10 | 10 | // Sort.selectSort(testArr); |
11 | | - Sort.display(testArr); |
12 | | - } |
13 | | - |
14 | | - public static void display(int[] arr){ |
15 | | - for (int i = 0; i < arr.length; i++) { |
16 | | - System.out.print(arr[i]+","); |
17 | | - } |
18 | | - } |
19 | | - |
20 | | - /** |
21 | | - * 插入排序 |
22 | | - * 关键:1、临时变量保存待排序的数字 |
23 | | - * 2、边比较边后移 |
24 | | - * 适用情况:序列基本有序的时候 |
25 | | - */ |
26 | | - public static void insertSort(int[] arr){ |
27 | | - for (int i = 1; i < arr.length; i++) { |
28 | | - int temp = arr[i];//临时保存待插入序列 |
29 | | - int j;//下面先保证J大于0再去访问arr[j]!!!! |
30 | | - for (j = i-1;j >= 0 && temp > arr[j]; j--) {//带插入序列大于当前序列就前移--》从大到小排列 |
31 | | - arr[j+1] = arr[j]; |
32 | | - } |
33 | | - arr[j+1]=temp; |
34 | | - } |
35 | | - } |
36 | | - /** |
37 | | - * 希尔排序 |
38 | | - */ |
39 | | - public static void shellSort(int[] arr){ |
40 | | - |
41 | | - for (int i = arr.length/2; i >= 1; i=i/2) {//子序列的划分 |
42 | | - |
43 | | - } |
44 | | - |
45 | | - } |
46 | | - /** |
47 | | - * 冒泡排序 |
48 | | - * 关键:1、外循环标识无序区和有序区 |
49 | | - * 2、内循环每次将一个无序区的(最大)数字排到最前面 |
50 | | - */ |
51 | | - public static void bubbleSort(int[] arr){ |
52 | | - for (int i = 0; i <arr.length-1; i++) {//从后往前推小的 |
53 | | - for (int j = arr.length-1; j>i; j--) {//从最后一个开始遍历 |
54 | | - if (arr[j]>arr[j-1]) { |
55 | | - int temp = arr[j];//swap |
56 | | - arr[j] = arr[j-1]; |
57 | | - arr[j-1] = temp; |
58 | | - } |
59 | | - } |
60 | | - } |
61 | | - } |
62 | | - |
63 | | - /** |
64 | | - * 快速排序 |
65 | | - * 关键: 1、选轴值 |
66 | | - * 2、一次划分 |
67 | | - * 3、递归快排 |
68 | | - */ |
69 | | - public static void quickSort(int[] arr,int start,int end){ |
70 | | - if(start<end){ |
71 | | - int middle = partition(arr, start, end); |
72 | | - quickSort(arr, start, middle-1); |
73 | | - quickSort(arr, middle+1, end); |
74 | | - } |
75 | | - } |
76 | | - /** |
77 | | - * 以arr[0]为基准进行一次划分算法,左边都小于arr[0],右边都大于arr[0] |
78 | | - */ |
79 | | - private static int partition(int[] arr,int first,int end){ |
80 | | - while(first < end){ |
81 | | - //先扫描右端 |
82 | | - while(first < end && arr[first]<arr[end]){ |
83 | | - end--; |
84 | | - } |
85 | | - if(first < end){ |
86 | | - int temp = arr[end]; |
87 | | - arr[end] = arr[first]; |
88 | | - arr[first] = temp; |
89 | | - first++; |
90 | | - } |
91 | | - //扫描左边 |
92 | | - while(first < end && arr[first]<arr[end]){ |
93 | | - first++; |
94 | | - } |
95 | | - if(first<end){ |
96 | | - int temp = arr[end]; |
97 | | - arr[end] = arr[first]; |
98 | | - arr[first] = temp; |
99 | | - end--; |
100 | | - } |
101 | | - } |
102 | | - return first; |
103 | | - } |
104 | | - |
105 | | - private static int partition2(int[] arr,int left,int right){ |
106 | | - int pivotKey = arr[left]; |
107 | | - int pivotePointer = left; |
108 | | - |
109 | | - while(left < right){ |
110 | | - while(left<right&&arr[right]>=pivotKey){ |
111 | | - right--; |
112 | | - } |
113 | | - while(left<right && arr[left]<=pivotKey){ |
114 | | - left++; |
115 | | - } |
| 11 | + Sort.display(testArr); |
| 12 | + } |
| 13 | + |
| 14 | + public static void display(int[] arr) { |
| 15 | + for (int i = 0; i < arr.length; i++) { |
| 16 | + System.out.print(arr[i] + ","); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * 插入排序 |
| 22 | + * 关键:1、临时变量保存待排序的数字 |
| 23 | + * 2、边比较边后移 |
| 24 | + * 适用情况:序列基本有序的时候 |
| 25 | + */ |
| 26 | + public static void insertSort(int[] arr) { |
| 27 | + for (int i = 1; i < arr.length; i++) { |
| 28 | + int temp = arr[i];//临时保存待插入序列 |
| 29 | + int j;//下面先保证J大于0再去访问arr[j]!!!! |
| 30 | + for (j = i - 1; j >= 0 && temp > arr[j]; j--) {//带插入序列大于当前序列就前移--》从大到小排列 |
| 31 | + arr[j + 1] = arr[j]; |
| 32 | + } |
| 33 | + arr[j + 1] = temp; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * 希尔排序 |
| 39 | + */ |
| 40 | + public static void shellSort(int[] arr) { |
| 41 | + |
| 42 | + for (int i = arr.length / 2; i >= 1; i = i / 2) {//子序列的划分 |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * 冒泡排序 |
| 50 | + * 关键:1、外循环标识无序区和有序区 |
| 51 | + * 2、内循环每次将一个无序区的(最大)数字排到最前面 |
| 52 | + */ |
| 53 | + public static void bubbleSort(int[] arr) { |
| 54 | + for (int i = 0; i < arr.length - 1; i++) {//从后往前推小的 |
| 55 | + for (int j = arr.length - 1; j > i; j--) {//从最后一个开始遍历 |
| 56 | + if (arr[j] > arr[j - 1]) { |
| 57 | + int temp = arr[j];//swap |
| 58 | + arr[j] = arr[j - 1]; |
| 59 | + arr[j - 1] = temp; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * 快速排序 |
| 67 | + * 关键: 1、选轴值 |
| 68 | + * 2、一次划分 |
| 69 | + * 3、递归快排 |
| 70 | + */ |
| 71 | + public static void quickSort(int[] arr, int start, int end) { |
| 72 | + if (start < end) { |
| 73 | + int middle = partition(arr, start, end); |
| 74 | + quickSort(arr, start, middle - 1); |
| 75 | + quickSort(arr, middle + 1, end); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * 以arr[0]为基准进行一次划分算法,左边都小于arr[0],右边都大于arr[0] |
| 81 | + */ |
| 82 | + private static int partition(int[] arr, int first, int end) { |
| 83 | + while (first < end) { |
| 84 | + //先扫描右端 |
| 85 | + while (first < end && arr[first] < arr[end]) { |
| 86 | + end--; |
| 87 | + } |
| 88 | + if (first < end) { |
| 89 | + int temp = arr[end];//swap |
| 90 | + arr[end] = arr[first]; |
| 91 | + arr[first] = temp; |
| 92 | + first++; |
| 93 | + } |
| 94 | + //扫描左边 |
| 95 | + while (first < end && arr[first] < arr[end]) { |
| 96 | + first++; |
| 97 | + } |
| 98 | + if (first < end) { |
| 99 | + int temp = arr[end];//swap |
| 100 | + arr[end] = arr[first]; |
| 101 | + arr[first] = temp; |
| 102 | + end--; |
| 103 | + } |
| 104 | + } |
| 105 | + return first; |
| 106 | + } |
| 107 | + |
| 108 | + private static int partition2(int[] arr, int left, int right) { |
| 109 | + int pivotKey = arr[left]; |
| 110 | + int pivotePointer = left; |
| 111 | + |
| 112 | + while (left < right) { |
| 113 | + while (left < right && arr[right] >= pivotKey) { |
| 114 | + right--; |
| 115 | + } |
| 116 | + while (left < right && arr[left] <= pivotKey) { |
| 117 | + left++; |
| 118 | + } |
116 | 119 | // swap(arr,left,right); |
117 | | - } |
| 120 | + } |
118 | 121 | // swap(arr,pivotePointer,left); |
119 | | - return left; |
120 | | - } |
121 | | - |
122 | | - |
123 | | - /** |
124 | | - * 选择排序: O(n^2) |
125 | | - * 每次遍历无序区选出最小的 |
126 | | - */ |
127 | | - public static void selectSort(int[] arr){ |
128 | | - int minIndex = 0; |
129 | | - for (int i = 0; i < arr.length-1; i++) { |
130 | | - minIndex = i;//指向最小的值的角标 |
131 | | - for (int j = i+1; j < arr.length; j++) {//遍历无序区 |
132 | | - if(arr[j] < arr[minIndex]){ |
133 | | - minIndex = j; |
134 | | - } |
135 | | - } |
136 | | - if (minIndex != i) { //无序区遍历完了,找到最小的角标了,交换 |
137 | | - int temp = arr[minIndex]; |
138 | | - arr[minIndex] = arr[i]; |
139 | | - arr[i] = temp; |
140 | | - } |
141 | | - } |
142 | | - } |
143 | | - |
| 122 | + return left; |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + /** |
| 127 | + * 选择排序: O(n^2) |
| 128 | + * 每次遍历无序区选出最小的 |
| 129 | + */ |
| 130 | + public static void selectSort(int[] arr) { |
| 131 | + int minIndex = 0; |
| 132 | + for (int i = 0; i < arr.length - 1; i++) { |
| 133 | + minIndex = i;//指向最小的值的角标 |
| 134 | + for (int j = i + 1; j < arr.length; j++) {//遍历无序区 |
| 135 | + if (arr[j] < arr[minIndex]) { |
| 136 | + minIndex = j; |
| 137 | + } |
| 138 | + } |
| 139 | + if (minIndex != i) { //无序区遍历完了,找到最小的角标了,交换 |
| 140 | + int temp = arr[minIndex]; |
| 141 | + arr[minIndex] = arr[i]; |
| 142 | + arr[i] = temp; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
144 | 147 | } |
145 | 148 |
|
146 | 149 |
|
|
0 commit comments