#文章没有解释和代码注释,代码经改进,做成了好理解,关键是好记忆的方式进行书写。用于自己进行查阅
#include <stdio.h>void swap(int arr[],int i,int j)
{int temp = arr[i];arr[i] = arr[j];arr[j] = temp;
}void heapify(int arr[],int i,int size)
{int left = 2 * i + 1;int right = 2 * i + 2;int max = i;if(left < size && arr[left] > arr[max])max = left;if(right < size && arr[right] > arr[max])max = right;if(max != i){swap(arr,0,--size);heapify(arr,0,size);}
}void heapsort(int arr[],int size)
{int i = size;for(; i >= 0; i--)heapify(arr,i,size);while(size > 1){swap(arr,0,--size);heapify(arr,0,size);}
}int main()
{int arr[] = {1,2,3,4,5,6,7,8,9,0};int size = sizeof(arr)/sizeof(arr[0]);heapsort(arr,size);int i = 0;for(; i < size; i++)printf("%d ",arr[i]);return 0;
}
#看不懂,过来找我,当面解释,三两句就说完了