-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathout_Of_Place_Heap_Sort.cpp
More file actions
69 lines (63 loc) · 1.63 KB
/
Copy pathout_Of_Place_Heap_Sort.cpp
File metadata and controls
69 lines (63 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
using namespace std;
class priorityQueue{
int * A;
int size = 0;
int LEFT(int index){
return (2 * index + 1) ;
}
int RIGHT(int index){
return (2 * index + 2) ;
}
void heapifyDown(int index){
int smallest = index;
int left = LEFT(index);
int right = RIGHT(index);
if(left < size && A[smallest] > A[left]) smallest = left;
if(right < size && A[smallest] > A[right]) smallest = right;
if(smallest != index){
swap(A[index],A[smallest]);
heapifyDown(smallest);
}
}
public:
priorityQueue(int arr[],int size){
A = new int[size];
this->size = size;
for (int i = 0; i < size; ++i) A[i] = arr[i];
int index = (size - 2) / 2;
while(index >= 0) heapifyDown(index--);
}
~priorityQueue(){
delete [] A;
}
bool empty(){
return size == 0;
}
int pop(){
try{
if(size == 0) throw out_of_range("Heap Underflow");
int data = A[0];
A[0] = A[size-- - 1];
heapifyDown(0);
return data;
}
catch (const out_of_range & err){
cout<<"\n"<<err.what()<<"\n";
}
}
};
void heapSort(int arr[],int size){
priorityQueue pq (arr,size);
int index = 0;
while(!pq.empty()) arr[index++] = pq.pop();
}
int main(){
int arr[] = { 3, 8, 5, 4, 1,1, 9, -2 ,-1,-3,-10,-11,-12,-13};
int size = (*(&arr + 1) - arr);
heapSort(arr,size);
for (int k = 0; k < (*(&arr + 1) - arr); ++k) {
cout<<arr[k]<<" ";
}cout<<"\n";
return 0;
}