固定分片快速排序

 1 public class QuickSort {
 2     public static void sort(int[] a, int lo, int hi) {
 3         int start = lo;
 4         int end   = hi;
 5         int key   = a[lo];
 6         
 7         while(start < end) {
 8             while(start < end && a[end] >= key) {
 9                 end--;
10             }
11                 
12             if(a[end] <= key) {
13                 int temp = a[end];
14                 a[end] = a[start];
15                 a[start] = temp;
16             }
17             
18             while(start < end && a[start] <= key) {
19                 start++;
20             }
21             
22             if(a[start] >= key) {
23                 int temp = a[start];
24                 a[start] = a[end];
25                 a[end] = temp;
26             }
27         }
28         
29         if(lo < start) { sort(a, lo, start - 1); }
30         if(end < hi) { sort(a, end + 1, hi); }
31     }
32 }
View Code

 

posted @ 2017-03-15 00:05  yixiu868  阅读(173)  评论(0编辑  收藏  举报