/*======================================================================\ * Author (作者): i.sshe * Date (日期): 2016/03/27 * Others (其他): \*=======================================================================*/ int max_heap_up_sort(int *heap_array, int len) { int *temp_array = NULL; int i = 0; //先生成最小堆 create_max_heap(heap_array, len); //注意: 这里也要分配多一个单元的内存. temp_array = (int *)malloc((len + 1)* sizeof(int) ); if (temp_array == NULL) { printf("排序失败: 临时数组分配失败!\n"); return 0; } for (i = 1; i < len + 1; i++) { temp_array[i] = heap_array[i]; } //下面排序 //输出根->把最后一个放到根->堆减1->调整, 一直循环. max_delete_root_and_sort(temp_array, len); return 1; }
void heap_sort(int *arr, int size) { struct Heap *max_heap; max_heap = create_max_heap(arr, size); while (max_heap->size > 1) { swap(&max_heap->array[0], &max_heap->array[max_heap->size - 1]); max_heap->size--; max_heapify(max_heap, 0); } }