标签:sizeof printf std 序列 color tmp pad adjust style
#include <stdio.h> void swap(int *x, int *y) { int tmp = *x; *x = *y; *y = tmp; } // 将index为根的树调整成对大堆 void HeapAdjust(int *array, int index, int length) { int nChild; int lChild; int rChild; for (; 2 * index + 1 < length; index = nChild) { nChild = lChild = 2 * index + 1, rChild = lChild + 1; if (lChild < length - 1 && array[rChild] > array[nChild]) nChild = rChild; if (array[index] < array[nChild]) { swap( &array[index], &array[nChild]); } else { break; } } } // 堆排序 void HeapSort(int *array, int length) { int i; //从最后一个非叶节点开始调整,将整个数组调整成最大堆 for (i = length/2 - 1; i >= 0; i--) { HeapAdjust(array, i, length); } //从最后一个元素开始对序列进行调整,不断的缩小调整的范围直到第一个元素 for (i = length - 1; i > 0; i--) { swap(&array[0], &array[i]); HeapAdjust(array, 0, i); } } int main() { int i; int num[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; HeapSort(num, sizeof(num)/sizeof(int)); for (i=0; i < sizeof(num)/sizeof(int); i++) { printf("%d ",num[i]); } return 0; }
标签:sizeof printf std 序列 color tmp pad adjust style
原文地址:http://www.cnblogs.com/aceg/p/6819171.html