-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheapsort2.cpp
83 lines (67 loc) · 1.35 KB
/
heapsort2.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
// Program for Heap Sorting
#include <iostream>
using namespace std;
#define LEFT(X) (2*X + 1)
#define RIGHT(X) (2*X + 2)
int heapsize;
int count =0;
void maxHeapify( int a[], int i )
{
int largest;
int l = LEFT(i);
int r = RIGHT(i);
if( l < heapsize && a[l] > a[i] )
largest = l;
else
largest = i;
if( r < heapsize && a[r] > a[largest] )
largest = r;
count++;
if( largest != i )
{
int temp = a[i];
a[i] = a[largest];
a[largest] = temp;
maxHeapify( a, largest );
}
}
void build_max_heap(int a[], int size)
{
heapsize = size;
for( int i = ((size/2)-1); i >= 0; i-- )
maxHeapify( a, i );
}
void heapsort( int a[], int size )
{
int i;
int temp;
build_max_heap( a, size );
for( i = size - 1; i >= 1; i-- )
{
temp = a[0];
a[0] = a[i];
a[i] = temp;
heapsize--;
maxHeapify( a, 0 );
}
}
int main()
{
cout << " \n\t\t\t Heap Sorting \n\t\t\t ";
int size;
cout << " \n\t\t\t Enter the size of array: \n\t\t\t ";
cin >> size;
int *a = new int [size];
cout << " \n\t\t\t Enter the elements of an unsorted array: \n\t\t\t ";
for( int i = 0; i < size; i++ )
{ cin >> a[i]; cout << " \n\t\t\t "; }
heapsort( a, size );
cout << " \n\t\t\t New sorted array is: \n\t\t\t ";
for( int j = 0; j < size; j++ )
{
cout << a[j] << " ";
}
cout << " \n\n\t\t\t The no. of comparisons are: " << count;
cout << "\n\n";
return 0;
}