forked from CodersForLife/Data-Structures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapsort.cpp
More file actions
131 lines (111 loc) · 3.33 KB
/
Copy pathHeapsort.cpp
File metadata and controls
131 lines (111 loc) · 3.33 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <cstdio>
#include <vector>
#include <iostream>
#include <climits>
using namespace std; //Not the best tactic, used for cleaner code and better understanding
class Heap {
private:
std::vector<int> heap;
int hpsz;
public:
Heap(){
hpsz=0; //At first the Heap is Empty
heap.push_back(0); //Index 0 must be initialized because we start indexing from 1
}
Heap(std::vector<int>& array){
int n = array.size();
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
heap.insert(heap.end(),array.begin(),array.end()); //προσθέτω το 0
hpsz=n; //Heap size = Heap Array size
for (int i = n / 2; i > 0; i--) combine(i);
}
void insert(int elmnt) {
heap.push_back(elmnt);
hpsz++;
int i = hpsz, parent = i / 2;
while ((i > 1) && (heap[parent] < heap[i]))
{
iter_swap(heap.begin() + parent, heap.begin() + i);
i = parent; parent = i / 2;
}
}
//Get max without deleting from heap
int peek_max(){
return heap[1];
}
void combine (int i){
int mp=i,left=2*i,right=(2*i)+1;
if((left<=hpsz) && (heap[left]>heap[mp])) {
mp = left;
}
if((right<=hpsz) && (heap[right]>heap[mp])) {
mp = right;
}
if(mp!=i) {
iter_swap(heap.begin() + i, heap.begin() + mp);
combine(mp);
}
}
int deletemax(){
if(isEmpty()) return -1;
int max = heap[1];
heap[1] = heap[hpsz--];
combine(1);
return max;
}
bool isEmpty(){
return (hpsz==0);
}
int getHeapSize(){
return (hpsz);
}
void heapSort(){
int temp = hpsz;
for (int i = hpsz; i > 1; i--) {
iter_swap(heap.begin() + 1, heap.begin() + i);
hpsz--;
combine(1);
}
hpsz = temp;
}
void printHeap(){
for (int i = 1; i <= hpsz; i++) {
printf("%d ", heap[i] );
}
printf("\n");
}
};
int main(){
/*
Heap can be constructed using vector as input
*/
int a[] = {3,4,10,8,15,16,17,12,11,20};
vector<int> heapin (a, a + sizeof(a) / sizeof(a[0]) ); //C++2003
//std::vector<int> heapin ({3,4,10,8,15,16,17,12,11,20}); //C++2011
// std::vector<int> heapin(std::begin(a), std::end(a)); //C++2003
Heap test(heapin);
/*
Heap can be constructed using one by one insertion of elements
<-------------------------------------------------------------->
Heap test;
int num_elements;
printf("Enter number of elements\n");
scanf("%d\n",&num_elements);
int elem;
while(num_elements!=0) {
num_elements--;
scanf("%dendl",&elem );
test.insert(elem);
}
*/
test.printHeap(); //Heap array
test.heapSort(); //after heap sorting the array does not include a list
test.printHeap(); //Sorted array
/*
* Use the following loop to extract all the elements from head of max heap
while (!test.isEmpty()) {
printf("Max after delete: %d\n", test.deletemax());
}
*/
}