排序之快速排序(quickSort)

好比說一個數組java

6,1,2,7,9,4,5,10,8
int a[]={6,1,2,7,9,4,5,10,8};

 快排有三個要素:基準flag、左哨兵i、右哨兵數組

爲了方便通常以數組的第一位做爲flag基準ide

記住,每次動都是右哨兵J先動,J從8開始探測,尋找比基準6小的數,J--ui

I從6開始探測尋找比基準6大的數,i++,交換spa

交換完爲止,仍是從右哨兵J開始探測,尋找比基準6小的數3d

左哨兵i++探測比基準大的數,交換指針

交換完爲止,仍是從右哨兵J開始探測,尋找比基準6小的數,找到了3code

左哨兵i++探測比基準大的數blog

兩個哨兵相遇了,就不能繼續走了排序

而後此時將該位置的3和基準交換

這時候,原序列就被分爲兩個序列了,左序列爲3 1 2 5 4右序列爲9 7 10 8

接下來就須要處理這兩個序列,首先都是處理左序列3 1 2 5 4

基準爲3 繼續上邊的操做 調整事後2  1  3  5  4

左序列爲2 1 調整事後爲 1 2 3 5 4 6 9 7 10 8

右序列調整 1 2 3 4 5 9 7 10 8

接下來處理右序列 9 7 10 8 一樣的方法

 

代碼以下

 1 public class QuickSort {  
 2       
 3     /** 
 4      * 將數組的某一段元素進行劃分,小的在左邊,大的在右邊 
 5      * @param a 
 6      * @param start 
 7      * @param end 
 8      * @return 
 9      */  
10     public static int divide(int[] a, int start, int end){  
11         //每次都以最右邊的元素做爲基準值  
12         int base = a[end];  
13         //start一旦等於end,就說明左右兩個指針合併到了同一位置,能夠結束此輪循環。  
14         while(start < end){  
15             while(start < end && a[start] <= base)  
16                 //從左邊開始遍歷,若是比基準值小,就繼續向右走  
17                 start++;  
18             //上面的while循環結束時,就說明當前的a[start]的值比基準值大,應與基準值進行交換  
19             if(start < end){  
20                 //交換  
21                 int temp = a[start];  
22                 a[start] = a[end];  
23                 a[end] = temp;  
24                 //交換後,此時的那個被調換的值也同時調到了正確的位置(基準值右邊),所以右邊也要同時向前移動一位  
25                 end--;  
26             }     
27             while(start < end && a[end] >= base)  
28                 //從右邊開始遍歷,若是比基準值大,就繼續向左走  
29                 end--;  
30             //上面的while循環結束時,就說明當前的a[end]的值比基準值小,應與基準值進行交換  
31             if(start < end){  
32                 //交換  
33                 int temp = a[start];  
34                 a[start] = a[end];  
35                 a[end] = temp;  
36                 //交換後,此時的那個被調換的值也同時調到了正確的位置(基準值左邊),所以左邊也要同時向後移動一位  
37                 start++;  
38             }     
39               
40         }  
41         //這裏返回start或者end皆可,此時的start和end都爲基準值所在的位置  
42         return end;  
43     }  
44   
45     /** 
46      * 排序 
47      * @param a 
48      * @param start 
49      * @param end 
50      */  
51     public static void sort(int[] a, int start, int end){  
52         if(start > end){  
53             //若是隻有一個元素,就不用再排下去了  
54             return;  
55         }   
56         else{  
57             //若是不止一個元素,繼續劃分兩邊遞歸排序下去  
58             int partition = divide(a, start, end);  
59             sort(a, start, partition-1);  
60             sort(a, partition+1, end);  
61         }  
62               
63     }  
64     public static void main(String[] args) {  
65         
66         int[] b = new int[]{2,7,4,5,10,1,9,3,8,6};  
67         int[] c = new int[]{1,2,3,4,5,6,7,8,9,10};  
68         int[] d = new int[]{10,9,8,7,6,5,4,3,2,1};  
69         int[] a = new int[]{1,10,2,9,3,2,4,7,5,6};  
70               
71         sort(a, 0, a.length-1);  
72               
73         System.out.println("排序後的結果:");  
74         for(int x : a){  
75             System.out.print(x+" ");  
76         }  
77     }  
78 
79 } 
相關文章
相關標籤/搜索