-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.c
129 lines (108 loc) · 2.83 KB
/
heap.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <stdio.h>
int size = 0, sizeForSort;
//swap method
void swap(int arr[], int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
//heapify method for making heap
void heapify(int arr[], int size, int i){
if(size==1){
printf("This is a one element array!\n");
}
else {
int val = i;
int l = 2*i + 1;
int r = 2*i + 2;
if(l<size && arr[l]>arr[val]){
val = l;
}
if(r<size && arr[r]>arr[val]){
val = r;
}
if(val != i){
swap(arr, i, val);
heapify(arr, size, val);
}
}
}
//inserting value at heap
void insertValue(int arr[], int val){
if(size == 0){
arr[0] = val;
size += 1;
}
else{
arr[size] = val;
size += 1;
for(int i=(size/2)-1; i>=0; i--){
heapify(arr, size, i);
}
}
}
//deleting value from heap
void deleteValue(int arr[], int num){
int i;
for(i=0; i<size; i++){
if(arr[i]==num){
break;
}
}
swap(arr, i, size-1);
arr[size-1] = 0;
size -= 1;
for(int j=(size/2)-1; j>=0; j--){
heapify(arr, size, j);
}
}
//printing heap array
void printHeap(int arr[]){
for(int i=0; i<size; i++){
printf("%d\t", arr[i]);
}
printf("\n");
}
//heap maximum value
void heapMax(int arr[]){
printf("Max value of the heap is - %d", arr[0]);
}
//sorting using heap
void heapSort(int arrE[]){
int sizeForSort = size;
int arrB[size], arrA[size];
//we are using copy of our main array. Because as we run this sort our main array disappeares as we run deletevalue funtion till size reaches
// we also update main size for not being any effect over the main size and main array. Later we could do other task with our main size and main array.
for(int i=0; i<size; i++){
arrA[i] = arrE[i];
}
//here another method has been called, HEAP-EXTRACT-MAX [which method returns max-value at every iteration]
for(int i=0; i<sizeForSort; i++){
arrB[sizeForSort-i-1] = arrA[0];
deleteValue(arrA, arrA[0]);
}
printf("Sorted arr is - ");
for(int i=0; i<sizeForSort; i++){
printf("%d\t", arrB[i]);
}
printf("\n");
//updating size as previous because size been reduced as we used deletevalue
size = sizeForSort;
}
int main(void){
int arr[10];
insertValue(arr, 1);
insertValue(arr, 2);
insertValue(arr, 3);
insertValue(arr, 4);
insertValue(arr, 5);
insertValue(arr, 6);
insertValue(arr, 7);
printHeap(arr);
deleteValue(arr, 4);
printHeap(arr);
heapSort(arr);
printHeap(arr);
heapMax(arr);
return 0;
}