quickSort

 1 package POJ;
 2 
 3 public class Main {
 4 
 5     /**
 6      * 
 7      * QuickSort
 8      * 
 9      */
10     public static void main(String[] args) {
11         Main so = new Main();
12         int[] list = { 6, 4, 2, 3, 1, 5, 10, 4, 9, 8, 11, 7 };    
13         int[] result=so.quickSort(list, 0, 11);
14         for(int i:result)
15             System.out.println(i);
16     }    
17     public int[] quickSort(int[] list,int left,int right){
18         int index=partition(list,left,right);
19         if(left<index-1){
20             quickSort(list, left, index-1);
21         }
22         if(index<right){
23             quickSort(list, index, right);
24         }
25         return list;
26     }
27     private int partition(int[] list, int left, int right) {
28         // TODO Auto-generated method stub
29         int pivot=list[(left+right)/2];
30         while(left<=right){
31             while(list[left]<pivot)
32                 left++;
33             while(list[right]>pivot)
34                 right--;
35             if(left<=right){
36                 swap(list,left,right);
37                 left++;
38                 right--;
39             }
40         }
41         return left;
42     }
43     private void swap(int[] list, int left, int right) {
44         // TODO Auto-generated method stub
45         int temp=list[left];
46         list[left]=list[right];
47         list[right]=temp;
48     }
49 }

 

posted @ 2014-08-19 13:38  Phoebe815  阅读(186)  评论(0编辑  收藏  举报