0% found this document useful (0 votes)
73 views37 pages

NAME: Nitesh Patel Class: Se Comps B' Roll No: 49 Practical 1 Implementation of Stack Using Array

This document contains code for implementing various operations on linked lists like insertion, deletion and traversal. It defines a node structure with data and next pointer. The main function takes user input for list operations and calls corresponding functions to insert nodes at beginning, end or after a given node. Functions are also defined to delete nodes from beginning or end of list and to traverse and display the list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views37 pages

NAME: Nitesh Patel Class: Se Comps B' Roll No: 49 Practical 1 Implementation of Stack Using Array

This document contains code for implementing various operations on linked lists like insertion, deletion and traversal. It defines a node structure with data and next pointer. The main function takes user input for list operations and calls corresponding functions to insert nodes at beginning, end or after a given node. Functions are also defined to delete nodes from beginning or end of list and to traverse and display the list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

NAME : Nitesh Patel

CLASS : SE COMPS ‘B’ ROLL NO : 49


PRACTICAL 1

IMPLEMENTATION OF STACK USING ARRAY

#include
<stdio.h>
#include
<stdlib.h>
#include
<conio.h>
#define MAX 3

int st[MAX], top=-1;

void push(int st[], int


val); int pop(int
st[]);
int peek(int
st[]); void
display(int
st[]);

int main() {
int val,
option;
do
{
printf("\n *****MAIN
MENU*****"); printf("\n 1.
PUSH");
printf("\n 2. POP");
printf("\n 3. PEEK");
printf("\n 4. DISPLAY");
printf("\n 5. EXIT");
printf("\n Enter your option:
"); scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the number to be pushed on stack: ");
scanf("%d", &val);
push(st,
val);
break;
case 2:
val = pop(st); if(val != -1)
printf("\n The value deleted from stack is: %d",
val); break;
case 3:
val = peek(st);
if(val != -1)
printf("\n The value stored at top of stack is: %d", val);
break;
case 4:
displa
y(st);
brea
k;
}
}while(option != 5);
return 0;
}
void push(int st[], int val)

