::public/자료구조
우선순위 큐(STL 힙 정렬)
해맑은욱
2019. 9. 3. 17:14
#include <queue> #include <functional> // STL 우선순위 큐를 이용한 내림차순 정렬. 최대 힙 void heapSortDec(int arr[], int n) { priority_queue<int> maxHeap; for (int i = 0; i < n; i++) maxHeap.push(arr[i]); // maxHeap을 내림차순으로 정렬 for (int i = 0; i < n; i++) { arr[i] = maxHeap.top(); maxHeap.pop(); } } // STL 우선순위 큐를 이용한 오름차순 정렬. 최소 힙 void heapSortInc(int arr[], int n) { priority_queue<int, vector<int>, greater<int>> minHeap; for (int i = 0; i < n; i++) minHeap.push(arr[i]); // maxHeap을 오름차순으로 정렬 for (int i = 0; i < n; i++) { arr[i] = minHeap.top(); minHeap.pop(); } } | cs |