반응형
#include <stdio.h>#define MAX_SIZE 100int heap[MAX_SIZE];int heapSize = 0;int heapPush(int value){if (heapSize > MAX_SIZE){printf("queue is full!");return 0;}heap[++heapSize] = value;int current = heapSize;while (current > 1 && heap[current] < heap[current / 2]){int temp = heap[current / 2];heap[current / 2] = heap[current];heap[current] = temp;current = current / 2;}return 1;}int heapPop(int *value){if (heapSize <= 0){return -1;}*value = heap[1];heap[1] = heap[heapSize];heapSize--;int current = 1;while (current * 2 <= heapSize){int child;if (current * 2 == heapSize){child = current * 2;}else{child = heap[current * 2] < heap[current * 2 + 1] ? current * 2 : current * 2 + 1;}if (heap[current] < heap[child]){break;}int temp = heap[current];heap[current] = heap[child];heap[child] = temp;current = child;}return 1;}
반응형
'ETC > Data Struct | Algorithm' 카테고리의 다른 글
Array 탐색 시 빠르게 연속된 1 찾기(?) (0) | 2022.01.11 |
---|---|
[Queue] Thread 간 통신을 위한 Async Queue (비동기 큐) (0) | 2018.08.08 |
Geometry (0) | 2017.07.17 |
[STL] algorithm 내부 sort 함수의 원형 (0) | 2015.04.01 |
Graph - 인접 행렬 그래프 (0) | 2014.06.25 |