用Java实现几种常见的排序算法

用Java语言实现的各种排序,包括插入排序、冒泡排序、选择排序、Shell排序、快速排序、归并排序、堆排序、SortUtil等。


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

你可能感兴趣的:(java,算法,J#)