代码heapsort.c
#include "heapsort.h"
void buildHeap(int *src,unsigned int length){int k,i,j;
for(k = length - 1;k>0;k--){
for(i = k;i>(k/2);i--){
for(j = i;j>=1;j = (j - 1)/2){
if(*(src+j)>*(src + (j-1)/2)){
int temp = *(src+j);
*(src + j) = *(src + (j-1)/2);
*(src + (j-1)/2) = temp;
}
}
}
int r = *src;
*src = *(src + k);
*(src +k) = r;
}
}
int main(){
int arr[] = {23,1,5,67,88,15,72,33,90,42};
buildHeap(arr,10);
int i = 0;
for(;i<10;i++){
printf("%d\t",arr[i]);
}
return -1;
}
代码 heapsort.h
#ifndef _HEAPSORT_H_
#define _HEAPSORT_H_
#include <stdio.h>
void buildHeap(int *src,unsigned int length);
#endif