Skip to content

Commit b2140de

Browse files
committed
Add C++ Heap Sort using heap based on Vectors
1 parent 1ebf8b7 commit b2140de

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

Sorting/Heapsort.cpp

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
#include <vector>
4+
#include <iostream>
5+
#include <climits>
6+
7+
using namespace std; //Not the best tactic, used for cleaner code and better understanding
8+
9+
class Heap {
10+
private:
11+
std::vector<int> heap;
12+
int hpsz;
13+
public:
14+
15+
Heap(){
16+
hpsz=0; //At first the Heap is Empty
17+
heap.push_back(0); //Index 0 must be initialized because we start indexing from 1
18+
}
19+
20+
Heap(std::vector<int>& array){
21+
int n = array.size();
22+
heap.push_back(100); //The first index of heap array is 1 so we must fill 0 index with dummy content let it be 0
23+
heap.insert(heap.end(),array.begin(),array.end()); //προσθέτω το 0
24+
hpsz=n; //Heap size = Heap Array size
25+
for (int i = n / 2; i > 0; i--) combine(i);
26+
}
27+
28+
void insert(int elmnt) {
29+
heap.push_back(elmnt);
30+
hpsz++;
31+
int i = hpsz, parent = i / 2;
32+
while ((i > 1) && (heap[parent] < heap[i]))
33+
{
34+
iter_swap(heap.begin() + parent, heap.begin() + i);
35+
i = parent; parent = i / 2;
36+
}
37+
}
38+
39+
//Get max without deleting from heap
40+
int peek_max(){
41+
return heap[1];
42+
}
43+
44+
void combine (int i){
45+
int mp=i,left=2*i,right=(2*i)+1;
46+
if((left<=hpsz) && (heap[left]>heap[mp])) {
47+
mp = left;
48+
}
49+
if((right<=hpsz) && (heap[right]>heap[mp])) {
50+
mp = right;
51+
}
52+
if(mp!=i) {
53+
iter_swap(heap.begin() + i, heap.begin() + mp);
54+
combine(mp);
55+
}
56+
}
57+
58+
int deletemax(){
59+
if(isEmpty()) return -1;
60+
int max = heap[1];
61+
62+
heap[1] = heap[hpsz--];
63+
combine(1);
64+
return max;
65+
}
66+
67+
bool isEmpty(){
68+
return (hpsz==0);
69+
}
70+
71+
int getHeapSize(){
72+
return (hpsz);
73+
}
74+
75+
void heapSort(){
76+
int temp = hpsz;
77+
for (int i = hpsz; i > 1; i--) {
78+
iter_swap(heap.begin() + 1, heap.begin() + i);
79+
hpsz--;
80+
combine(1);
81+
}
82+
hpsz = temp;
83+
}
84+
85+
void printHeap(){
86+
for (int i = 1; i <= hpsz; i++) {
87+
printf("%d ", heap[i] );
88+
}
89+
printf("\n");
90+
}
91+
};
92+
93+
int main(){
94+
/*
95+
Heap can be constructed using vector as input
96+
*/
97+
int a[] = {3,4,10,8,15,16,17,12,11,20};
98+
vector<int> heapin (a, a + sizeof(a) / sizeof(a[0]) ); //C++2003
99+
//std::vector<int> heapin ({3,4,10,8,15,16,17,12,11,20}); //C++2011
100+
// std::vector<int> heapin(std::begin(a), std::end(a)); //C++2003
101+
Heap test(heapin);
102+
/*
103+
Heap can be constructed using one by one insertion of elements
104+
<-------------------------------------------------------------->
105+
Heap test;
106+
int num_elements;
107+
printf("Enter number of elements\n");
108+
scanf("%d\n",&num_elements);
109+
int elem;
110+
111+
while(num_elements!=0) {
112+
num_elements--;
113+
scanf("%dendl",&elem );
114+
test.insert(elem);
115+
}
116+
*/
117+
118+
test.printHeap(); //Heap array
119+
test.heapSort(); //after heap sorting the array does not include a list
120+
test.printHeap(); //Sorted array
121+
122+
/*
123+
* Use the following loop to extract all the elements from head of max heap
124+
while (!test.isEmpty()) {
125+
printf("Max after delete: %d\n", test.deletemax());
126+
}
127+
*/
128+
129+
130+
131+
}

0 commit comments

Comments
 (0)