www

导航

QuickSort(Java)

 1    private void quickSort(int[] input, int start, int end) {
 2         if (start >= end) return;
 3 
 4         int index = partition(input, start, end);
 5 
 6         if (index > start) {
 7             quickSort(input, start, index-1);
 8         }
 9 
10         if (index < end) {
11             quickSort(input, index+1, end);
12         }
13     }
14 
15     private int partition(int[] input, int start, int end) {
16         int index = start;
17         for (int i=start+1; i<=end; i++) {
18             if (input[i] < input[start]) {
19                 index++;
20                 if (index != i) {
21                     swap(input, i, index);
22                 }
23             }
24         }
25         swap(input, start, index);
26 
27         return index;
28     }
29 
30     private void swap(int[] input, int i, int j) {
31         int temp = input[i];
32         input[i] = input[j];
33         input[j] = temp;
34     }

 

posted on 2019-02-16 17:32  www_practice  阅读(162)  评论(0编辑  收藏  举报