java排序大全

来源:互联网 发布:软件调整窗口大小 编辑:程序博客网 时间:2024/06/09 19:02

   java排序大全

插入排序:

 

  1.   package org.rut.util.algorithm.support;
  2.   import org.rut.util.algorithm.SortUtil;
  3.   /**
  4.   * @author treeroot
  5.   * @since 2006-2-2
  6.   * @version 1.0
  7.   */
  8.   public class InsertSort implements SortUtil.Sort
  9.   {
  10.    /* (non-Javadoc)
  11.    * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  12.    */
  13.    public void sort(int[] data) 
  14.    {
  15.     int temp;
  16.     for(int i=1;i for(int j=i;(j>0)&&(data[j] SortUtil.swap(data,j,j-1);
  17.    }
  18.   } 

  冒泡排序:

 

  1.   package org.rut.util.algorithm.support;
  2.   import org.rut.util.algorithm.SortUtil;
  3.   /**
  4.   * @author treeroot
  5.   * @since 2006-2-2
  6.   * @version 1.0
  7.   */
  8.   public class BubbleSort implements SortUtil.Sort
  9.   {
  10.    /* (non-Javadoc)
  11.    * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  12.    */
  13.    public void sort(int[] data) 
  14.    {
  15.     int temp;
  16.     for(int i=0;i for(int j=data.length-1;j>i;j--)
  17.     {
  18.      if(data[j] SortUtil.swap(data,j,j-1);
  19.     }
  20.    }
  21.   }

  选择排序:

  

  1. package org.rut.util.algorithm.support;
  2.   import org.rut.util.algorithm.SortUtil;
  3.   /**
  4.   * @author treeroot
  5.   * @since 2006-2-2
  6.   * @version 1.0
  7.   */
  8.   public class SelectionSort implements SortUtil.Sort 
  9.   {
  10.    /*
  11.    * (non-Javadoc)
  12.    * 
  13.    * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  14.    */
  15.    public void sort(int[] data) 
  16.    {
  17.     int temp;
  18.     for (int i = 0; i < data.length; i++) 
  19.     {
  20.      int lowIndex = i;
  21.      for (int j = data.length - 1; j >i; j--) 
  22.      {
  23.       if (data[j] < data[lowIndex]) 
  24.       {
  25.        lowIndex = j;
  26.       }
  27.      }
  28.      SortUtil.swap(data,i,lowIndex);
  29.     }
  30.    }
  31.   }

Shell排序:

  

  1. package org.rut.util.algorithm.support;
  2.   import org.rut.util.algorithm.SortUtil;
  3.   /**
  4.   * @author treeroot
  5.   * @since 2006-2-2
  6.   * @version 1.0
  7.   */
  8.   public class ShellSort implements SortUtil.Sort
  9.   {
  10.    /* (non-Javadoc)
  11.    * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  12.    */
  13.    public void sort(int[] data) 
  14.    {
  15.     for(int i=data.length/2;i>2;i/=2)
  16.     {
  17.      for(int j=0;j insertSort(data,j,i);
  18.     }
  19.    }
  20.    insertSort(data,0,1);
  21.   }
  22.   /**
  23.   * @param data
  24.   * @param j
  25.   * @param i
  26.   */
  27.   private void insertSort(int[] data, int start, int inc) 
  28.   {
  29.    int temp;
  30.    for(int i=start+inc;i for(int j=i;(j>=inc)&&(data[j] SortUtil.swap(data,j,j-inc);
  31.   }

  快速排序:

 

  1.  package org.rut.util.algorithm.support;
  2.   import org.rut.util.algorithm.SortUtil;
  3.   /**
  4.   * @author treeroot
  5.   * @since 2006-2-2
  6.   * @version 1.0
  7.   */
  8.   public class QuickSort implements SortUtil.Sort
  9.   {
  10.    /* (non-Javadoc)
  11.    * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  12.    */
  13.    public void sort(int[] data) 
  14.    {
  15.     quickSort(data,0,data.length-1); 
  16.    }
  17.    private void quickSort(int[] data,int i,int j)
  18.    {
  19.     int pivotIndex=(i+j)/2;
  20.     //swap
  21.     SortUtil.swap(data,pivotIndex,j);
  22.     int k=partition(data,i-1,j,data[j]);
  23.     SortUtil.swap(data,k,j);
  24.     if((k-i)>1) quickSort(data,i,k-1);
  25.     if((j-k)>1) quickSort(data,k+1,j);
  26.    }
  27.    /**
  28.    * @param data
  29.    * @param i
  30.    * @param j
  31.    * @return
  32.    */
  33.    private int partition(int[] data, int l, int r,int pivot) 
  34.    {
  35.     do
  36.     {
  37.      while(data[++l] while((r!=0)&&data[--r]>pivot);
  38.      SortUtil.swap(data,l,r);
  39.     }
  40.     while(l SortUtil.swap(data,l,r); 
  41.     return l;
  42.    }
  43.   }

  改进后的快速排序:

  

  1. package org.rut.util.algorithm.support;
  2.   import org.rut.util.algorithm.SortUtil;
  3.   /**
  4.   * @author treeroot
  5.   * @since 2006-2-2
  6.   * @version 1.0
  7.   */
  8.   public class ImprovedQuickSort implements SortUtil.Sort 
  9.   {
  10.    private static int MAX_STACK_SIZE=4096;
  11.    private static int THRESHOLD=10;
  12.    /* (non-Javadoc)
  13.    * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  14.    */
  15.    public void sort(int[] data) 
  16.    {
  17.     int[] stack=new int[MAX_STACK_SIZE];
  18.     int top=-1;
  19.     int pivot;
  20.     int pivotIndex,l,r;
  21.     stack[++top]=0;
  22.     stack[++top]=data.length-1;
  23.     while(top>0)
  24.     {
  25.      int j=stack[top--];
  26.      int i=stack[top--];
  27.      pivotIndex=(i+j)/2;
  28.      pivot=data[pivotIndex];
  29.      SortUtil.swap(data,pivotIndex,j);
  30.      //partition
  31.      l=i-1;
  32.      r=j;
  33.      do
  34.      {
  35.       while(data[++l] while((r!=0)&&(data[--r]>pivot));
  36.       SortUtil.swap(data,l,r);
  37.      }
  38.      while(l SortUtil.swap(data,l,r);
  39.      SortUtil.swap(data,l,j);
  40.      if((l-i)>THRESHOLD)
  41.      {
  42.       stack[++top]=i;
  43.       stack[++top]=l-1;
  44.      }
  45.      if((j-l)>THRESHOLD)
  46.      {
  47.       stack[++top]=l+1;
  48.       stack[++top]=j;
  49.      }
  50.     }
  51.     //new InsertSort().sort(data);
  52.     insertSort(data);
  53.    }
  54.    /**
  55.    * @param data
  56.    */
  57.    private void insertSort(int[] data) 
  58.    {
  59.     int temp;
  60.     for(int i=1;i for(int j=i;(j>0)&&(data[j] SortUtil.swap(data,j,j-1);
  61.    }
  62.   } 

归并排序:

  

  1. package org.rut.util.algorithm.support;
  2.   import org.rut.util.algorithm.SortUtil;
  3.   /**
  4.   * @author treeroot
  5.   * @since 2006-2-2
  6.   * @version 1.0
  7.   */
  8.   public class MergeSort implements SortUtil.Sort
  9.   {
  10.    /* (non-Javadoc)
  11.    * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  12.    */
  13.    public void sort(int[] data) 
  14.    {
  15.     int[] temp=new int[data.length];
  16.     mergeSort(data,temp,0,data.length-1);
  17.    }
  18.    private void mergeSort(int[] data,int[] temp,int l,int r)
  19.    {
  20.     int mid=(l+r)/2;
  21.     if(l==r) return ;
  22.     mergeSort(data,temp,l,mid);
  23.     mergeSort(data,temp,mid+1,r);
  24.     for(int i=l;i<=r;i++){
  25.     temp[i]=data[i];
  26.    }
  27.    int i1=l;
  28.    int i2=mid+1;
  29.    for(int cur=l;cur<=r;cur++)
  30.    {
  31.     if(i1==mid+1)
  32.     data[cur]=temp[i2++];
  33.     else if(i2>r)
  34.     data[cur]=temp[i1++];
  35.     else if(temp[i1] data[cur]=temp[i1++];
  36.     else
  37.     data[cur]=temp[i2++]; 
  38.    }
  39.   }
  40.  

  改进后的归并排序:

 

  1.  package org.rut.util.algorithm.support;
  2.   import org.rut.util.algorithm.SortUtil;
  3.   /**
  4.   * @author treeroot
  5.   * @since 2006-2-2
  6.   * @version 1.0
  7.   */
  8.   public class ImprovedMergeSort implements SortUtil.Sort 
  9.   {
  10.    private static final int THRESHOLD = 10;
  11.    /*
  12.    * (non-Javadoc)
  13.    * 
  14.    * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  15.    */
  16.    public void sort(int[] data) 
  17.    {
  18.     int[] temp=new int[data.length];
  19.     mergeSort(data,temp,0,data.length-1);
  20.    }
  21.    private void mergeSort(int[] data, int[] temp, int l, int r) 
  22.    {
  23.     int i, j, k;
  24.     int mid = (l + r) / 2;
  25.     if (l == r)
  26.     return;
  27.     if ((mid - l) >= THRESHOLD)
  28.     mergeSort(data, temp, l, mid);
  29.     else
  30.     insertSort(data, l, mid - l + 1);
  31.     if ((r - mid) >THRESHOLD)
  32.     mergeSort(data, temp, mid + 1, r);
  33.     else
  34.     insertSort(data, mid + 1, r - mid);
  35.     for (i = l; i <= mid; i++) 
  36.     {
  37.      temp[i] = data[i];
  38.     }
  39.     for (j = 1; j <= r - mid; j++) 
  40.     {
  41.      temp[r - j + 1] = data[j + mid];
  42.     }
  43.     int a = temp[l];
  44.     int b = temp[r];
  45.     for (i = l, j = r, k = l; k <= r; k++) 
  46.     {
  47.      if (a < b) 
  48.      {
  49.       data[k] = temp[i++];
  50.       a = temp[i];
  51.      } 
  52.      else 
  53.      {
  54.       data[k] = temp[j--];
  55.       b = temp[j];
  56.      }
  57.     }
  58.    }
  59.    /**
  60.    * @param data
  61.    * @param l
  62.    * @param i
  63.    */
  64.    private void insertSort(int[] data, int start, int len) 
  65.    {
  66.     for(int i=start+1;i for(int j=i;(j>start) && data[j] SortUtil.swap(data,j,j-1);
  67.    }
  68.   }

堆排序:

  1.   package org.rut.util.algorithm.support;
  2.   import org.rut.util.algorithm.SortUtil;
  3.   /**
  4.   * @author treeroot
  5.   * @since 2006-2-2
  6.   * @version 1.0
  7.   */
  8.   public class HeapSort implements SortUtil.Sort
  9.   {
  10.    /* (non-Javadoc)
  11.    * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  12.    */
  13.    public void sort(int[] data) 
  14.    {
  15.     MaxHeap h=new MaxHeap();
  16.     h.init(data);
  17.     for(int i=0;i h.remove();
  18.     System.arraycopy(h.queue,1,data,0,data.length);
  19.    }
  20.    private static class MaxHeap
  21.    { 
  22.     void init(int[] data)
  23.     {
  24.      this.queue=new int[data.length+1];
  25.      for(int i=0;i queue[++size]=data[i];
  26.      fixUp(size);
  27.     }
  28.    }
  29.    private int size=0;
  30.    private int[] queue;
  31.    public int get() 
  32.    {
  33.     return queue[1];
  34.    }
  35.    public void remove() 
  36.    {
  37.     SortUtil.swap(queue,1,size--);
  38.     fixDown(1);
  39.    }
  40.    //fixdown
  41.    private void fixDown(int k) 
  42.    {
  43.     int j;
  44.     while ((j = k << 1) <= size) 
  45.     {
  46.      if (j < size && queue[j] j++; 
  47.      if (queue[k]>queue[j]) //不用交换
  48.      break;
  49.      SortUtil.swap(queue,j,k);
  50.      k = j;
  51.     }
  52.    }
  53.    private void fixUp(int k) 
  54.    {
  55.     while (k >1
  56.     {
  57.      int j = k >>1;
  58.      if (queue[j]>queue[k])
  59.      break;
  60.      SortUtil.swap(queue,j,k);
  61.      k = j;
  62.     }
  63.    }
  64.   }
  65.   SortUtil:
  66.   package org.rut.util.algorithm;
  67.   import org.rut.util.algorithm.support.BubbleSort;
  68.   import org.rut.util.algorithm.support.HeapSort;
  69.   import org.rut.util.algorithm.support.ImprovedMergeSort;
  70.   import org.rut.util.algorithm.support.ImprovedQuickSort;
  71.   import org.rut.util.algorithm.support.InsertSort;
  72.   import org.rut.util.algorithm.support.MergeSort;
  73.   import org.rut.util.algorithm.support.QuickSort;
  74.   import org.rut.util.algorithm.support.SelectionSort;
  75.   import org.rut.util.algorithm.support.ShellSort;
  76.   /**
  77.   * @author treeroot
  78.   * @since 2006-2-2
  79.   * @version 1.0
  80.   */
  81.   public class SortUtil 
  82.   {
  83.    public final static int INSERT = 1;
  84.    public final static int BUBBLE = 2;
  85.    public final static int SELECTION = 3;
  86.    public final static int SHELL = 4;
  87.    public final static int QUICK = 5;
  88.    public final static int IMPROVED_QUICK = 6;
  89.    public final static int MERGE = 7;
  90.    public final static int IMPROVED_MERGE = 8;
  91.    public final static int HEAP = 9;
  92.    public static void sort(int[] data) 
  93.    {
  94.     sort(data, IMPROVED_QUICK);
  95.    }
  96.    private static String[] name=
  97.    {
  98.     "insert""bubble""selection""shell""quick""improved_quick""merge""improved_merge""heap"
  99.    };
  100.    private static Sort[] impl=new Sort[]
  101.    {
  102.     new InsertSort(),
  103.     new BubbleSort(),
  104.     new SelectionSort(),
  105.     new ShellSort(),
  106.     new QuickSort(),
  107.     new ImprovedQuickSort(),
  108.     new MergeSort(),
  109.     new ImprovedMergeSort(),
  110.     new HeapSort()
  111.    };
  112.    public static String toString(int algorithm)
  113.    {
  114.     return name[algorithm-1];
  115.    }
  116.    public static void sort(int[] data, int algorithm) 
  117.    {
  118.     impl[algorithm-1].sort(data);
  119.    }
  120.    public static interface Sort 
  121.    {
  122.     public void sort(int[] data);
  123.    }
  124.    public static void swap(int[] data, int i, int j) 
  125.    {
  126.     int temp = data[i];
  127.     data[i] = data[j];
  128.     data[j] = temp;
  129.    }
  130.   }