备战校招——常见排序
算法简单,但要想写出稳定健壮性好的排序程序还是很有挑战的,尤其是对边界的控制。
直接上干货吧,Resource Code.....:
1 public class Sort {
2
3 public static void main(String [] argv){
4
5
6 int [] source0 = {5,3,7,9,1,2,6,4,8};
7 int [] source1 = {5,3,7,9,1,2,6,4,8};
8 int [] source2 = {5,3,7,9,1,2,6,4,8};
9 int [] source3 = {5,3,7,9,1,2,6,4,8};
10 //* 快排测试 *//
11 Sort test = new Sort();
12 test.Quick_sort(source0,0,source0.length-1);
13 System.out.println("Quick sort result:");
14 for (int k:source0){
15 System.out.print(" "+k);
16 }
17 System.out.println();
18
19 //* 冒泡测试 *//
20 test.Bubble_sort(source1);
21 System.out.println("Bubble sort result:");
22 for (int k:source1){
23 System.out.print(" "+k);
24 }
25 System.out.println();
26
27 //* 归并测试 *//
28 test.Merge_sort(source2, 0, source2.length-1);
29 System.out.println("Merge sort result:");
30 for (int k:source2){
31 System.out.print(" "+k);
32 }
33 System.out.println();
34
35 //* 堆排测试 *//
36 test.Heap_sort(source3);
37 System.out.println("Heap sort result:");
38 for (int k:source3){
39 System.out.print(" "+k);
40 }
41 }
42
43 //* 堆排序 *//
44 public void Heap_sort(int[] arr){
45 int [] Result = new int[arr.length];
46
47 for(int i=0; i<arr.length; i++){
48 Heap_up(Result,i, arr[i]);
49 }
50 //System.out.println("............");
51 for (int i=arr.length; i>=1; i--){
52 //System.out.print(" "+Result[0]);
53 Heap_down(Result,i);
54 }
55 for(int i=0; i<arr.length; i++){
56 arr[i]=Result[arr.length-1-i];
57 //System.out.println(Result[i]);
58 }
59 }
60
61 public void Heap_up(int[] arr,int n, int n_Num){
62
63 arr[n]=n_Num;
64 Heap_up_adjust(arr,n);
65
66 }
67
68 public void Heap_up_adjust(int[] arr,int i){
69
70 int j,temp;
71 j=(i-1)/2;
72 temp = arr[i];
73 while(j>=0&&i!=0){
74
75 if(arr[j]<=temp)
76 break;
77 arr[i]=arr[j];
78 i=j;
79 j=(i-1)/2;
80 }
81 arr[i]=temp;
82 }
83
84 public void Heap_down(int[] arr,int n){
85
86 int tmp = arr[0];
87 arr[0]=arr[n-1];
88 arr[n-1]=tmp;
89 Heap_down_adjust(arr, n);
90 }
91
92 public void Heap_down_adjust(int[] arr, int n){
93
94 int i=0;
95 int j = 2*i+1;
96 int tmp=arr[0];
97 while(j<n-1){
98 if (j + 1 < n-1 && arr[j + 1] < arr[j])
99 j++;
100 if(arr[j]>=tmp)
101 break;
102 arr[i]=arr[j];
103 i=j;
104 j=2*i+1;
105 }
106 arr[i]=tmp;
107 }
108
109 //* 归并排序 *//
110 public void Merge_sort(int[] arr, int x, int y){
111 int mid = (x+y+1)/2;
112 //System.out.println(mid);
113 if(mid-1>x) Merge_sort(arr,x,mid-1);
114 if(y>mid) Merge_sort(arr,mid,y);
115 merge(arr,x,mid,y);
116 }
117 public void merge(int[] arr, int s, int m, int t){
118 int[] tmp = new int[t-s+1];
119 int i = 0;
120 int j = s;
121 int k = m;
122 while(!(j==m&&k==t+1)){
123 while(j<m){
124 if(k==t+1){
125 tmp[i]=arr[j];
126 //System.out.println(tmp[i]+" "+arr[j]);
127 //System.out.println("temp "+i+" "+tmp[i]+" "+j+" "+arr[j]);
128 i++;
129 j++;
130
131 }
132 else{
133 if(arr[j]<=arr[k]){
134 tmp[i]=arr[j];
135 //System.out.println(tmp[i]+" "+arr[j]);
136 //System.out.println("temp "+i+" "+tmp[i]+" "+j+" "+arr[j]);
137 i++;
138 j++;
139 }
140 else
141 break;
142 }
143
144 }
145 while(k<=t){
146 if(j==m){
147 tmp[i]=arr[k];
148 //System.out.println(tmp[i]+" "+arr[k]);
149 //System.out.println("temp "+i+" "+tmp[i]+" "+k+" "+arr[k]);
150 i++;
151 k++;
152 }
153 else{
154 if(arr[k]<=arr[j]){
155
156 tmp[i]=arr[k];
157 //System.out.println(tmp[i]+" "+arr[j]);
158 //System.out.println("temp "+i+" "+tmp[i]+" "+k+" "+arr[k]);
159 i++;
160 k++;
161 }
162 else
163 break;
164 }
165
166 }
167 }
168 for(int p=s; p<=t; p++){
169 arr[p]=tmp[p-s];
170 //System.out.print(" "+arr[p]);
171 }
172 }
173
174 //* 冒泡排序 *//
175 public void Bubble_sort(int[] arr){
176
177 for(int i=0; i<arr.length-1;i++){
178 for(int j=0; j<arr.length-1-i;j++){
179 //System.out.println("i: "+i+" j: "+j);
180 if(arr[j]>arr[j+1]){
181 int temp =arr[j];
182 arr[j]=arr[j+1];
183 arr[j+1]=temp;
184 }
185 }
186
187 }
188
189 }
190
191 //* 快速排序 *//
192 public void Quick_sort(int[] arr, int low, int high)
193 {
194 int l=low;
195 int h=high;
196 int povit=arr[low];
197
198 while(l<h)
199 {
200 while(l<h&&arr[h]>=povit)
201 h--;
202 if(l<h){
203 int temp=arr[h];
204 arr[h]=arr[l];
205 arr[l]=temp;
206 l++;
207 }
208
209 while(l<h&&arr[l]<=povit)
210 l++;
211
212 if(l<h){
213 int temp=arr[h];
214 arr[h]=arr[l];
215 arr[l]=temp;
216 h--;
217 }
218 }
219 // System.out.print(arr);
220 // System.out.print("l="+(l+1)+"h="+(h+1)+"povit="+povit+"\n");
221 if(l>low) Quick_sort(arr,low,l-1);
222 if(h<high) Quick_sort(arr,l+1,high);
223 }
224
225
226 }