堆排序算法 java实现 亲测有效,可以直接口服
package com.exceeddata.vcloud.util;
import com.sun.xml.bind.v2.model.annotation.Quick;
import java.util.HashMap;
/**
* @Author: Haojie.Z
* @Description:
* @Date:Created in 2022/11/23 14:27
* @Modified By:
*/
public class SortUtils {
public static void main(String[] args) {
Integer[] A = {0, 4, 3, 7, 5, 8};
// InsertSort(A, A.length);
// BubbleSort(A, A.length);
// QucikSort(A, 0, A.length - 1);
Integer[] B = {53, 17, 78, 9, 45, 65, 87, 32};
HeapSort(B, B.length);
// MergeSort(A,0,A.length);
// for (int i = 0; i < A.length; i++) {
// System.out.println(A[i]);
// }
}
//直接插入排序
public static void InsertSort(Integer[] A, int n) {
for (int i = 2; i < A.length; i++) {
if (A[i] < A[i - 1]) {
A[0] = A[i];
int j = i - 1;
for (; A[0] < A[j]; --j) {
A[j + 1] = A[j];
}
A[j + 1] = A[0];
}
}
}
//冒泡排序
public static void BubbleSort(Integer[] A, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = n - 1; j > i; j--) {
if (A[j - 1] > A[j]) { //交换
int temp = A[j];
A[j] = A[j - 1];
A[j - 1] = temp;
}
}
}
}
//快排
public static void QucikSort(Integer[] A, int low, int high) {
if (low < high) {
int pivotopos = Partition(A, low, high); //划分
QucikSort(A, low, pivotopos - 1); //依次对两个子表进行递归排序
QucikSort(A, pivotopos + 1, high);
}
}
private static int Partition(Integer[] a, int low, int high) { //一趟划分
Integer pivot = a[low]; //设置当前第一个元素为参照,对表进行划分
while (low < high) {
while (low < high && a[high] >= pivot)
--high;
a[low] = a[high]; //将比参考元素小的元素移动到左端
while (low < high && a[low] <= pivot)
++low;
a[high] = a[low]; //将比参考元素大的元素移动到右端
}
a[low] = pivot;
return low;
}
//建立大根堆
public static void BuildMapHeap(Integer[] a, int len) {
for (int i = len / 2; i > 0; i--) { //从i=[n/2]---1 反复调整堆
HeadAdjust(a, i, len);
}
}
public static void HeadAdjust(Integer[] a, int k, int len) {
//将元素k为根的子数进行调整
int temp = a[k - 1]; //暂存子树的根结点
for (int i = 2 * k; i <= len; i *= 2) { //沿key较大的子节点向下筛选
if (i < len && a[i - 1] < a[i])
i++; //取key较大的子节点的下标
if (temp >= a[i - 1])
break; //筛选结束
else {
a[k - 1] = a[i - 1]; //将a[i]调整到双亲结点上
k = i; //修改k值,以便继续向下筛选
}
}
a[k - 1] = temp; //被筛选结点的值放入最终位置
}
public static void HeapSort(Integer[] A, int len) {
BuildMapHeap(A, len); //初始建堆
for (int i = len; i > 1; i--) { //n-1趟的交换和建堆过程
int temp = A[i-1]; //输出堆顶元素(和堆底元素交换)
A[i-1] = A[0];
System.out.println(A[0]);
A[0] = temp;
HeadAdjust(A, 1, i - 1); //调整,把剩余的i-1个元素整理成堆
}
}
//递归排序
public static void MergeSort(Integer[] A, int low, int high) {
if (low < high) {
int mid = (low + high) / 2; //从中间划分两个子序列
MergeSort(A, low, mid);
MergeSort(A, mid + 1, high);
Merge(A, low, mid, high); //归并
}
}
public static void Merge(Integer[] A, int low, int mid, int high) {
//表A的两段A[low...high]和A[mid+1....high]各自有序,将它们合并为一个有序表
Integer[] B = new Integer[A.length];
int i = low;
int j = mid + 1, k = i;
for (int p = low; p <= high; p++)
B[p] = A[p]; //将A中所有元素复制到B中
for (; i <= mid && j <= high; k++) {
if (B[i] <= B[j]) //比较B的左右两段中的元素
A[k] = B[i++]; //将较小值复制到A中
else
A[k] = B[j++];
}
while (i <= mid)
A[k++] = B[i++]; //若第一个表未检测完,复制
while (j <= high)
A[k++] = B[j++]; //若第二个表未检测完,复制
}
}