1 堆排序
1.1 堆的概念
- 堆逻辑上就是一颗完全二叉树, 物理上是保存在数组中;
- 大根堆: 满足任意结点的值都大于其子树中结点的值;
- 小根堆: 满足任意结点的值都小于其子树中结点的值.
- 堆的基本作用就是快速找到集合中的最值, 关于其逻辑结构和存储结构如下图所示(以小根堆为例):
1.2 操作过程–向下调整
向下调整的前提是左右子树必须已经是一个堆才能调整.我们以小堆为例, 如下所示.
- index 如果已经是叶子结点, 则调整过程结束;
– 判断 index 位置有没有孩子;
– 因为堆是完全二叉树, 因此没有左孩子就一定没有右孩子, 所以要进行判断有没有左孩子;
– 因为堆的存储结构是数组, 所以判断是否有左孩子即判断左孩子下标是否越界, 即 left >= size越界; - 确定 left 或 right, 谁是 index 的最小孩子 min;
– 如果右孩子不存在, 则 min = left;
– 如果右孩子存在, 比较 array[left] 和 array[right] 值的大小, 选择小的为 min; - 比较 array[index] 的值和 array[min] 的值, 如果 array[index] <= array[min], 则满足堆的性质, 调整结束;
- 如果 array[index] >= array[min], 交换 array[index] 和 array[min] 的值;
- 因为 min 位置的堆的性质可能被破坏, 所以把 min 视作 index, 如此重复以上过程.
其中: array 代表存储堆的数组; size 代表数组中被视为堆数据的个数; index 代表要调整位置的下标; left 代表 index 左孩子下标; right 代表 index 右孩子下标; min 代表 index 的最小值孩子的下标.
1.3 操作过程–建堆
关于堆的建立基本思想就是从倒数第一个非叶子节点的子树开始调整, 一直调整到根节点的树, 即为堆.这里我们以大根堆为例.
1.4 堆排序过程
堆排序的基本思想也是选择排序, 只是不再使用遍历的方式查找无序区间的最大值, 而是通过堆来选择无序区间的最大的数; 还需要注意: 升序排序要建大堆; 降序排序要建小堆.
详细过程如下:
代码如下:
public class SelectSort {
public static void createHeap(int[] arr) {
//从小到大排序 --> 大根堆
for(int i = (arr.length - 1 - 1)/2;i>=0;i--) {
siftDown(arr,i, arr.length);
}
}
public static void siftDown(int[] arr,int root,int len) {
int parent = root;
int child = 2*parent+1;
while (child < len) {
if(child+1 < len && arr[child] < arr[child+1]) {
child++;
}
//child的下标就是左右孩子的最大值下标
if(arr[child] > arr[parent]) {
int tmp = arr[child];
arr[child] = arr[parent];
arr[parent] = tmp;
parent = child;
child = 2*parent+1;
} else {
break;
}
}
}
public static void heapSort(int[] arr) {
createHeap(arr); //O(n)
int end = arr.length - 1;
while (end > 0) { //O(N*LogN)
int tmp = arr[end];
arr[end] = arr[0];
arr[0] = tmp;
siftDown(arr,0,end);
end--;
}
}
public static void main(String[] args) {
int[] arr = {3,2,1,5,8,7,6,4,9};
System.out.println(Arrays.toString(arr));
heapSort(arr);
System.out.println(Arrays.toString(arr));
}
}
运行结果:
性能分析:
2 直接选择排序
直接选择排序比较简单, 每一次排序只能确定一个数值, 因此这里不再进行描述其详细过程.
代码如下:
public class SelectSort {
public static void selectSort(int[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = i+1; j < array.length; j++) {
if (array[j] < array[i]) {
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
}
}
public static void main(String[] args) {
int[] arr = {3,2,5,7,1,6,8,9,4};
System.out.println(Arrays.toString(arr));
selectSort(arr);
System.out.println(Arrays.toString(arr));
}
}
运行结果:
性能分析: