본문 바로가기
공부/개발노트

업힙(UpHeap) 알고리즘 정의하기

반응형

소스 코드

#include <stdio.h>
void UpHeap(int * heap, int index){
int parent = index/2;
int temp, bigger;
if((parent>=1)&&(heap[index]>heap[parent]))
bigger = index;
else
bigger = parent;
if(bigger != parent){
temp = heap[parent];
heap[parent] = heap[index];
heap[index] = temp;
UpHeap(heap, parent);
}
} // end of UpHeap
void BuildHeap(int* heap, int heapSize){
for(int i = 2; i<=heapSize; i++)
UpHeap(heap, i);
} // end of buildHeep
int main(){
int node[10] = {
0, 40, 80, 60, 50, 30, 70, 10, 20, 90
};
BuildHeap(node,9);
for(int i=0; i<10; i++)
printf("%d\n", node[i]);
return 0;
}
view raw UpHeap.cpp hosted with ❤ by GitHub

결과

반응형