晒太阳

java 快速排序

 1 
 2 public class QuickSort {
 3     public static int partition(int[] arr, int low, int high){
 4         int pivot = arr[low];
 5         while(low<high){
 6             while(low<high && arr[high]>=pivot){
 7                 high--;
 8             }
 9             if(low<high){
10                 arr[low] = arr[high];
11                 low++;
12             }
13             
14             while(low<high && arr[low]<=pivot){
15                 low++;
16             }
17             if(low<high){
18                 arr[high] = arr[low];
19                 high--;
20             }
21         }
22         arr[low] = pivot;
23         return low;
24     }
25     
26     public static void sort(int[] arr, int low, int high){
27         int pivot;
28         if(low<high){
29             pivot = partition(arr,low,high);
30             sort(arr,low,pivot-1);
31             sort(arr,pivot+1,high);
32         }
33     }
34     
35     public static void quickSort(int[] arr){
36         sort(arr,0,arr.length-1);
37     }
38     
39     /*public static void main(String[] args) {
40         int[] arr = {46,34,2,99,6,23,20,8};
41         quickSort(arr);
42         StringBuilder sb = new StringBuilder();
43         for(int i=0;i<arr.length;i++){
44             sb.append(arr[i]+",");
45         }
46         sb.deleteCharAt(sb.length()-1);
47         System.out.println(sb);
48     }*/    
49 }
50 

posted on 2012-11-01 12:06 晒太阳 阅读(94) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 
<2012年11月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

导航

统计

常用链接

留言簿

随笔档案

文章档案

收藏夹

搜索

最新评论

阅读排行榜

评论排行榜