-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPrg 10.cpp
70 lines (63 loc) · 1.01 KB
/
Prg 10.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
/**
Lab Program 10
Heap Operations
Author SkyKOG
*/
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <Windows.h>
void heaper(int,int*);
void insert(int,int*,int*);
void display(int *,int);
void main()
{
int arr[20]={1000},ele,n=0,ch;
while(true)
{
system("cls");
printf("1. Insert Into Heap\n2. Display Array\n3. Exit\n\nEnter Selection : ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter Element to Insert : ");
scanf("%d",&ele);
insert(ele,arr,&n);
display(arr,n);
break;
case 2:display(arr,n);
break;
case 3:exit(0);
break;
default:printf("Invalid Selection");
break;
}
getch();
}
}
void display(int *arr,int n)
{
int i;
printf("\n\nThe Contents of The Heap are as follows : ");
for(i=1;i<=n;i++)
{
printf("%d ",arr[i]);
}
}
void insert(int ele,int *arr,int *n)
{
(*n)++;
arr[*n]=ele;
heaper(*n,arr);
}
void heaper(int i,int *arr)
{
int val;
val=arr[i];
while(arr[i/2]<=val)
{
arr[i]=arr[i/2];
i=i/2;
}
arr[i]=val;
}