-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathheap_sort.cpp
78 lines (69 loc) · 2.75 KB
/
heap_sort.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
#include<iostream>
using namespace std;
void adjust(int a[] , int n ,int i){
while(2*i+1<=n){
int j = 2*i+1;
if(j+1<=n && a[j+1]>a[j])
j = j+1;
if(a[i] >= a[j])
break;
else{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
i = j;
}
}
}
void heapsort(int a[] , int n){
for(int i = n/2 - 1;i>=0 ;i--){ //using this for loop for converting it into max heap
adjust(a,n-1,i);
}
cout<<"the max heap is "<<endl;
for(int i = 0;i<n;i++){ //printing the max heap elements
cout<<a[i]<<" ";
}
cout<<endl;
while(n>0){ //using this while for given no of elements
int t = 0;
t = a[0]; //first swapping of first and last element and then again converting to heap
a[0] = a[n-1];
a[n-1]=t;
n--;
adjust(a,n-1,0);
}
}
int main(){
int n;
cin>>n;
int a[n];
cout<<"Enter the elements in heap"<<endl; //accepting the elements in heap
for(int i=0;i<n;i++){ //heap with index starting from 0
cin>>a[i];
}
heapsort(a,n); //calling heapsort to sort and convert it into max heap too
cout<<"Sorted array is using heapsort is"<<endl;
for(int i = 0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
/*
OUTPUT :-
10
Enter the elements in heap
13
1
2
3
4
4
5
6
7
8
the max heap is
13 8 5 7 4 4 2 6 3 1
Sorted array is using heapsort is
1 2 3 4 4 5 6 7 8 13
*/