You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
voidInsert(int A[], int n) // it is taking a heap array as well as index of element thst you want to insert
3
+
{
4
+
int i = n, temp; // i sud point on index of element when it is inserted i.e i will start from n // take a temp variable
5
+
temp = A[i]; // temp variable sud have value which we want to strore in heap
6
+
// now compare and swap the element or arrage it
7
+
while (i > 1 && temp > A[i / 2]) // as long as i is greater then 1 and temp is grateter then parent // if temp is greater then parent copy the patend value
8
+
{
9
+
A[i] = A[i / 2]; // a of i copy the value of a of i by 2
10
+
i = i / 2; // and i sud be moved to the parent
11
+
}
12
+
A[i] = temp; // finally we move to last place //temp reach at the right place // then copy the element a of i as temp
13
+
}
14
+
15
+
intmain() {
16
+
int H[] = { 0,10,20,30,25,5,40,35 }; // 1st pos will be 0 since it starts from 1
17
+
int i;
18
+
for (i = 2; i <= 7; i++) // using loop we are inserting elements // 1st is inserted so we will start from 2
19
+
Insert(H, i); // every time set element to the index
20
+
21
+
22
+
for (i = 1; i <= 7; i++) // using for loop to Display all the elements
23
+
printf("%d ", H[i]); // print all the elements as a heap
0 commit comments