Java数据结构与算法--高级排序

(1)快速排序

  1. /** 
  2.  * 
  3.  * @author sunnyykn 
  4.  */  
  5. class ArrayIns  
  6. {  
  7.     private long[] theArray;            //ref to array theArray  
  8.     private int nElems;                 //number of data items  
  9.     public ArrayIns(int max)           //constructor  
  10.     {  
  11.         theArray = new long[max];       //create the array  
  12.         nElems = 0;                     //no items yet  
  13.     }  
  14.     public void insert(long value)     //put element into array  
  15.     {  
  16.         theArray[nElems] = value;       //insert it  
  17.         nElems ++;                      //increment size  
  18.     }  
  19.     public void display()  
  20.     {  
  21.         System.out.print("A = ");  
  22.         for(int j = 0;j < nElems;j ++)  
  23.             System.out.print(theArray[j] + " ");  
  24.         System.out.println("");  
  25.     }  
  26.     public void quickSort()  
  27.     {  
  28.         recQuickSort(0,nElems - 1);  
  29.     }  
  30.     public void recQuickSort(int left,int right)  
  31.     {  
  32.         int size = right - left + 1;  
  33.         if(size < 10)                                   //insertion sort if small  
  34.             insertionSort(left,right);  
  35.         else                                            //quicksort if large  
  36.         {  
  37.             long median = medianOf3(left,right);  
  38.             int partition = partitionIt(left,right,median);  
  39.             recQuickSort(left,partition - 1);  
  40.             recQuickSort(partition + 1,right);  
  41.         }  
  42.     }  
  43.     public long medianOf3(int left,int right)  
  44.     {  
  45.         int center = (left + right)/2;  
  46.         if( theArray[left] > theArray[center] )     //order left & center  
  47.             swap(left,right);  
  48.         if( theArray[left] > theArray[right] )      //order left & right  
  49.             swap(left,right);  
  50.         if( theArray[center] > theArray[right] )    //order center & right  
  51.             swap(center,right);  
  52.         swap(center,right - 1);  
  53.         return theArray[right - 1];  
  54.     }  
  55.     public void swap(int dex1,int dex2)  
  56.     {  
  57.         long temp = theArray[dex1];  
  58.         theArray[dex1] = theArray[dex2];  
  59.         theArray[dex2] = temp;  
  60.     }  
  61.     public int partitionIt(int left,int right,long pivot)  
  62.     {  
  63.         int leftPtr = left;  
  64.         int rightPtr = right;  
  65.         while(true)  
  66.         {  
  67.             while( theArray[++ leftPtr] < pivot );  
  68.             while( theArray[-- rightPtr] > pivot );  
  69.             if(leftPtr >= rightPtr)  
  70.                 break;  
  71.             else  
  72.                 swap(leftPtr,rightPtr);  
  73.         }  
  74.         swap(leftPtr,right - 1);  
  75.         return leftPtr;  
  76.     }  
  77.     public void insertionSort(int left,int right)  
  78.     {  
  79.         int in,out;  
  80.         for(out = left + 1;out <= right;out ++)  
  81.         {  
  82.             long temp = theArray[out];  
  83.             in = out;  
  84.             while(in > left && theArray[in - 1] >= temp)  
  85.             {  
  86.                 theArray[in] = theArray[in - 1];  
  87.                 -- in;  
  88.             }  
  89.             theArray[in] = temp;  
  90.         }  
  91.     }  
  92. }  
  93. class QuickSortApp  
  94. {  
  95.     public static void main(String[] args)  
  96.     {  
  97.         int maxSize = 50;               //array size  
  98.         ArrayIns arr;                   //reference to array  
  99.         arr = new ArrayIns(maxSize);    //create the array  
  100.         for(int j = 0;j < maxSize;j ++) //fill array with  
  101.         {                               //random numbers  
  102.             long n = (int)(java.lang.Math.random()*99);  
  103.             arr.insert(n);  
  104.         }  
  105.         arr.display();                  //display items  
  106.         arr.quickSort();                //quicksort them  
  107.         arr.display();                  //display them again  
  108.     }  
  109. }  
(2)希尔排序

public void shellSort{
  int inner, outer;
  long temp;
  int h = 1;
  while(h <= nElems/3)
   h = h*3 + 1;
  while(h > 0){
   for(outer = h; outer < nElems; outer++){
    temp = theArray[outer];
    inner = outer;
    while(inner > h - 1 && theArray[inner - h] >= temp){
     theArray[inner] = theArray[inner - h];
     inner -= h;
    }
    theArray[inner] = temp;
   }
   h = (h - 1)/3;
  }
 }



你可能感兴趣的:(Java数据结构与算法--高级排序)