快速排序Java源碼(遞歸和非遞歸)

http://blog.csdn.net/bnuside/article/details/6906688


  1. ackage com.side.quicksort;  
  2.   
  3. import com.side.tests.Stack;//作者自己定義的棧類  
  4.   
  5. public class QuickSort {  
  6.   
  7.     /** 
  8.      * @param args 
  9.      */  
  10.     public static void main(String[] args) {  
  11.         // TODO Auto-generated method stub  
  12.         QuickSort t=new QuickSort();  
  13.         t.test();  
  14.     }  
  15.     public void test(){  
  16.         //int a[]={10,1,4,7,8,6,3,4,4,4,4,4,2,5,9,4,2};  
  17.         int a[]={};  
  18.         printArray(a);  
  19.         //quickSort(a,0,a.length-1);  
  20.         //nonRecrutSort(a);  
  21.         nonRecrutQuickSort(a);  
  22.         printArray(a);  
  23.         //partition(a, 0, 5);  
  24.     }  
  25.     public void quickSort(int[] a,int start,int end){//遞歸快排  
  26.         if(start<end){  
  27.             quickSort(a,start,partition(a,start,end)-1);  
  28.             quickSort(a,partition(a,start,end)+1,end);  
  29.         }  
  30.     }  
  31.     public void nonRecrutSort(int[] a){//非遞歸快排,兩個棧  
  32.         //設置兩個棧,一個用於保存  
  33.         if(a==null||a.length<0return;  
  34.         Stack<Integer> startStack=new Stack<Integer>();//保存當前劃分的最高位  
  35.         Stack<Integer> endStack=new Stack<Integer>();//保存當前劃分的最低位  
  36.         int start=0;  
  37.         int end=a.length-1;  
  38.               
  39.         int pivotPos;  
  40.           
  41.         startStack.push(start);  
  42.         endStack.push(end);  
  43.           
  44.         while(!startStack.isEmpty()){  
  45.             start=startStack.pop();  
  46.             end=endStack.pop();  
  47.             pivotPos=partition(a, start, end);  
  48.             if(start<pivotPos-1){  
  49.                 startStack.push(start);  
  50.                 endStack.push(pivotPos-1);  
  51.             }  
  52.             if(end>pivotPos+1){  
  53.                 startStack.push(pivotPos+1);  
  54.                 endStack.push(end);  
  55.             }  
  56.         }  
  57.     }  
  58.   
  59.     public void nonRecrutQuickSort(int a[]){  
  60.         if(a==null||a.length<=0)return;  
  61.         Stack<Integer> index=new Stack<Integer>();  
  62.         int start=0;  
  63.         int end=a.length-1;  
  64.           
  65.         int pivotPos;  
  66.               
  67.         index.push(start);  
  68.         index.push(end);  
  69.               
  70.         while(!index.isEmpty()){  
  71.             end=index.pop();  
  72.             start=index.pop();  
  73.               
  74.             pivotPos=partition(a,start,end);  
  75.             if(start<pivotPos-1){  
  76.                 index.push(start);  
  77.                 index.push(pivotPos-1);  
  78.             }  
  79.             if(end>pivotPos+1){  
  80.                 index.push(pivotPos+1);  
  81.                 index.push(end);  
  82.             }  
  83.         }     
  84.     }  
  85.       
  86.     public int partition(int[] a,int start,int end){//分塊方法,在數組a中,對下標從start到end的數列進行劃分  
  87.         int pivot=a[start];                     //把比pivot(初始的pivot=a[start]小的數移動到pivot的左邊  
  88.         while(start<end){                       //把比pivot大的數移動到pivot的右邊  
  89.             while(start<end&&a[end]>=pivot) end--;  
  90.             a[start]=a[end];  
  91.             while(start<end&&a[start]<=pivot) start++;  
  92.             a[end]=a[start];              
  93.         }  
  94.         a[start]=pivot;  
  95.         return start;//返回劃分後的pivot的位置  
  96.         //printArray(a);  
  97.     }  
  98.       
  99.     public void printArray(int a[]){//打印數組內容的方法,用於測試  
  100.         for(int i=0;i<a.length;i++){  
  101.             System.out.print(a[i]+" ");  
  102.         }  
  103.         System.out.println();  
  104.     }  
  105.   


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章