Java中各种排序算法

[1].[代码] [Java]代码 跳至 [1]

?
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
//插入排序:
 
package org.rut.util.algorithm.support;
 
import org.rut.util.algorithm.SortUtil;
/**
  * @author treeroot
  * @since 2006-2-2
  * @version 1.0
  */
public class InsertSort implements SortUtil.Sort{
 
     /** (non-Javadoc)
      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
      */
     public void sort( int [] data) {
         int temp;
         for ( int i= 1 ;i<data.length;i++){
             for ( int j=i;(j> 0 )&&(data[j]<data[j- 1 ]);j--){
                 SortUtil.swap(data,j,j- 1 );
             }
         }       
     }
 
}
//冒泡排序:
 
 
for (var i= 0 ; i<arr.length; i++) {   
     for (var j=i+ 1 ; j<=arr.length- 1 ; j++) {
         if (eval(arr[i]) < eval(arr[j])) {       
             temp = arr[i];                     
             arr[i] = arr[j];                       
             arr[j] = temp;                                             
         }
     }
}
 
 
 
package org.rut.util.algorithm.support;
 
import org.rut.util.algorithm.SortUtil;
 
/**
  * @author treeroot
  * @since 2006-2-2
  * @version 1.0
  */
public class BubbleSort implements SortUtil.Sort{
 
     /** (non-Javadoc)
      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
      */
     public void sort( int [] data) {
         int temp;
         for ( int i= 0 ;i<data.length;i++){
             for ( int j=data.length- 1 ;j>i;j--){
                 if (data[j]<data[j- 1 ]){
                     SortUtil.swap(data,j,j- 1 );
                 }
             }
         }
     }
 
}
 
//选择排序:
 
package org.rut.util.algorithm.support;
 
import org.rut.util.algorithm.SortUtil;
 
/**
  * @author treeroot
  * @since 2006-2-2
  * @version 1.0
  */
public class SelectionSort implements SortUtil.Sort {
 
     /**
      * (non-Javadoc)
      *
      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
      */
     public void sort( int [] data) {
         int temp;
         for ( int i = 0 ; i < data.length; i++) {
             int lowIndex = i;
             for ( int j = data.length - 1 ; j > i; j--) {
                 if (data[j] < data[lowIndex]) {
                     lowIndex = j;
                 }
             }
             SortUtil.swap(data,i,lowIndex);
         }
     }
 
}
 
//Shell排序:
 
package org.rut.util.algorithm.support;
 
import org.rut.util.algorithm.SortUtil;
 
/**
  * @author treeroot
  * @since 2006-2-2
  * @version 1.0
  */
public class ShellSort implements SortUtil.Sort{
 
     /** (non-Javadoc)
      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
      */
     public void sort( int [] data) {
         for ( int i=data.length/ 2 ;i> 2 ;i/= 2 ){
             for ( int j= 0 ;j<i;j++){
                 insertSort(data,j,i);
             }
         }
         insertSort(data, 0 , 1 );
     }
 
     /**
      * @param data
      * @param j
      * @param i
      */
     private void insertSort( int [] data, int start, int inc) {
         int temp;
         for ( int i=start+inc;i<data.length;i+=inc){
             for ( int j=i;(j>=inc)&&(data[j]<data[j-inc]);j-=inc){
                 SortUtil.swap(data,j,j-inc);
             }
         }
     }
 
}
 
//快速排序:
 
package org.rut.util.algorithm.support;
 
import org.rut.util.algorithm.SortUtil;
 
/**
  * @author treeroot
  * @since 2006-2-2
  * @version 1.0
  */
public class QuickSort implements SortUtil.Sort{
 
     /** (non-Javadoc)
      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
      */
     public void sort( int [] data) {
         quickSort(data, 0 ,data.length- 1 );       
     }
     private void quickSort( int [] data, int i, int j){
         int pivotIndex=(i+j)/ 2 ;
         //swap
         SortUtil.swap(data,pivotIndex,j);
         
         int k=partition(data,i- 1 ,j,data[j]);
         SortUtil.swap(data,k,j);
         if ((k-i)> 1 ) quickSort(data,i,k- 1 );
         if ((j-k)> 1 ) quickSort(data,k+ 1 ,j);
         
     }
     /**
      * @param data
      * @param i
      * @param j
      * @return
      */
     private int partition( int [] data, int l, int r, int pivot) {
         do {
            while (data[++l]<pivot);
            while ((r!= 0 )&&data[--r]>pivot);
            SortUtil.swap(data,l,r);
         }
         while (l<r);
         SortUtil.swap(data,l,r);       
         return l;
     }
 
}
//改进后的快速排序:
 
