forked from dhirajfx3/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap_sort.cpp
More file actions
78 lines (68 loc) · 1.68 KB
/
Copy pathheap_sort.cpp
File metadata and controls
78 lines (68 loc) · 1.68 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
#include<bits/stdc++.h>
using namespace std;
void max_heapify(std::vector<int> &v, int index, int size){
// Given an index and current heapsize. This function assumes that both left
// and right subtrees for the given index follow max-heap property. It then
// moves the given index value to appropriate place.
int left_child = 2*index + 1;
int right_child = 2*index +2;
int heap_size = size;
int largest = index;
if (left_child < heap_size && v[left_child] > v[index])
{
largest = left_child;
}
if (right_child < heap_size && v[right_child] > v[largest])
{
largest = right_child;
}
if (largest != index)
{
int temp = v[largest];
v[largest] = v[index];
v[index] = temp;
max_heapify(v,largest,size);
}
}
void convert_into_max_heap(std::vector<int> &v){
// Converts a given array of elements into a array following max-heap property.
int heap_size = v.size();
for(int i = heap_size/2 - 1; i >= 0 ; --i) {
max_heapify(v,i,heap_size);
}
}
void heap_sort(std::vector<int> &v){
// Sorts the elements. Keeps larger elements towards the end of the array and
// keeps decrementing heap_size
convert_into_max_heap(v);
int heap_size = v.size();
while(heap_size)
{
int temp = v[heap_size-1];
v[heap_size-1] = v[0];
v[0] = temp;
heap_size--;
max_heapify(v,0,heap_size);
}
}
int main()
{
std::vector<int> v;
int n;
cout<<"Enter no. of elements:\t";
cin>>n;
cout<<"\nEnter elements:\t";
int input_count=0;
int temp_input;
while(input_count<n){
cin>>temp_input;
v.push_back(temp_input);
input_count++;
}
// sorting starts here.
heap_sort(v);
cout<<"\nSorted output: \t";
copy(v.begin(), v.end(),
ostream_iterator<int>(cout," "));
return 0;
}