几种排序算法之快速排序
看了很多教程,全都是垃圾,骗子!!!点开一看几十万的浏览量,结果代码和思路全是错的,错的你说nsnmn?
快速排序:
https://blog.51cto.com/13733462/2113397这个人写的还可以,就看这个就行了,别的大多是是假的。
原理:
(1) 我们从待排序的记录序列中选取一个记录(通常第一个)作为基准元素(称为key)key=arr[left],然后设置两个变量,left指向数列的最左部,right指向数据的最右部。
(2) key首先与arr[right]进行比较,如果arr[right]<key,则arr[left]=arr[right]将这个比key小的数放到左边去,如果arr[right]>key则我们只需要将right--,right--之后,再拿arr[right]与key进行比较,直到arr[right]<key交换元素为止。
(3) 如果右边存在arr[right]<key的情况,将arr[left]=arr[right],接下来,将转向left端,拿arr[left ]与key进行比较,如果arr[left]>key,则将arr[right]=arr[left],如果arr[left]<key,则只需要将left++,然后再进行arr[left]与key的比较。
(4) 然后再移动right重复上述步骤
(5) 最后得到 {23 58 13 10 57 62} 65 {106 78 95 85},再对左子数列与右子数列进行同样的操作。最终得到一个有序的数列。
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 }