{
if(top == MAX-1)
{
printf("\n STACK OVERFLOW");
}
else
{
top++;
st[top]
= val;
}
}
int pop(int st[])
{
int val;
if (top == -1)
{
printf("\n STACK UNDERFLOW");
return -1;

else
{
val =
st[top]
; top--;
return val;
}
}

void display(int st[])

{
int i;

if(top == -1)
printf("\n STACK IS EMPTY");
else
{
for(i=top;i>=0;i--)
printf("\n %d",st[i]);

printf("\n");
}
}

int peek(int st[])

{
if(top == -1)

{
printf("\n STACK IS EMPTY");
return -1;
}
else
return (st[top]);
}

OUTPUT:
NAME : Nitesh Patel
CLASS : SE COMPS ‘B’ ROLL NO : 49
PRACTICAL 2

IMPLEMENTATION OF QUEUE USING ARRAY

#include
<stdio.h>
#include
<conio.h>
#define MAX
10 int
queue[MAX]
;
int front = -1, rear = -
1; void insert(void);
int
delete_element(voi
d); int peek(void);
void
display(void);
int main()
{
int
option,
val; do
{
printf(“\n\n ***** MAIN MENU
*****”); printf(“\n 1. Insert an
element”);
printf(“\n 2. Delete an
element”); printf(“\n 3.
Peek”);
printf(“\n 4. Display the
queue”); printf(“\n 5.
EXIT”);
printf(“\n Enter your option : “);
scanf(“%d”, &option);
switch(option)
{
case 1: insert();
break;
case 2:
val = delete_element();
if (val != -1)
printf(“\n The number deleted is : %d”,
val); break;
case 3:
val = peek();
if (val != -1)
printf(“\n The first value in queue is : %d”,
val); break;

case 4:
disp
lay
();
br
ea
k;
}
}while(option
!= 5);
getch();
return 0;
}
void insert()
{
int num;
printf(“\n Enter the number to be inserted in the
queue : “); scanf(“%d”, &num);
if(rear == MAX-1)
printf(“\n OVERFLOW”);
else if(front == -1 && rear ==
-1) front = rear = 0;
el
s
e
r
e
a
r
+
+
;
queue[rear] = num;
}
int delete_element()
{
int val;
if(front == -1 || front>rear)
{
printf(“\n
UNDERFLOW”);
return -1;
}
else
{
val =
queue[front]
; front++;
if(front >
rear) front =
rear = -1;
return val;
}
}
int peek()
{
if(front==-1 || front>rear)
{
printf(“\n QUEUE IS
EMPTY”); return -1;
}
else
{
return queue[front];
}
}
void display()
{
int i;
printf(“\n”);
if(front == -1 || front > rear)
printf(“\n QUEUE IS
EMPTY”);
else
{
for (i = front;i <= rear;i++) printf(“\t %d”, queue[i]);
}
}

OUTPUT:
NAME : Nitesh Patel
CLASS : SE COMPS ‘B’ ROLL NO : 49
PRACTICAL 3

IMPLEMENTATION OF INFIX TO POSTIX USING STACK

#include
<stdio.h>
#include
<conio.h>
#include
<ctype.h>
#include
<string.h>
#define MAX 100
char
st[MAX];
int top=–
1;
void push(char st[],
char); char pop(char
st[]);
void InfixtoPostfix(char source[], char
target[]); int getPriority(char);
int main()
{
char infix[100], postfix[100];
clrscr();
printf("\n Enter any infix expression
: "); gets(infix);
strcpy(postfix, "");
InfixtoPostfix(infix,
postfix);
printf("\n The corresponding postfix expression is : ");
puts(postfix);
getch();
return
0;
}
void InfixtoPostfix(char source[], char target[])
{
int i=0,
j=0; char
temp;
strcpy(target, "");
while(source[i]!='\0')
{
if f(source[i]=='(')
{
push(st,
source[i]);
i++;
}
else if(source[i] == ')')
{
while((top!=–1) && (st[top]!='('))
{
target[j] =
pop(st);
j++;
}
if(top==–1)
{
printf("\n INCORRECT EXPRESSION");
exit(1);
}
temp = pop(st);//remove left
parenthesis i++;
}
else if(isdigit(source[i]) || isalpha(source[i]))
{
target[j] =
source[i]; j++;
i++;
}
else if (source[i] == '+' || source[i] == '–' || source[i] == '*' || source[i] == '/' || source[i] == '%')
{
while( (top!=–1) && (st[top]!= '(') && (getPriority(st[top]) > getPriority(source[i])))
{
target[j] =
pop(st); j++;
}
push(st,
source[i]);
i++;
}
else
{
printf("\n INCORRECT ELEMENT IN EXPRESSION");
exit(1);
}
}
while((top!=–1) && (st[top]!='('))
{
target[j] =
pop(st); j++;
}
target[j]='\0';
}
int getPriority(char op)
{
if(op=='/' || op == '*' ||
op=='%') return 1;
else if(op=='+' ||
op=='–') return 0;
}
void push(char st[], char val)
{
if(top==MAX–1)
printf("\n STACK OVERFLOW");
else
{
top++;
st[to
p]=va
l;
}
}
char pop(char st[])
{
char val=' ';
if(top==–1)
printf("\n STACK UNDERFLOW");
else
{
val=st[
top];
top–
–;
}
return val;
}

OUTPUT:
NAME : Nitesh Patel
CLASS : SE COMPS ‘B’ ROLL NO : 49
PRACTICAL 4

IMPLEMENTATION OF CIRCULAR QUEUE

#include<stdi
o.h>
#include<co
nio.h>
#define
MAX 10

int queue[MAX];
int front=-1,rear=-1;

void insert();
int
delete_elemen
t(); void
display();

int main()
{
int
val,opti
on; do
{ printf("\n 1.Insert
element"); printf("\n
2.Delete element");
printf("\n 3.Display
element"); printf("\n
4.EXIT");
printf("\n Enter a
choice");
scanf("%d",&option)
; switch(option)
{
case
1:insert();
break;
case
2:val=delete_el
ement();
if(val!=-1)
printf(“The number deleted is
%d",val); break;
case 3:display();
break;
}
}while(option!=4);
getch();
}

void insert()
{
int num;
printf("Enter number to be
inserted"); scanf("%d",&num);
if(front==-1&&rear==-1)
{
front=rear=
0;
queue[rear]
=num;
}
else if(front==0&&rear==MAX-1)
{ printf("\n OVERFLOW");
}
else if(rear==MAX-1&&FRONT!=0)
{ rear=0;
queue[rear]
=num;
}
else
{ rear++;
queue[rear]
=num;
}
}
int delete_element()
{
int val;
if(front==-1&&rear==-1)
{
printf("\n Underflow");
}
val=queue[front];
if(front==rear)
{
front=rear=-1;
}
else
{
if(front==
MAX-1)
front=0;
else
front=fr
ont+1;
}
return val;
}
void display()
{
int i;
if(front==-1&&rear==-
1) printf("\n Queue is
empty");

else
{
if(front<rear)
{
for(i=front;i<=rear
;i++) printf("\t
%d",queue[i]);
}
else
{
for(i=front;i<MAX
;i++) printf("\t
%d",queue[i]);
for(i=0;i<=rear;i++
) printf("\t
%d",queue[i]);
}
}
}

OUTPUT:
NAME : Nitesh Patel
CLASS : SE COMPS ‘B’ ROLL NO : 49
PRACTICAL 5

IMPLEMENTATION OF SINGLY LINKED LIST

#include
<stdio.h>
#include
<stdlib.h>
#include
<conio.h>
#include
<malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *start = NULL;
struct node *display(struct node *);
struct node *insert_beg(struct
node *); struct node
*insert_end(struct node *); struct
node *insert_after(struct node *);
struct node *delete_beg(struct
node *); struct node
*delete_end(struct node *);
int main()
{
int option;

do
{
printf(“\n\n *****MAIN MENU
*****”); printf(“\n 1: Display the
list”);
printf(“\n 2: Add a node at the
beginning”); printf(“\n 3: Add a node
at the end”);
printf(“\n 4: Add a node after a given node”);
printf(“\n 5: Delete a node from the
beginning”); printf(“\n 6: Delete a node
from the end”);
printf(“\n 7: EXIT”);
printf(“\n\n Enter your option :
“); scanf(“%d”, &option);
switch(option)
{
case 1: start =
display(start); break;
case 2: start =
insert_beg(start); break;
case 3: start =
insert_end(start); break;
case 4: start =
insert_after(start); break;
case 5: start =
delete_beg(start); break;
case 6: start =
delete_end(start); break;
}
}while(option
!=7);
getch();
return 0;
}
struct node *display(struct node *start)
{
struct node
*ptr; ptr =
start;
while(ptr != NULL)
{
printf(“\t %d”, ptr ->
data); ptr = ptr ->
next;
}
return start;
}
struct node *insert_beg(struct node *start)
{
struct node
*new_node; int
num;
printf(“\n Enter the data : “);
scanf(“%d”, &num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
new_node -> next =
start; start =
new_node;
return start;
}
struct node *insert_end(struct node *start)
{
struct node *ptr,
*new_node; int num;
printf(“\n Enter the data : “);
scanf(“%d”, &num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
new_node -> next =
NULL; ptr = start;
while(ptr -> next !=
NULL) ptr = ptr ->
next;
ptr -> next =
new_node; return
start;
}
struct node *insert_after(struct node *start)
{
struct node *new_node, *ptr,
*preptr; int num, val;
printf(“\n Enter the data : “);
scanf(“%d”, &num);
printf(“\n Enter the value after which the data has to be inserted :
“); scanf(“%d”, &val);
new_node = (struct node *)malloc(sizeof(struct
node)); new_node -> data = num;
ptr =
start;
preptr =
ptr;
while(preptr -> data != val)
{
preptr = ptr;
ptr = ptr -> next;
}
preptr ->
next=new_node;
new_node -> next =
ptr; return start;
}
struct node *delete_beg(struct node *start)
{
struct node
*ptr; ptr =
start;
start = start ->
next;
free(ptr);
return start;
}
struct node *delete_end(struct node *start)
{
struct node *ptr,
*preptr; ptr = start;
while(ptr -> next != NULL)
{
preptr = ptr;
ptr = ptr -> next;
}

preptr -> next =


NULL; free(ptr);
return start;
}
OUTPUT:
NAME : Nitesh Patel
CLASS : SE COMPS ‘B’ ROLL NO : 49
PRACTICAL 6

IMPLEMENTATION OF QUEUE USING LINKED LIST

#include
<stdio.h>
#include
<conio.h>
#include
<malloc.h>
struct node
{
int data;
struct node *next;
};
struct queue
{
struct node
*front;
struct node
*rear;
};
struct queue *q;
void create_queue(struct queue *);
struct queue *insert(struct queue
*,int);
struct queue *delete_element(struct queue *);
struct queue *display(struct queue *);
int peek(struct queue *);
int main()
{
int val,
option;
create_que
ue(q);
clrscr();
do
{
printf("\n *****MAIN
MENU*****"); printf("\n 1.
INSERT");
printf("\n 2. DELETE");
printf("\n 3. PEEK");
printf("\n 4. DISPLAY");
printf("\n 5. EXIT");
printf("\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the number to insert in the queue:");
scanf("%d", &val);
q =
insert(q,v
al); break;
case 2:
q=
delete_element(
q); break;

case 3:
val =
peek(q)
; if(val!
= –1)
printf("\n The value at front of queue is : %d",
val); break;
case 4:
q=
display(
q);
break;
}
}while(option != 5);
getch();
return
0;
}
void create_queue(struct queue *q)
{
q -> rear =
NULL; q ->
front =
NULL;
}
struct queue *insert(struct queue *q,int val)
{
struct node *ptr;
ptr = (struct node*)malloc(sizeof(struct
node)); ptr -> data = val;
if(q -> front == NULL)
{
q -> front
= ptr; q -
> rear =
ptr;
q -> front -> next = q -> rear -> next = NULL;
}
else
{
q -> rear -> next
= ptr; q -> rear
= ptr;
q -> rear -> next = NULL;
}
return q;
}
struct queue *display(struct queue *q)
{
struct node
*ptr; ptr = q -
> front;
if(ptr == NULL)
printf("\n QUEUE IS EMPTY");
else
{
printf("\n");
while(ptr!=q -
> rear)
{
printf("%d\t", ptr ->
data); ptr = ptr ->
next;
}
printf("%d\t", ptr -> data);
}
return q;
}
struct queue *delete_element(struct queue *q)
{
struct node
*ptr; ptr = q -
> front;
if(q -> front == NULL)
printf("\n
UNDERFLOW");
else
{
q -> front = q -> front -> next;
printf("\n The value being deleted is : %d", ptr ->
data); free(ptr);
}
return q;
}
int peek(struct queue *q)
{
if(q->front==NULL)
{
printf("\n QUEUE IS
EMPTY"); return –1;
}
else
return q->front->data;
}
OUTPUT:
NAME : Nitesh Patel
CLASS : SE COMPS ‘B’ ROLL NO : 49
PRACTICAL 7

IMPLEMENTATION OF BINARY SEARCH TREE MENU DRIVEN PROGRAM

#include
<stdio.h>
#include
<conio.h>
#include
<malloc.h>
struct node
{
int data;
struct node
*left; struct
node *right;
};
struct node *tree;
void create_tree(struct node *);
struct node *insertElement(struct node *,
int); void inorderTraversal(struct node
*);
struct node *deleteElement(struct node *, int);
int main()
{
int option,
val; struct
node *ptr;
create_tree
(tree);
clrscr();
do
{
printf("\n ******MAIN MENU*******
\n"); printf("\n 1. Insert Element");
printf("\n 2. Inorder
Traversal"); printf("\n 3.
Delete an element");
printf("\n 4. Exit");
printf("\n\n Enter your option :
"); scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the value of the new node : ");
scanf("%d", &val);
tree = insertElement(tree,
val); break;
case 2:
printf("\n The elements of the tree are :
\n"); inorderTraversal(tree);
bre
ak;
cas
e
3:
printf("\n Enter the element to be deleted : ");
scanf("%d", &val);
tree = deleteElement(tree,
val); break;
}
}while(option!
=14); return
0;
}
void create_tree(struct node *tree)
{
tree = NULL;
}
struct node *insertElement(struct node *tree, int val)
{
struct node *ptr, *nodeptr, *parentptr;
ptr = (struct node*)malloc(sizeof(struct node));
ptr–>data = val;
ptr–>left =
NULL; ptr–
>right =
NULL;
if(tree==NU
LL)
{
tree=ptr;
tree–
>left=NULL;
tree–
>right=NULL
;
}
else
{
parentptr=NULL;
nodeptr=tree;
while(nodeptr!=
NULL)
{
parentptr=nodeptr
; if(val<nodeptr–
>data)
nodeptr=nodeptr
–>left; else
nodeptr = nodeptr–>right;
}
if(val<parentptr–
>data)
parentptr–>left =
ptr; else
parentptr–>right = ptr;
}
return tree;
}
void inorderTraversal(struct node *tree)
{
if(tree != NULL)
{
inorderTraversal(tree-
>left); printf("%d\t",
tree->data);
inorderTraversal(tree-
>right);}
}
struct node *deleteElement(struct node *tree, int val)
{
struct node *cur, *parent, *suc, *psuc, *ptr; if(tree–
>left==NULL)
{
printf("\n The tree is
empty "); return(tree);
}
parent =
tree; cur =
tree–
>left;
while(cur!=NULL && val!= cur–>data)
{
parent = cur;
cur = (val<cur–>data)? cur–>left:cur–>right;
}
if(cur == NULL)
{
printf("\n The value to be deleted is not present in the tree");
return(tree);
}
if(cur–>left ==
NULL) ptr =
cur–>right;
else if(cur–>right ==
NULL) ptr = cur–
>left;
else
{
psuc = cur;
cur = cur–>left;
while(suc–
>left!=NULL)
{
psuc = suc;
suc = suc–>left;
}
if(cur==psuc)
{
suc–>left = cur–>right;
}
else
{
suc–>left = cur–
>left; psuc–>left =
suc–>right; suc–
>right = cur–
>right;
}
ptr = suc;
}
if(parent–>left
== cur) parent–
>left=ptr;
else parent–
>right=ptr;
free(cur);
return tree;
}
OUTPUT
NAME : Nitesh Patel
CLASS : SE COMPS ‘B’ ROLL NO : 49
PRACTICAL 8

IMPLEMENTATION OF QUICK SORT


#include<std
io.h> int
main()
{
int i, count, number[25];
printf("@@@@@@@@@@@@@@@ QUICK SORT
@@@@@@@@@@@@@@@@@@"); printf("ENTER THE NUMBER OF ELEMENT TO
SORT:");
scanf("%d",&count);
printf("Enter %d elements: ",
count); for(i=0;i<count;i++)
scanf("%d",&number
[i]);
quicksort(number,0,co
unt-1);
printf("Order of Sorted
elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
void quicksort(int number[25],int first,int last)
{
int i, j, pivot, temp;
if(first<last)
{
pivot
=firs
t;
i=firs
t;
j=las
t;
while(i<j)
{
while(number[i]<=number[pivot]&&
i<last) i++;
while(number[j]>number[
pivot]) j--;
if(i<j)
{
temp=number[i
];
number[i]=num
ber[j];
number[j]=tem
p;
}
}
temp=number[pivot];
number[pivot]=numb
er[j];
number[j]=temp;
quicksort(number,fir
st,j-1);
quicksort(number,j+
1,last);

}
}

OUTPUT:
NAME : Nitesh Patel
CLASS : SE COMPS ‘B’ ROLL NO : 49
PRACTICAL 9

Implementation of Linear search and Binary search

#include <stdio.h>
#include <stdlib.h>
main()
{
int array[100],search_key,i,j,n,low,high,location,choice;
void linear_search(int search_key,int array[100],int n);
void binary_search(int search_key,int array[100],int n);
clrscr();
printf("ENTER THE SIZE OF THE ARRAY:");
scanf("%d",&n);
printf("ENTER THE ELEMENTS OF THE ARRAY:\n");
for(i=1;i<=n;i++)
{
scanf("%d",&array[i]);
}
printf("ENTER THE SEARCH KEY:");
scanf("%d",&search_key);
printf("___________________\n");
printf("1.LINEAR SEARCH\n");
printf("2.BINARY SEARCH\n");
printf("___________________\n");
printf("ENTER YOUR CHOICE:");
scanf("%d",&choice);
switch(choice)
{
case 1:
linear_search(search_key,array,n);
break;
case 2:
binary_search(search_key,array,n);
break;
default:
exit(0);
}
getch();
return 0;
}
void linear_search(int search_key,int array[100],int n)
{
int i,location;
for(i=1;i<=n;i++)
{
if(search_key == array[i])
{
location = i;
printf("______________________________________\n");
printf("The location of Search Key = %d is %d\n",search_key,location);
printf("______________________________________\n");
}
}
}

void binary_search(int search_key,int array[100],int n)


{
int mid,i,low,high;
low = 1;
high = n;

mid = (low + high)/2;


i=1;
while(search_key != array[mid])
{
if(search_key <= array[mid])
{
low = 1;
high = mid+1;
mid = (low+high)/2;
}
else
{
low = mid+1;
high = n;
mid = (low+high)/2;
}
}
printf("__________________________________\n");
printf("location=%d\t",mid);
printf("Search_Key=%d Found!\n",search_key);
printf("__________________________________\n");
}

Output:

ENTER THE SIZE OF THE ARRAY: 6


ENTER THE ELEMENTS OF THE ARRAY:
45
6
86
23
64
77
ENTER THE SEARCH KEY: 86
__________________________________
1. LINEAR SEARCH
2.BINARY SEARCH
__________________________________
ENTER YOUR CHOICE: 2
__________________________________
Location 3 Search_Key= 86 Found!
__________________________________
Press any key to continue .......
NAME : Nitesh Patel
CLASS : SE COMPS ‘B’ ROLL NO : 49
PRACTICAL 10 A

Implementation of Depth First Search

#include<stdio.h>
#include<conio.h>
void DFS(int);
int main()
{
int n,i,j,G[100][100],visited[100];
clrscr();
printf("\n Enter The Number Of Vertices : ");
scanf("%d",&n);
printf("\n Enter The Adjacency Matrix : ");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf(" The DFS Traversal Is : ");
for(i=0;i<n;i++)
{
visited[1]=0;
DFS(i);
}
getch();
return 0;
}
void DFS(int i)
{
int n,j,visited[100],G[100][100];
printf("%d\t",i);
visited[i]=1;
for(j=0;j<n;j++)
{
if(!visited[j]&&G[i][j]==1)
{
DFS(i);
}
}
}

Output :-
NAME : Nitesh Patel
CLASS : SE COMPS ‘B’ ROLL NO : 49
PRACTICAL 10 B

Implementation of Breadth First Search

#include <stdio.h>

#define MAX 10

void breadth_first_search(int adj[][MAX],int visited[],int start)

int queue[MAX],rear = –1,front =– 1, i;

queue[++rear] = start;

visited[start] = 1;

while(rear != front)

start = queue[++front];

if(start == 4)

printf("5\t");

else

printf("%c \t",start + 65);

for(i = 0; i < MAX; i++)

if(adj[start][i] == 1 && visited[i] == 0)

queue[++rear] = i;

visited[i] = 1;

void main()

int visited[MAX] = {0};


int adj[MAX][MAX], i, j;

clrscr();

printf("\n Enter the adjacency matrix: ");

for(i = 0; i < MAX; i++)

for(j = 0; j < MAX; j++)

scanf("%d", &adj[i][j]);

breadth_first_search(adj,visited,0);

getch();

OUTPUT:-
NAME : Nitesh Patel
CLASS : SE COMPS ‘B’ ROLL NO : 49
PRACTICAL 11

Implementation of Heap Sort

#include<stdio.h>
void create(int []);
void down_adjust(int [],int);
void main()
{
int heap[30],n,i,last,temp;
clrscr();
printf("Enter no. of elements:");
scanf("%d",&n);
printf("\nEnter elements:");
for(i=1;i<=n;i++)
scanf("%d",&heap[i]);
//create a heap
heap[0]=n;
create(heap);
//sorting
while(heap[0] > 1)
{
//swap heap[1] and heap[last]
last=heap[0];
temp=heap[1];
heap[1]=heap[last];
heap[last]=temp;
heap[0]--;
down_adjust(heap,1);
}
//print sorted data
printf("\nArray after sorting:\n");
for(i=1;i<=n;i++)
printf("%d ",heap[i]);
getch();
}
void create(int heap[])
{
int i,n;
n=heap[0]; //no. of elements
for(i=n/2;i>=1;i--)
down_adjust(heap,i);
}
void down_adjust(int heap[],int i)
{
int j,temp,n,flag=1;
n=heap[0];

while(2*i<=n && flag==1)


{
j=2*i; //j points to left child
if(j+1<=n && heap[j+1] > heap[j])
j=j+1;
if(heap[i] > heap[j])
flag=0;
else
{
temp=heap[i];
heap[i]=heap[j];
heap[j]=temp;
i=j;
}
}
}
OUTPUT:

You might also like