直接代码附上,如果需要思路,推荐视频:https://www.bilibili.com/video/av47196993?from=search&seid=11104628614194650826
import java.util.Arrays;
public class HeadSort {
public static void main(String[] args) {
int[] tree ={2,5,3,1,10,4};
HeadSort headSort = new HeadSort();
headSort.head_sort(tree,tree.length);
System.out.println(Arrays.toString(tree));
}
//编写交换
public void swap(int[] arr,int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/**
* 构建堆,子树已经是堆
* @param tree 代表树的一个数组
* @param n 代表多少个节点【数】
* @param i 从什么地方开始构建
*/
public void headify(int[] tree,int n,int i){
if(i >= n){
return;
}
int left = 2*i+1;
int right = 2*i+2;
int max = i;
if(left < n && tree[left] > tree[max]){
max=left;
}
if(right < n && tree[right] > tree[max]){
max=right;
}
if(max!=i){
swap(tree,max,i);
headify(tree,n,max);
}
}
//构建一个完整的堆:从下往上
public void build_heap(int[] tree,int n){
int last_node = n-1;
int parent = (last_node-1)/2;
for(int i = parent;i >= 0;i--){
headify(tree,n,i);
}
}
//堆排序,将堆数组转换为堆排序数组
public void head_sort(int[] tree,int n){
build_heap(tree,n);
for (int i = n-1; i >= 0; i--) {
swap(tree,0,i); //将最大的节点与最后一个节点交换,以确保排序数组
headify(tree,i,0); //默认斩掉最后一个节点
}
}
}