package org.rut.util.algorithm.support;
 
import org.rut.util.algorithm.SortUtil;
 
/**
  * @author treeroot
  * @since 2006-2-2
  * @version 1.0
  */
public class ImprovedQuickSort implements SortUtil.Sort {
 
     private static int MAX_STACK_SIZE= 4096 ;
     private static int THRESHOLD= 10 ;
     /** (non-Javadoc)
      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
      */
     public void sort( int [] data) {
         int [] stack= new int [MAX_STACK_SIZE];
         
         int top=- 1 ;
         int pivot;
         int pivotIndex,l,r;
         
         stack[++top]= 0 ;
         stack[++top]=data.length- 1 ;
         
         while (top> 0 ){
             int j=stack[top--];
             int i=stack[top--];
             
             pivotIndex=(i+j)/ 2 ;
             pivot=data[pivotIndex];
             
             SortUtil.swap(data,pivotIndex,j);
             
             //partition
             l=i- 1 ;
             r=j;
             do {
                 while (data[++l]<pivot);
                 while ((r!= 0 )&&(data[--r]>pivot));
                 SortUtil.swap(data,l,r);
             }
             while (l<r);
             SortUtil.swap(data,l,r);
             SortUtil.swap(data,l,j);
             
             if ((l-i)>THRESHOLD){
                 stack[++top]=i;
                 stack[++top]=l- 1 ;
             }
             if ((j-l)>THRESHOLD){
                 stack[++top]=l+ 1 ;
                 stack[++top]=j;
             }
             
         }
         //new InsertSort().sort(data);
         insertSort(data);
     }
     /**
      * @param data
      */
     private void insertSort( int [] data) {
         int temp;
         for ( int i= 1 ;i<data.length;i++){
             for ( int j=i;(j> 0 )&&(data[j]<data[j- 1 ]);j--){
                 SortUtil.swap(data,j,j- 1 );
             }
         }      
     }
 
}
 
//归并排序:
 
package org.rut.util.algorithm.support;
 
import org.rut.util.algorithm.SortUtil;
 
/**
  * @author treeroot
  * @since 2006-2-2
  * @version 1.0
  */
public class MergeSort implements SortUtil.Sort{
 
     /** (non-Javadoc)
      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
      */
     public void sort( int [] data) {
         int [] temp= new int [data.length];
         mergeSort(data,temp, 0 ,data.length- 1 );
     }
     
     private void mergeSort( int [] data, int [] temp, int l, int r){
         int mid=(l+r)/ 2 ;
         if (l==r) return ;
         mergeSort(data,temp,l,mid);
         mergeSort(data,temp,mid+ 1 ,r);
         for ( int i=l;i<=r;i++){
             temp[i]=data[i];
         }
         int i1=l;
         int i2=mid+ 1 ;
         for ( int cur=l;cur<=r;cur++){
             if (i1==mid+ 1 )
                 data[cur]=temp[i2++];
             else if (i2>r)
                 data[cur]=temp[i1++];
             else if (temp[i1]<temp[i2])
                 data[cur]=temp[i1++];
             else
                 data[cur]=temp[i2++];           
         }
     }
 
}
 
//改进后的归并排序:
 
package org.rut.util.algorithm.support;
 
import org.rut.util.algorithm.SortUtil;
 
/**
  * @author treeroot
  * @since 2006-2-2
  * @version 1.0
  */
public class ImprovedMergeSort implements SortUtil.Sort {
 
     private static final int THRESHOLD = 10 ;
 
     /**
      * (non-Javadoc)
      *
      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
      */
     public void sort( int [] data) {
         int [] temp= new int [data.length];
         mergeSort(data,temp, 0 ,data.length- 1 );
     }
 
     private void mergeSort( int [] data, int [] temp, int l, int r) {
         int i, j, k;
         int mid = (l + r) / 2 ;
         if (l == r)
             return ;
         if ((mid - l) >= THRESHOLD)
             mergeSort(data, temp, l, mid);
         else
             insertSort(data, l, mid - l + 1 );
         if ((r - mid) > THRESHOLD)
             mergeSort(data, temp, mid + 1 , r);
         else
             insertSort(data, mid + 1 , r - mid);
 
         for (i = l; i <= mid; i++) {
             temp[i] = data[i];
         }
         for (j = 1 ; j <= r - mid; j++) {
             temp[r - j + 1 ] = data[j + mid];
         }
         int a = temp[l];
         int b = temp[r];
         for (i = l, j = r, k = l; k <= r; k++) {
             if (a < b) {
                 data[k] = temp[i++];
                 a = temp[i];
             } else {
                 data[k] = temp[j--];
                 b = temp[j];
             }
         }
     }
 
