quickSort in-place version whose space complexity is O(log(N))

 1 public void quickSortSwapping(int data[]){
 2 //call this method
 3 quickSortSwapping(data,0,data.length);
 4 }
 5 
 6 
 7 public void quickSortSwapping(int data[],int start,int len){
 8 if(len<2)return;
 9 int pivotIndex=start+len/2;
10 int pivotValue=data[pivotIndex];
11 int end=data.length;
12 int cur=start;
13 
14 //swapping the pivot to the end of array,for the testcase [a,a,a,a,a,a](PIE p130)
15 
16 swap(data,pivotIndex,--end);
17 
18 //partiton the rest of array
19 while(cur<end){
20 
21 if(data[cur]<pivotVaule){
22 
23 cur++;
24 
25 }else{
26 
27 swap(data,cur,--end);
28 
29 }
30 
31 //put pivot back to the original place
32 
33 swap(data,end,start+len-1);
34 
35 //using recursive method to partition ench part
36 
37 int llen=end-start;
38 int rlen=len-llen-1;
39 
40 if(llen>1){
41 
42 quickSortSwapping(data,start,llen);
43 }
44 
45 if(rlen>1){
46 
47 quickSortSwapping(data,end+1,rlen);
48 }
49 }
50 
51 
52 }

 

posted @ 2014-10-24 11:12  TonyChiang  阅读(162)  评论(0)    收藏  举报