-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap_sort.cpp
More file actions
executable file
·84 lines (78 loc) · 1.47 KB
/
heap_sort.cpp
File metadata and controls
executable file
·84 lines (78 loc) · 1.47 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
/*
* heap_sort.cpp
*
* Created on: Oct 15, 2014
* Author: Apoorva.D.A
*/
#include <cstdlib>
#include <iostream>
using namespace std;
class Heap
{
public:
void print_array(int *a, int n);
void max_heapify(int *a,int i,int n);
void build_heap(int *a,int n);
void heap_sort(int *a, int n);
};
void Heap::print_array(int *a, int n){
int i;
cout << "The elements of the array are :" << endl;
for(i = 0; i < n; i++){
cout << a[i] << "->";
}
cout << "\n\n" ;
}
void Heap::max_heapify(int *a,int i,int n){
int l,r,largest,tmp;
l = (2 * i);
r = (2 * i) + 1;
if (l<=n && a[l-1]>a[i-1]){
largest = l;
} else {
largest = i;
}
if (r<=n && a[r-1]>a[largest-1]){
largest = r;
}
if (largest != i){
tmp = a[i-1];
a[i-1] = a[largest-1];
a[largest-1]=tmp;
} else {
return;
}
max_heapify(a,largest,n);
}
void Heap::build_heap (int *a, int n){
int i;
cout << "Building the heap" << endl;
for(i = n/2 ; i >= 1 ; i--){
max_heapify(a,i,n);
}
}
void Heap::heap_sort ( int *a, int n){
int i, tmp;
cout << "Performing Heap sort" << endl;
for ( i = n-1; i >= 1; i--){
tmp = a[0];
a[0] = a[i];
a[i] = tmp;
max_heapify(a,1,i);
}
}
int main (){
Heap h;
int i;
int n=12;
int a[12];
cout << "Enter the elemnts of the array\n" << endl;
for (i=0; i<n; i++){
cin>>a[i];
}
h.print_array(a,n);
h.build_heap(a,n);
h.print_array(a,n);
h.heap_sort(a,n);
h.print_array(a,n);
}