Skip to content

Commit 45e80d6

Browse files
authored
Add files via upload
1 parent bfc7b59 commit 45e80d6

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

Heap/03 Insert in Heap.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <stdio.h>
2+
void Insert(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+
int main() {
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
24+
printf("\n");
25+
26+
return 0;
27+
}

Heap/04 Delete element from Heap.c

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <stdio.h>
2+
void Insert(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+
int Delete(int A[], int n) // fxn for del elements from the heap // take array as a // and the size of array we need the last index
15+
{
16+
int i, j, x, temp, val;
17+
val = A[1]; // value which is deleated
18+
x = A[n]; // take last elemet in x
19+
A[1] = A[n]; // copy last at 1st place
20+
A[n] = val; // last value sud be assign with deleted value // that element will not consider in heap
21+
i = 1; j = i * 2; // i starts from 1st elements and j points on left child of i (i*2)
22+
while (j <= n - 1) // adjusting the elements // as we del 1 elements so size of array reduced so it is n-1
23+
{
24+
if (j<n - 1 && A[j + 1]>A[j]) //compare left child and right child which ever is greater let j point on that
25+
j = j + 1;
26+
if (A[i] < A[j]) // if parent is smaller then child
27+
{
28+
temp = A[i]; // swap the elements
29+
A[i] = A[j];
30+
A[j] = temp;
31+
i = j; // i sud come upon j i.e child
32+
j = 2 * j; // j sud move to left child
33+
}
34+
else // it it is properly adjusted then break
35+
break;
36+
}
37+
return val; // return deleted value
38+
}
39+
int main() {
40+
int H[] = { 0,10,20,30,25,5,40,35 }; // 1st pos will be 0 since it starts from 1
41+
int i;
42+
for (i = 2; i <= 7; i++) // using loop we are inserting elements // 1st is inserted so we will start from 2
43+
Insert(H, i); // every time set element to the index
44+
45+
printf("Deleted Value is %d \n",Delete(H,7));
46+
printf("Deleted Value is %d \n", Delete(H, 6)); // deleted value will be in the last of array that will not considerd in heap
47+
48+
for (i = 1; i <= 7; i++) // using for loop to Display all the elements
49+
printf("%d ", H[i]); // print all the elements as a heap
50+
printf("\n");
51+
52+
return 0;
53+
}

Heap/05 Heap sort.c

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <stdio.h>
2+
void Insert(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+
int Delete(int A[], int n) // fxn for del elements from the heap // take array as a // and the size of array we need the last index
15+
{
16+
int i, j, x, temp, val;
17+
val = A[1]; // value which is deleated
18+
x = A[n]; // take last elemet in x
19+
A[1] = A[n]; // copy last at 1st place
20+
A[n] = val; // last value sud be assign with deleted value // that element will not consider in heap
21+
i = 1; j = i * 2; // i starts from 1st elements and j points on left child of i (i*2)
22+
while (j <= n - 1) // adjusting the elements // as we del 1 elements so size of array reduced so it is n-1
23+
{
24+
if (j<n - 1 && A[j + 1]>A[j]) //compare left child and right child which ever is greater let j point on that
25+
j = j + 1;
26+
if (A[i] < A[j]) // if parent is smaller then child
27+
{
28+
temp = A[i]; // swap the elements
29+
A[i] = A[j];
30+
A[j] = temp;
31+
i = j; // i sud come upon j i.e child
32+
j = 2 * j; // j sud move to left child
33+
}
34+
else // it it is properly adjusted then break
35+
break;
36+
}
37+
return val; // return deleted value
38+
}
39+
int main() {
40+
//int H[] = { 0,10,20,30,25,5,40,35 }; // 1st pos will be 0 since it starts from 1
41+
int H[20],no;
42+
int i;
43+
printf("enter the no of elements in array ");
44+
scanf_s("%d", &no);
45+
for (i = 0; i <= no; i++)
46+
{
47+
scanf_s("%d", &H[i]);
48+
}
49+
50+
51+
for (i = 2; i <= 7; i++) // using loop we are inserting elements // 1st is inserted so we will start from 2
52+
Insert(H, i); // every time set element to the index
53+
54+
// now let us run a loop for Heap sort
55+
for (i = 7; i >1 ; i--)
56+
{
57+
Delete(H, i); // calling del fxn
58+
}
59+
for (i = 1; i <= 7; i++) // using for loop to traverse through elements
60+
printf("%d ", H[i]); // print all the elements in a sorted manner
61+
printf("\n");
62+
63+
return 0;
64+
}

0 commit comments

Comments
 (0)