forked from kabirthakkar/Hkfst2k21
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeapSort.cpp
59 lines (55 loc) · 1.04 KB
/
HeapSort.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
#include<iostream>
using namespace std;
int temp;
void heapify(int ar[] ,int size,int i)
{
int largest=i;
int left,right;
left=2*i+1;
right=2*i+2;
if (left<size && ar[left]>ar[largest])
largest=left;
if (right<size && ar[right]>ar[largest])
largest=right;
if (largest!=i)
{
temp=ar[i];
ar[i]=ar[largest];
ar[largest]=temp;
heapify(ar,size,largest);
}
}
void heapsort(int ar[],int size)
{
int i;
for (i = size/2-1; i >= 0; i--)
{
heapify(ar,size,i);
}
for ( i = size-1; i > 0; i--)
{
temp=ar[0];
ar[0]=ar[i];
ar[i]=temp;
heapify(ar,i,0);
}
}
int main()
{
int ar[10],n,i;
cout<<"Name: Shrey Midha \nRollNo.:1816110197\n";
cout<<"Enter the Number of Elements:";
cin>>n;
cout<<"\nEnter the Array Elements:\n";
for(i=0;i<n;i++)
{
cin>>ar[i];
}
heapsort(ar,n);
cout<<"Sorted Array:";
for (i = 0; i < n; i++)
{
cout<<ar[i]<<" ";
}
return 0;
}