快速排序

快排3步走

Step1: 选取Base基点

采用简单的方法取每次排列数组的第一个点,Base的选取同样有很多优化,可以到网上找些教程

同时申明变量 向后查找游标为i,向前查找节点坐标为j

Step2: 从后向前找小于Base的点

找到 ------->array[i] = array[j]

Step3: 从前向后找大于Base的点(查找游标为j)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package algorithm.innersort;

/**
* @author YellowRifle
* @projectName DataStructure
* @description: TODO
* @create 19-7-1上午9:30
*/

//采用递归实现
public class QuickSort {
public static void main(String[] args) {
int[] array = {1,3,1,3,4,6,7,89,2,432,523,421,9,6};
order(array,0,array.length-1);
for (int i : array) {
System.out.println(i);
}
}

public static void order(int[] array,int low,int high){
if (low > high){
return;
}
int i = low;
int j = high;
int base = array[low];
while (i < j) {
while(array[j] >= base && j > i){
j--;
}
if (i < j) {
array[i++] = array[j];
}
while (array[i] <= base && j > i) {
i++;
}
if (i < j) {
array[j--] = array[i];
}
array[i] = base;
order(array,low,i-1);
order(array,i+1,high);
}
}
}


感谢您的阅读,本文由 YellowRifle 版权所有。如若转载,请注明出处:YellowRifle(https://yellowrifle.github.io/2019/11/04/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F/
堆排序
归并排序