voidswap(std::vector<int> &heap, int a, int b){
int temp = heap[a];
heap[a] = heap[b];
heap[b] = temp;
}
voidheapify(std::vector<int> &heap, int n, int i){
// 부모, 왼쪽, 오른쪽 순으로 저장int parent = i, l_c = i * 2 + 1, r_c = i * 2 + 2;
// 왼쪽 자식과 비교해서 인덱스 교환if (l_c < n && heap[parent] < heap[l_c])
parent = l_c;
// 오른쪽 자식과 비교해서 인덱스 교환if (r_c < n && heap[parent] < heap[r_c])
parent = r_c;
// 무언가 변환이 되었다면 교환후 재귀과정if (i != parent)
{
swap(heap, parent, i);
heapify(heap, n, parent);
}
}
voidheap_sort(std::vector<int> &unsorted_array){
int n = unsorted_array.size();
// 트리에 넣기.for (int i = n / 2 - 1; i <= 0; --i)
heapify(unsorted_array, n, i);
// 최댓값을 뒤로 빼는 작업for (int i = n - 1; i > 0; --i)
{
swap(unsorted_array, 0, i);
heapify(unsorted_array, i, 0);
}
}