-
Notifications
You must be signed in to change notification settings - Fork 34
/
heap_sorting.cpp
95 lines (77 loc) · 1.51 KB
/
heap_sorting.cpp
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
#include<iostream>
using namespace std;
class heap{
public:
int arr[100];
int size = 0;
void swap(int *a,int *b){
int temp =*a;
*a = *b;
*b = temp;
}
void insert( int val) {
size = size +1;
int index = size; //// 50 55 --> 55 50 52
arr[index] = val;
while(index>1){
int parent = index/2;
if(arr[parent] < arr[index] ){
swap(&arr[parent],&arr[index]);
index = parent;
}
else{
return;
}
}
}
void print(){
for(int i = 1;i<=size ;i++){
cout<<arr[i]<<" ";
}cout<<endl;
}
};
void heapify(int arr[], int n , int i){
int largest = i;
int left = 2*i;
int right = 2*i +1;
if(arr[largest] < arr[left] && left <= n){
largest = left;
}
if(arr[largest] < arr[right] && right <= n ){
largest = right;
}
if(largest != i){
swap(arr[i],arr[largest]);
heapify(arr,n,largest);
}
}
void heapSort(int arr[], int n){
int size = n;
while(size >1){
swap(arr[size], arr[1] );
size--;
heapify(arr,size,1);
}
}
void print(int arr[],int size){
for(int i = 1;i <= size ;i++){
cout<<arr[i]<<" ";
}cout<<endl;
}
int main(){
heap h;
h.insert(55);
h.insert(50);
h.insert(41);
h.insert(66);
h.insert(52);
int arr[6] ={-1,45,70,33,55,63};
int n = 5;
for(int i = n/2 ; i >0 ;i--){
heapify(arr,n,i);
}
print(arr,5);
heapSort(arr, 5);
print(arr,5);
return 0;
}