     /**
      * @param data
      * @param l
      * @param i
      */
     private void insertSort( int [] data, int start, int len) {
         for ( int i=start+ 1 ;i<start+len;i++){
             for ( int j=i;(j>start) && data[j]<data[j- 1 ];j--){
                 SortUtil.swap(data,j,j- 1 );
             }
         }
     }
 
}
//堆排序:
 
package org.rut.util.algorithm.support;
 
import org.rut.util.algorithm.SortUtil;
 
/**
  * @author treeroot
  * @since 2006-2-2
  * @version 1.0
  */
public class HeapSort implements SortUtil.Sort{
 
     /** (non-Javadoc)
      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
      */
     public void sort( int [] data) {
         MaxHeap h= new MaxHeap();
         h.init(data);
         for ( int i= 0 ;i<data.length;i++)
             h.remove();
         System.arraycopy(h.queue, 1 ,data, 0 ,data.length);
     }
 
 
      private static class MaxHeap{
          
         
         void init( int [] data){
             this .queue= new int [data.length+ 1 ];
             for ( int i= 0 ;i<data.length;i++){
                 queue[++size]=data[i];
                 fixUp(size);
             }
         }
          
         private int size= 0 ;
 
         private int [] queue;
                 
         public int get() {
             return queue[ 1 ];
         }
 
         public void remove() {
             SortUtil.swap(queue, 1 ,size--);
             fixDown( 1 );
         }
         //fixdown
         private void fixDown( int k) {
             int j;
             while ((j = k << 1 ) <= size) {
                 if (j < size && queue[j]<queue[j+ 1 ])
                     j++;
                 if (queue[k]>queue[j]) //不用交换
                     break ;
                 SortUtil.swap(queue,j,k);
                 k = j;
             }
         }
         private void fixUp( int k) {
             while (k > 1 ) {
                 int j = k >> 1 ;
                 if (queue[j]>queue[k])
                     break ;
                 SortUtil.swap(queue,j,k);
                 k = j;
             }
         }
 
     }
 
}
 
  
 
//SortUtil:
 
package org.rut.util.algorithm;
 
import org.rut.util.algorithm.support.BubbleSort;
import org.rut.util.algorithm.support.HeapSort;
import org.rut.util.algorithm.support.ImprovedMergeSort;
import org.rut.util.algorithm.support.ImprovedQuickSort;
import org.rut.util.algorithm.support.InsertSort;
import org.rut.util.algorithm.support.MergeSort;
import org.rut.util.algorithm.support.QuickSort;
import org.rut.util.algorithm.support.SelectionSort;
import org.rut.util.algorithm.support.ShellSort;
 
/**
  * @author treeroot
  * @since 2006-2-2
  * @version 1.0
  */
public class SortUtil {
     public final static int INSERT = 1 ;
 
     public final static int BUBBLE = 2 ;
 
     public final static int SELECTION = 3 ;
 
     public final static int SHELL = 4 ;
 
     public final static int QUICK = 5 ;
 
     public final static int IMPROVED_QUICK = 6 ;
 
     public final static int MERGE = 7 ;
 
     public final static int IMPROVED_MERGE = 8 ;
 
     public final static int HEAP = 9 ;
 
     public static void sort( int [] data) {
         sort(data, IMPROVED_QUICK);
     }
     private static String[] name={
             "insert" , "bubble" , "selection" , "shell" , "quick" , "improved_quick" , "merge" , "improved_merge" , "heap"
     };
     
     private static Sort[] impl= new Sort[]{
             new InsertSort(),
             new BubbleSort(),
             new SelectionSort(),
             new ShellSort(),
             new QuickSort(),
             new ImprovedQuickSort(),
             new MergeSort(),
             new ImprovedMergeSort(),
             new HeapSort()
     };
 
     public static String toString( int algorithm){
         return name[algorithm- 1 ];
     }
     
     public static void sort( int [] data, int algorithm) {
         impl[algorithm- 1 ].sort(data);
     }
 
     public static interface Sort {
         public void sort( int [] data);
     }
 
     public static void swap( int [] data, int i, int j) {
         int temp = data[i];
         data[i] = data[j];
         data[j] = temp;
     }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值