Some sort of quick sort algorithm

Read a lot of tutorials, all rubbish, liar! ! ! Point to open a look at the hundreds of thousands of views, the resulting code and ideas are all wrong, wrong, you say nsnmn?

Quick Sort:

https://blog.51cto.com/13733462/2113397 this person may also write, to see this on the line, the other mostly false.

principle:

(1) we choose a sequence of records to be sorted from a recording (usually the first) as the reference element (referred to as key) key = arr [left], then set two variables, left point to the leftmost portion of the number of columns, right point to the rightmost data.

(2) comparing the first key arr [right], if arr [right] <key, the arr [left] = arr [right] is smaller than this number into the key to the left, if arr [right]> key then we only need to right -, after right--, shouting arr [right] is compared with the key, until arr [right] <key elements of the exchange so far.

(3) If the right exists arr [right] <key situation will arr [left] = arr [right], Subsequently, the left end of the turn, take ARR [left] compared with the key, if arr [left]> key, then arr [right] = arr [left], if arr [left] <key, you only need to left ++, and compare arr [left] and of the key.

(4) then moves right above steps are repeated

(5) to give the final {235813105762} 65 {106,789,585}, then the number of columns on the right and left of the number of sub-columns the same operation. Finally get an ordered sequence.

 

  1 package Sort;
  2 import java.util.Arrays;
  3 
  4 public class Quicksort {
  5     
  6     public static void quickSort(int [] arr,int left,int right) {
  7 
  8           int pivot=0;
  9 
 10           if(left<right) {
 11              pivot=partition(arr,left,right);
 12              quickSort(arr,left,pivot-1);
 13              quickSort(arr,pivot+1,right);
 14           }
 15        }
 16 
 17        private static int partition(int[] arr,int left,int right) {
 18           int key=arr[left];
 19           
 20           while(left<right) {
 21              while(left<right && arr[right]>=key) {
 22                 right--;
 23              }
 24              arr[left]=arr[right];
 25              
 26              while(left<right && arr[left]<=key) {
 27                 left++;
 28              }
 29              arr[right]=arr[left];
 30           }
 31           arr[left]=key;
 32           return left;
 33        }
 34        
 35     public static void comparator(int[] arr) {
 36         Arrays.sort(arr);//系统排序绝对正确的方法
 37     }
 38     
 39     public static int[] generateRandomArray(int maxSize, int maxValue) {//随机数发生器
 40         //Math.random 产生一个double [0,1)
 41         
 42         int[] arr = new int[(int) ((maxSize + 1) * Math.random())];//产生一个随机长度的数组
 43         for (int i = 0; i < arr.length; i++) {
 44             arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());//产生一个随机数,两个随机数减一下
 45         }
 46         return arr;
 47     }
 48     
 49     // for test
 50     public static int[] copyArray(int[] arr) {
 51         if (arr == null) {
 52             return null;
 53         }
 54         int[] res = new int[arr.length];
 55         for (int i = 0; i < arr.length; i++) {
 56             res[i] = arr[i];
 57         }
 58         return res;
 59     }
 60 
 61         // for test
 62     public static boolean isEqual(int[] arr1, int[] arr2) {
 63         if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
 64             return false;
 65         }
 66         if (arr1 == null && arr2 == null) {
 67             return true;
 68         }
 69         if (arr1.length != arr2.length) {
 70             return false;
 71         }
 72         for (int i = 0; i < arr1.length; i++) {
 73             if (arr1[i] != arr2[i]) {
 74                 return false;
 75             }
 76         }
 77         return true;
 78     }
 79     
 80     // for test
 81     public static void printArray(int[] arr) {
 82         if (arr == null) {
 83             return;
 84         }
 85         for (int i = 0; i < arr.length; i++) {
 86             System.out.print(arr[i] + " ");
 87         }
 88         System.out.println();
 89     }
 90     
 91 
 92     public static void main(String[] args) {
 93         
 94         int testTime = 500000;
 95         int maxSize = 10;
 96         int maxValue = 100;
 97         boolean succeed = true;
 98         for (int i = 0; i < testTime; i++) {
 99             int[] arr1 = generateRandomArray(maxSize, maxValue);
100             int[] arr2 = copyArray(arr1);
101             quickSort(arr1,0,arr1.length-1);
102             comparator(arr2);
103             if (!isEqual(arr1, arr2)) {
104                 succeed = false;
105                 break;
106             }
107         }
108         System.out.println(succeed ? "Nice!" : "Fucking fucked!");
109 
110         int[] arr = generateRandomArray(maxSize, maxValue);
111         printArray(arr);
112         quickSort(arr,0,arr.length-1);
113         printArray(arr);
114         
115     }
116 
117 }

 

Guess you like

Origin www.cnblogs.com/wangyufeiaichiyu/p/10944022.html