QuickSort

 1 public class QuickSort {
 2       Random  rand =  new Random();
 3     public int[] quickSort(int[] array) {
 4         if (array == null || array.length = 0) {
 5             return array;
 6         }
 7         quickSort(int[] array,0, array.length -1)
 8             return array;
 9     }
10     private void quickSort(int[] array, int left, int right) {
11         if (left >= right){
12             return;
13         }
14         int pivot = partition(array, left, right);
15         quickSort(array, left, pivot - 1);
16         quickSort(array, pivot + 1, right);
17     }
18     private void partition(int[] array, int left, int right){
19         int pivotIndex = left + rand.nextInt(right - left)/2;
20         int pivot = array[pivotIndex];
21         swap(array, pivot, pivotIndex);
22         int leftIndex = left;
23         int rightIndex = right -1;
24         while (leftIndex >= rightIndex){
25                        if (array[leftIndex] < pivot){
26                 leftIndex++;
27                        } else if (array[rightIndex] > pivot){
28                              rightIndex--;
29                        } else {
30                           swap(array, leftIndex, rightIndex);
31             }
32         }
33                   sawap(array, leftIndex, pivot);
34                return leftIndex;
35         }
36 
37     private void swap(int[] array, int left, int right){
38             int temp = array[left];
39             array[left] = array[right];
40             array[right] = temp;
41     }
42 }
43 
44        

 

posted @ 2018-04-03 07:57  小粉要努力呀  阅读(75)  评论(0编辑  收藏  举报