-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heap-Sort.cpp
99 lines (82 loc) · 1.87 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Heap-Sort.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <conio.h>
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <time.h>
using namespace std;
const int SIZE = 2000;
void HEAPIFY(int *A, int i, int heapsize)
{
int largest;
int left = 2 * i + 1;
int right = 2 * i + 2;
if((left <= heapsize) && (A[left] > A[i]))
largest = left;
else
largest = i;
if((right <= heapsize) && (A[right] > A[largest]))
largest = right;
if(largest != i)
{
swap(A[i], A[largest]);
HEAPIFY(A, largest, heapsize);
}
}
void BUILD_HEAP(int *A, int size)
{
int n = size - 1;
for(int i = n/2; i >= 0; i--)
HEAPIFY(A, i, n);
}
void HEAPSORT(int *A, int n)
{
int i;
BUILD_HEAP(A, n);
for(i = n - 1; i >= 1; i--)
{
swap(A[0], A[i]);
HEAPIFY(A, 0, i-1);
}
}
//int _tmain(int argc, _TCHAR* argv[])
int main()
{
int *arr;
int m = SIZE; //array input size;
cout<<"\n\nImplementation of the Heap-Sort Algorithm";
/*cout<<"\n\nEnter array size: ";
cin>>m;*/
arr = new int[m];
/*cout<<"\n\nInput elements of the array\n";
for(int i=0; i<m; i++)
cin>>arr[i];*/
//fill input array randomly
srand(static_cast<unsigned>(time(NULL)));
for(int i=0; i<SIZE; i++)
{
int randNum = 0;
int temp;
int num = rand();
temp = 1 + (num % SIZE); //generate a random number between 1 and SIZE
randNum = temp;
arr[i] = randNum;//randNum;
}
cout<<"\n\nUnsorted Array\n";
for(int i=0; i<m; i++)
cout<<arr[i]<<"\t";
_getch();
time_t start = clock();
HEAPSORT(arr, m); //Heap-Sort called on the array
time_t stop = clock();
time_t avg = stop - start;
cout<<"\n\nTime taken to sort (in clock ticks): "<<avg;
_getch();
cout<<"\n\nArray after sorting\n";
for(int i=0; i<m; i++)
cout<<arr[i]<<"\t";
_getch();
return 0;
}