-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.cpp
More file actions
67 lines (61 loc) · 994 Bytes
/
HeapSort.cpp
File metadata and controls
67 lines (61 loc) · 994 Bytes
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
#include<bits/stdc++.h>
using namespace std;
void HeapSort(int*,int);
void MaxHeapify(int*, int, int);
void BuildHeap(int a[], int n)
{
for(int i=n/2;i>=0;i--)
{
MaxHeapify(a,i,n);
}
}
void MaxHeapify(int a[],int i, int n)
{
int l=2*i+1, r=2*i+2, max=i;
if(l<n && a[l]>a[max])
max=l;
if(r<n && a[r]>a[max])
max=r;
if(max!=i)
{
int temp=a[i];
a[i]=a[max];
a[max]=temp;
MaxHeapify(a,max,n);
}
}
void HeapSort(int a[], int n)
{
for(int i=n;i>=0;i--)
{
int temp=a[i];
a[i]=a[0];
a[0]=temp;
MaxHeapify(a,0,i);
}
}
int main()
{
cout<<"Enter no. of elements:"<<endl;
int n,i;
cin>>n;
int a[n];
cout<<"Enter elements:\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\nInitial Array : [";
for(i=0;i<n-1;i++)
{
cout<<a[i]<<",";
}
cout<<a[n-1]<<"]"<<endl<<"Sorted Array : [";
BuildHeap(a,n-1);
HeapSort(a,n-1);
for(i=0;i<n-1;i++)
{
cout<<a[i]<<",";
}
cout<<a[n-1]<<"]"<<endl;
}