-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtw4Heap.cpp
More file actions
78 lines (66 loc) · 1.18 KB
/
tw4Heap.cpp
File metadata and controls
78 lines (66 loc) · 1.18 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
#include<bits/stdc++.h>
using namespace std;
void Swap(int *a , int *b)
{
int t = *a ;
*a = *b;
*b = t;
}
void Heapify(int A[] , int n , int i)
{
int largest = i;
int l = 2*i;
int r = 2*i+1;
if(l<=n && A[largest] <= A[l])
{
largest = l;
}
if(r<=n && A[largest] <= A[r])
{
largest = r;
}
if(largest != i)
{
Swap(&A[largest] , &A[i]);
Heapify(A , n , largest);
}
}
void HeapSort(int A[] , int n )
{
for(int i=n/2;i>=1;i--)
{
Heapify(A , n , i);
}
for(int i=n;i>=1;i--)
{
Swap(&A[1] , &A[i]);
Heapify(A , i-1 , 1);
}
}
int main()
{
int n;
cout << "Enter the Numebr of elements : ";
cin >> n;
cout << "Elements are : ";
int A[n+1];
for(int i=1;i<=n;i++)
{
A[i] = rand()%100;
cout << A[i] << "\t";
}
time_t s , e;
s=clock();
for(int i=0;i<100000;i++)
{
HeapSort(A , n);
}
e = clock();
cout << "\n\nSorted List : ";
for(int i=1;i<=n;i++)
{
cout << A[i] << "\t";
}
double cputime = (double)(e-s)/CLK_TCK;
cout << "\n\nTime : " << cputime << endl;
}