`
lvmlvy
  • 浏览: 42577 次
社区版块
存档分类
最新评论

各种排序算法java实现收藏

    博客分类:
  • java
 
阅读更多

各种排序算法java实现收藏

 
插入排序:
 
  1. package org.rut.util.algorithm.support;
  2. import org.rut.util.algorithm.SortUtil;
  3. public class InsertSort implements SortUtil.Sort{
  4.     /* (non-Javadoc)
  5.      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  6.      */
  7.     public void sort(int[] data) {
  8.         int temp;
  9.         for(int i=1;i<data.length;i++){
  10.             for(int j=i;(j>0)&&(data[j]<data[j-1]);j--){
  11.                 SortUtil.swap(data,j,j-1);
  12.             }
  13.         }        
  14.     }
  15. }
冒泡排序:
 
  1. package org.rut.util.algorithm.support;
  2. import org.rut.util.algorithm.SortUtil;
  3. public class BubbleSort implements SortUtil.Sort{
  4.     /* (non-Javadoc)
  5.      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  6.      */
  7.     public void sort(int[] data) {
  8.         int temp;
  9.         for(int i=0;i<data.length;i++){
  10.             for(int j=data.length-1;j>i;j--){
  11.                 if(data[j]<data[j-1]){
  12.                     SortUtil.swap(data,j,j-1);
  13.                 }
  14.             }
  15.         }
  16.     }
  17. }
选择排序:
  1. package org.rut.util.algorithm.support;
  2. import org.rut.util.algorithm.SortUtil;
  3. public class SelectionSort implements SortUtil.Sort {
  4.     /*
  5.      * (non-Javadoc)
  6.      * 
  7.      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  8.      */
  9.     public void sort(int[] data) {
  10.         int temp;
  11.         for (int i = 0; i < data.length; i++) {
  12.             int lowIndex = i;
  13.             for (int j = data.length - 1; j > i; j--) {
  14.                 if (data[j] < data[lowIndex]) {
  15.                     lowIndex = j;
  16.                 }
  17.             }
  18.             SortUtil.swap(data,i,lowIndex);
  19.         }
  20.     }
  21. }
Shell排序:
  1. package org.rut.util.algorithm.support;
  2. import org.rut.util.algorithm.SortUtil;
  3. public class ShellSort implements SortUtil.Sort{
  4.     /* (non-Javadoc)
  5.      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  6.      */
  7.     public void sort(int[] data) {
  8.         for(int i=data.length/2;i>2;i/=2){
  9.             for(int j=0;j<i;j++){
  10.                 insertSort(data,j,i);
  11.             }
  12.         }
  13.         insertSort(data,0,1);
  14.     }
  15.     /**
  16.      * @param data
  17.      * @param j
  18.      * @param i
  19.      */
  20.     private void insertSort(int[] data, int start, int inc) {
  21.         int temp;
  22.         for(int i=start+inc;i<data.length;i+=inc){
  23.             for(int j=i;(j>=inc)&&(data[j]<data[j-inc]);j-=inc){
  24.                 SortUtil.swap(data,j,j-inc);
  25.             }
  26.         }
  27.     }
  28. }
快速排序:
  1. package org.rut.util.algorithm.support;
  2. import org.rut.util.algorithm.SortUtil;
  3. public class QuickSort implements SortUtil.Sort{
  4.     /* (non-Javadoc)
  5.      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  6.      */
  7.     public void sort(int[] data) {
  8.         quickSort(data,0,data.length-1);        
  9.     }
  10.     private void quickSort(int[] data,int i,int j){
  11.         int pivotIndex=(i+j)/2;
  12.         //swap
  13.         SortUtil.swap(data,pivotIndex,j);
  14.         
  15.         int k=partition(data,i-1,j,data[j]);
  16.         SortUtil.swap(data,k,j);
  17.         if((k-i)>1) quickSort(data,i,k-1);
  18.         if((j-k)>1) quickSort(data,k+1,j);
  19.         
  20.     }
  21.     /**
  22.      * @param data
  23.      * @param i
  24.      * @param j
  25.      * @return
  26.      */
  27.     private int partition(int[] data, int l, int r,int pivot) {
  28.         do{
  29.            while(data[++l]<pivot);
  30.            while((r!=0)&&data[--r]>pivot);
  31.            SortUtil.swap(data,l,r);
  32.         }
  33.         while(l<r);
  34.         SortUtil.swap(data,l,r);        
  35.         return l;
  36.     }
  37. }
改进后的快速排序:
  1. package org.rut.util.algorithm.support;
  2. import org.rut.util.algorithm.SortUtil;
  3. public class ImprovedQuickSort implements SortUtil.Sort {
  4.     private static int MAX_STACK_SIZE=4096;
  5.     private static int THRESHOLD=10;
  6.     /* (non-Javadoc)
  7.      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  8.      */
  9.     public void sort(int[] data) {
  10.         int[] stack=new int[MAX_STACK_SIZE];
  11.         
  12.         int top=-1;
  13.         int pivot;
  14.         int pivotIndex,l,r;
  15.         
  16.         stack[++top]=0;
  17.         stack[++top]=data.length-1;
  18.         
  19.         while(top>0){
  20.             int j=stack[top--];
  21.             int i=stack[top--];
  22.             
  23.             pivotIndex=(i+j)/2;
  24.             pivot=data[pivotIndex];
  25.             
  26.             SortUtil.swap(data,pivotIndex,j);
  27.             
  28.             //partition
  29.             l=i-1;
  30.             r=j;
  31.             do{
  32.                 while(data[++l]<pivot);
  33.                 while((r!=0)&&(data[--r]>pivot));
  34.                 SortUtil.swap(data,l,r);
  35.             }
  36.             while(l<r);
  37.             SortUtil.swap(data,l,r);
  38.             SortUtil.swap(data,l,j);
  39.             
  40.             if((l-i)>THRESHOLD){
  41.                 stack[++top]=i;
  42.                 stack[++top]=l-1;
  43.             }
  44.             if((j-l)>THRESHOLD){
  45.                 stack[++top]=l+1;
  46.                 stack[++top]=j;
  47.             }
  48.             
  49.         }
  50.         //new InsertSort().sort(data);
  51.         insertSort(data);
  52.     }
  53.     /**
  54.      * @param data
  55.      */
  56.     private void insertSort(int[] data) {
  57.         int temp;
  58.         for(int i=1;i<data.length;i++){
  59.             for(int j=i;(j>0)&&(data[j]<data[j-1]);j--){
  60.                 SortUtil.swap(data,j,j-1);
  61.             }
  62.         }       
  63.     }
  64. }
归并排序:
  1. package org.rut.util.algorithm.support;
  2. import org.rut.util.algorithm.SortUtil;
  3. public class MergeSort implements SortUtil.Sort{
  4.     /* (non-Javadoc)
  5.      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  6.      */
  7.     public void sort(int[] data) {
  8.         int[] temp=new int[data.length];
  9.         mergeSort(data,temp,0,data.length-1);
  10.     }
  11.     
  12.     private void mergeSort(int[] data,int[] temp,int l,int r){
  13.         int mid=(l+r)/2;
  14.         if(l==r) return ;
  15.         mergeSort(data,temp,l,mid);
  16.         mergeSort(data,temp,mid+1,r);
  17.         for(int i=l;i<=r;i++){
  18.             temp[i]=data[i];
  19.         }
  20.         int i1=l;
  21.         int i2=mid+1;
  22.         for(int cur=l;cur<=r;cur++){
  23.             if(i1==mid+1)
  24.                 data[cur]=temp[i2++];
  25.             else if(i2>r)
  26.                 data[cur]=temp[i1++];
  27.             else if(temp[i1]<temp[i2])
  28.                 data[cur]=temp[i1++];
  29.             else
  30.                 data[cur]=temp[i2++];            
  31.         }
  32.     }
  33. }
改进后的归并排序:
 
  1. package org.rut.util.algorithm.support;
  2. import org.rut.util.algorithm.SortUtil;
  3. public class ImprovedMergeSort implements SortUtil.Sort {
  4.     private static final int THRESHOLD = 10;
  5.     /*
  6.      * (non-Javadoc)
  7.      * 
  8.      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  9.      */
  10.     public void sort(int[] data) {
  11.         int[] temp=new int[data.length];
  12.         mergeSort(data,temp,0,data.length-1);
  13.     }
  14.     private void mergeSort(int[] data, int[] temp, int l, int r) {
  15.         int i, j, k;
  16.         int mid = (l + r) / 2;
  17.         if (l == r)
  18.             return;
  19.         if ((mid - l) >= THRESHOLD)
  20.             mergeSort(data, temp, l, mid);
  21.         else
  22.             insertSort(data, l, mid - l + 1);
  23.         if ((r - mid) > THRESHOLD)
  24.             mergeSort(data, temp, mid + 1, r);
  25.         else
  26.             insertSort(data, mid + 1, r - mid);
  27.         for (i = l; i <= mid; i++) {
  28.             temp[i] = data[i];
  29.         }
  30.         for (j = 1; j <= r - mid; j++) {
  31.             temp[r - j + 1] = data[j + mid];
  32.         }
  33.         int a = temp[l];
  34.         int b = temp[r];
  35.         for (i = l, j = r, k = l; k <= r; k++) {
  36.             if (a < b) {
  37.                 data[k] = temp[i++];
  38.                 a = temp[i];
  39.             } else {
  40.                 data[k] = temp[j--];
  41.                 b = temp[j];
  42.             }
  43.         }
  44.     }
  45.     /**
  46.      * @param data
  47.      * @param l
  48.      * @param i
  49.      */
  50.     private void insertSort(int[] data, int start, int len) {
  51.         for(int i=start+1;i<start+len;i++){
  52.             for(int j=i;(j>start) && data[j]<data[j-1];j--){
  53.                 SortUtil.swap(data,j,j-1);
  54.             }
  55.         }
  56.     }
  57. }
堆排序:
  1. package org.rut.util.algorithm.support;
  2. import org.rut.util.algorithm.SortUtil;
  3. public class HeapSort implements SortUtil.Sort{
  4.     /* (non-Javadoc)
  5.      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  6.      */
  7.     public void sort(int[] data) {
  8.         MaxHeap h=new MaxHeap();
  9.         h.init(data);
  10.         for(int i=0;i<data.length;i++)
  11.             h.remove();
  12.         System.arraycopy(h.queue,1,data,0,data.length);
  13.     }
  14.      private static class MaxHeap{
  15.          
  16.         
  17.         void init(int[] data){
  18.             this.queue=new int[data.length+1];
  19.             for(int i=0;i<data.length;i++){
  20.                 queue[++size]=data[i];
  21.                 fixUp(size);
  22.             }
  23.         }
  24.          
  25.         private int size=0;
  26.         private int[] queue;
  27.                 
  28.         public int get() {
  29.             return queue[1];
  30.         }
  31.         public void remove() {
  32.             SortUtil.swap(queue,1,size--);
  33.             fixDown(1);
  34.         }
  35.         //fixdown
  36.         private void fixDown(int k) {
  37.             int j;
  38.             while ((j = k << 1) <= size) {
  39.                 if (j < size && queue[j]<queue[j+1])
  40.                     j++; 
  41.                 if (queue[k]>queue[j]) //不用交换
  42.                     break;
  43.                 SortUtil.swap(queue,j,k);
  44.                 k = j;
  45.             }
  46.         }
  47.         private void fixUp(int k) {
  48.             while (k > 1) {
  49.                 int j = k >> 1;
  50.                 if (queue[j]>queue[k])
  51.                     break;
  52.                 SortUtil.swap(queue,j,k);
  53.                 k = j;
  54.             }
  55.         }
  56.     }
  57. }
SortUtil:
  1. package org.rut.util.algorithm;
  2. import org.rut.util.algorithm.support.BubbleSort;
  3. import org.rut.util.algorithm.support.HeapSort;
  4. import org.rut.util.algorithm.support.ImprovedMergeSort;
  5. import org.rut.util.algorithm.support.ImprovedQuickSort;
  6. import org.rut.util.algorithm.support.InsertSort;
  7. import org.rut.util.algorithm.support.MergeSort;
  8. import org.rut.util.algorithm.support.QuickSort;
  9. import org.rut.util.algorithm.support.SelectionSort;
  10. import org.rut.util.algorithm.support.ShellSort;
  11. public class SortUtil {
  12.     public final static int INSERT = 1;
  13.     public final static int BUBBLE = 2;
  14.     public final static int SELECTION = 3;
  15.     public final static int SHELL = 4;
  16.     public final static int QUICK = 5;
  17.     public final static int IMPROVED_QUICK = 6;
  18.     public final static int MERGE = 7;
  19.     public final static int IMPROVED_MERGE = 8;
  20.     public final static int HEAP = 9;
  21.     public static void sort(int[] data) {
  22.         sort(data, IMPROVED_QUICK);
  23.     }
  24.     private static String[] name={
  25.             "insert","bubble","selection","shell","quick","improved_quick","merge","improved_merge","heap"
  26.     };
  27.     
  28.     private static Sort[] impl=new Sort[]{
  29.             new InsertSort(),
  30.             new BubbleSort(),
  31.             new SelectionSort(),
  32.             new ShellSort(),
  33.             new QuickSort(),
  34.             new ImprovedQuickSort(),
  35.             new MergeSort(),
  36.             new ImprovedMergeSort(),
  37.             new HeapSort()
  38.     };
  39.     public static String toString(int algorithm){
  40.         return name[algorithm-1];
  41.     }
  42.     
  43.     public static void sort(int[] data, int algorithm) {
  44.         impl[algorithm-1].sort(data);
  45.     }
  46.     public static interface Sort {
  47.         public void sort(int[] data);
  48.     }
  49.     public static void swap(int[] data, int i, int j) {
  50.         int temp = data[i];
  51.         data[i] = data[j];
  52.         data[j] = temp;
  53.     }
  54. }
分享到:
评论
4 楼 yangjianzhouctgu 2013-05-13  
恩,知道了,谢谢啦!
3 楼 lvmlvy 2013-05-10  
yangjianzhouctgu 写道
请问org.rut.util.algorithm.SortUtil这个类在哪里下载啊?

写错了,在代码最下面
2 楼 yangjianzhouctgu 2013-05-01  
请问org.rut.util.algorithm.SortUtil这个类在哪里下载啊?
1 楼 yangjianzhouctgu 2013-05-01  
请问org.rut.util.algorithm.SortUtil这个类在哪里下载啊?

相关推荐

Global site tag (gtag.js) - Google Analytics