-
Notifications
You must be signed in to change notification settings - Fork 1
/
Heap_sort.c
69 lines (69 loc) · 1.56 KB
/
Heap_sort.c
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
#include <stdio.h>
void create(int A[], int n)
{
/*Creates heap on the basis of the conditions that maximum numbers are at top as it
is a max heap.*/
int i = n;
int temp = A[i];
while (i > 1 && A[i / 2] < temp)
{ /*not equal to 1 as then comparison with 0.5 would also occur*/
A[i] = A[i / 2];
i = (i / 2);
}
A[i] = temp;
}
void insert(int A[], int n)
{
for (int i = 2; i <= n; i++)
{
create(A, i);
}
}
int delete (int A[], int n) // n here is no.of elements present in array.
{
/*deletion of only the root node occurs.*/
int val = A[1];
A[1] = A[n];
A[n] = val; // inserted maximum at the end of the array
int i = 1, j = (2 * i);
while (j < (n - 1))
{
if (A[j] < A[j + 1])
{
j = j + 1;
}
if (A[j] > A[i])
{
int temp;
temp = A[j];
A[j] = A[i];
A[i] = temp;
i = j;
j = (2 * i);
}
else
{
break;
}
}
return val;
}
int main()
{
int A[] = {0, 10, 20, 30, 25, 5, 40, 35};
insert(A, 7);
for (int i = 1; i <= 7; i++)
{
printf("%d ", A[i]);
}
for (int i = 7; i >= 1; i--)
{
delete (A, i); /*variable 'i' in delete represents number of elements before
deletion.*/
}
printf("\n");
for (int i = 1; i <= 7; i++)
{
printf("%d ", A[i]); // heap sort order of nlog(n)
}
}