首页 > 其他 > 详细

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

时间:2014-10-24 12:48:13      阅读:254      评论:0      收藏:0      [点我收藏+]
 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 }

 

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

原文:http://www.cnblogs.com/tonychiang/p/4047813.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!