快速排序

 1 public class QuickSort {
 2 
 3     static void sort(int a[],int start,int end) {
 4         
 5         int temp = a[start];
 6         int i = start;
 7         int j = end;
 8         
 9         while(i<j) {
10             while(i<j&&a[j]>=temp) {
11                 j--;
12             }
13             if(i<j) {
14                 a[i]=a[j];
15             }
16             while(i<j&&a[i]<temp) {
17                 i++;
18             }
19             if(i<j) {
20                 a[j]=a[i];
21             }
22         }
23         
24         a[i] = temp;
25         
26         if(i-start>1)
27             QuickSort.sort(a,start,i-1);
28         if(end-j>1)
29             QuickSort.sort(a,j+1,end);
30     
31         
32     }
33     
34     /**
35      * @param args
36      */
37     public static void main(String[] args) {
38         // TODO Auto-generated method stub
39         int a[]= {1,5,2,99,545,5,7};
40         for(int i = 0;i<a.length;i++)
41         System.out.print(a[i]+" ");
42         System.out.println();
43         sort(a,0,a.length-1);
44         for(int i = 0;i<a.length;i++)
45         System.out.print(a[i] + " ");
46         System.out.println();
47 
48     }
49 
50 }
原文地址:https://www.cnblogs.com/flying607/p/3442888.html