feitian

各种排序算法java实现 (转自http://duketian.blog.chinajavaworld.com/entry/3852/0/)

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

posted on 2011-04-24 12:04 飞天wfu 阅读(199) 评论(0)  编辑  收藏 所属分类: J2ME


只有注册用户登录后才能发表评论。


网站导航: