DS LAB Updated
DS LAB Updated
Given {4,7,3,2,1,7,9,0} find the location of 7 using Binary search and also display its first
occurrence.
FLOW CHART
ALGORITHAM
BINARY_SEARCH(A, lower_bound, upper_bound, VAL)
Step 5: IF POS = -1
PRINT "VALUE IS NOT PRESENT IN THE ARRAY"
[END OF IF]
Step 6: EXIT
SOURCE CODE
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10]={4,7,3,2,1,7,9,0};
int i, j, n, low, high, mid, item, temp, key;
clrscr();
n=8;
printf("\n Given Array elements are:\n");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n Sorted Array elements are");
for(i=0;i<n;i++)
printf("\n %d",a[i]);
printf("\n enter the element to be searched");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
{
printf("\n key %d found succesfully at position %d",key,mid++);
break;
}
else if(key<a[mid]) high=mid-1;
else low=mid+1;
}
if(low>high)
printf("\n key %d not found",key);
getch();
}
OUTPUT :
Program 2 : Given{5,3,1,6,0,2,4} order the numbers in ascending order using Quick sort.
FLOW CHART :
ALGORITHAM
PARTITION (ARR, BEG, END, LOC)
Step 1: [INITIALIZE] SET LEFT = BEG, RIGHT = END, LOC = BEG, FLAG =
[END OF IF]
Step 5: IF FLAG = 0
Repeat while ARR[LOC] >= ARR[LEFT] AND LOC != LEFT
SET LEFT = LEFT + 1
[END OF LOOP]
Step8: END
Step2: END
SOURCE CODE
#include <stdio.h>
#include<conio.h>
void quicksort (int [], int, int);
void main()
{
int list[50];
int size, i;
clrscr();
printf("Enter the number of elements: ");
scanf("%d", &size);
printf("Enter the elements to be sorted:\n");
for (i = 0; i < size; i++)
{
scanf("%d", &list[i]);
}
quicksort(list, 0, size - 1);
printf("After applying quick sort\n");
for (i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
printf("\n");
getch();
}
void quicksort(int list[], int low, int high)
{
int pivot, i, j, temp; if (low < high)
{
pivot = low; i = low;
j = high; while (i < j)
{
while (list[i] <= list[pivot] && i <= high)
{
i++;
}
while (list[j] > list[pivot] && j >= low)
{
j--;
}
if (i < j)
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
temp = list[j];
list[j] = list[pivot];
list[pivot] = temp;
quicksort(list, low, j - 1);
quicksort(list, j + 1, high);
}
}
OUTPUT
Program 3: Perform the Merge Sort on the input {75,8,1,16,48,3,7,0} and display the output in descending order.
FLOW CHART
ALGORITHM
Step1: [INITIALIZE] SET I = BEG, J = MID + 1, INDEX = 0
Step 6: Exit
#include<stdio.h>
#include<conio.h>
voidmergesort(int a[],inti,int j);
void merge(int a[],int i1,int j1,int i2,int j2);
void main()
{
int a[8]={75,8,1,16,48,3,7,0},i;
clrscr();
printf("Array elements are:");
for(i=0;i<8;i++)
printf("%d\t",a[i]);
mergesort(a,0,8-1);
printf("\nSorted array is :");
for(i=0;i<8;i++)
printf("%d\t",a[i]);
getch();
}
void mergesort(int a[],int i, int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a, i, mid);
mergesort(a,mid+1,j);
merge(a,i,mid,mid+1,j);
//merging of two sorted sub-arrays
}
}
void merge(int a[],int i1,int j1,int i2,int j2)
{
int temp[50];
int i, j, k;
i=i1;
j=i2;
k=0;
while(i<=j1 && j<=j2)
{
if(a[i]>a[j]) temp[k++]=a[i++];
else temp[k++]=a[j++];
}
while(i<=j1) temp[k++]=a[i++];
while(j<=j2) temp[k++]=a[j++];
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}
OUTPUT
Program 4: Write a program to insert the elements 61,16,8,27 into singly linked list and delete 8,61,27 from the list.
Display your list after each insertion and deletion.
FLOW CHART
ALGORITHAM:
Step1:IF HEAD=NULL
Step2:set PTR=HEAD
Step3:Repeat Step 4 and 5
While PTR->NEXT!=NULL
Step4:set PRE PTR=PTR
Step5:set PTR=PTR->NEXT
Step6:set PRE PTR->NEXT=NULL
Step7:FREE PTR
Step8:EXIT
SOURCE CODE
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct node
{
int value;
struct node *next;
};
void insert();
void display();
void delete();
int count();
scanf("%d", &option);
switch (option)
{
case 1:
insert();
display();
break;
case 2:
delete();
display();
break;
case 3:
display();
break;
default:
break;
}
}
return 0;
}
void insert()
{
printf("\n Enter Element for Insert Linked List : \n");
scanf("%d", &data);
void delete()
{
int countvalue, pos, i = 0;
countvalue = count();
temp_node = first_node;
{
head_node = prev_node;
}
printf("\nDeleted Successfully \n\n");
break;
}
else
{
i++;
prev_node = temp_node;
temp_node = temp_node -> next;
}
}
}
} else
printf("\nInvalid Position \n\n");
}
void display()
{
int count = 0;
temp_node = first_node;
printf("\nDisplay Linked List : \n");
while (temp_node != 0)
{
int count()
{
int count = 0;
temp_node = first_node;
while (temp_node != 0)
{
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
return count;
}
OUTPUT
Program 5: Write a program to add 6x3 +10x2+0x+5 and 4x2 +2x+1 using linked list.
FLOW CHART
ALGORTHIM
ALGORITHM:
Step1:Loop around all values of linked list and follow step 2and 3
Step2:IF the value of nodes exponent is greater copy this node to result node and head ->NEXT NODE
Step3:IF value of both nodes exponent is same add the coefficient and then copy the added value with
node to result.
Step4:Print the resultant node
Step5:End
SOURCE CODE
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{
intcoeff;
intpow;
struct Node* next;
};
void addPolynomials(struct Node** result, struct Node* first, struct Node* second)
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->next = NULL;
*result = temp; while(first && second)
{
if(first->pow> second->pow)
{
temp->coeff = first->coeff;
temp->pow = first->pow;
first = first->next;
}
}
else }
{
t f = second->coeff; temp-
e >pow = second->pow;
m second = second->next;
p
-
>
c temp->coeff = first->coeff + second->coeff;
o temp->pow = first->pow;
e first = first->next;
f second = second->next;
}
while(first || second)
{
temp->next = (struct Node*)malloc(sizeof(struct Node));
temp = temp->next;
temp->next = NULL;
if(first)
{
temp->coeff = first->coeff;
temp->pow = first->pow;
first = first->next;
}
else if(second)
{
temp->coeff = second->coeff;
temp->pow = second->pow;
second = second->next;
}
}
}
void main()
{
displayPolynomial(first);
printf("\nSecond polynomial:\n");
readPolynomial(&second);
printf("\nSecond");
displayPolynomial(second);
addPolynomials(&result, first, second);
printf("\n\nAdded");
displayPolynomial(result);
getch();
}
OUTPUT
DATA STRUCTURE LAB
27
DATA STRUCTURE LAB
Program 6: Write a program to push 5,9,34,17,32 into stack and pop 3 times from
the stack, also display the popped numbers.
FLOW CHART
28
DATA STRUCTURE LAB
29
DATA STRUCTURE LAB
DISPLAY
30
DATA STRUCTURE LAB
ALGORITHM
PUSH()
Step1:begin
Step2: if top = n then stack full
Step3: top = top + 1
Step4:stack (top) : = item; Step5:end
POP()
Step1:begin
Step2: IF top = 0 then stack empty;
Step3: item := stack(top);
Step4:top = top - 1; Step5:end;
Step2:item = stack[top]
Step3: return item
Step4:End
SOURCE CODE
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
Structstack
{
int data;
Structstack*next;
};
Typedef Structstack S;
S*top;
void push();
void pop();
void display();
void main()
{
int ch;
top=(S*)malloc(sizeof(S));
top=NULL;
while(1)
{
clrscr();
printf("1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n");
printf("\nEnteryourchoice:");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case4:exit(0);
break;
default:printf("InvalidChoice\n");
}
getch();
32
DATA STRUCTURE LAB
}
void push()
{
S*temp;
temp=(S*)malloc(sizeof(S));
printf("Enter the element:");
scanf("%d",&temp->data);
if(top==NULL)
{
temp->next=NULL;
top=temp;
}
else
{
temp->next=top;
top=temp;
}
printf("The %d element PUSHED succefully into the Stack\n",temp->data);
}
void pop()
{
S*temp;
temp=top;
if(top==NULL)
printf("StackUnderflow\n");
else
{
top=top->next;
printf("Element deleted is%d",temp->data); free(temp);
}
}
void display()
{
S*temp;
temp=top;
if(top==NULL)
printf("Stack is Empty\n"); else
{
}
}
}
OUTPUT
34
DATA STRUCTURE LAB
FLOW CHART
ALGORITHM:
Step1:GCD(x,y)
Step2:begin IF(y=0) return x
else
Cal GCD(y, x%y)
[END IF]
Step3:End
35
DATA STRUCTURE LAB
SOURCE CODE
#include<stdio.h>
#include<conio.h>
intfind_gcd(int , int);
void main()
{
int a, b, c, gcd1,gcd2;
clrscr();
printf("\n\nEnter three numbers to find GCD of \n");
scanf("%d%d%d", &a, &b, &c);
if(a==0 && b==0 && c==0)
{
printf("\nInvalid number");
exit(0);
}
gcd1=gcd(a,b);
gcd2=gcd(c,gcd1);
printf("\n\nGCD of %d ,%d and %d is: %d\n\n", a, b, c,gcd2);
getch();
}
OUTPUT
36
DATA STRUCTURE LAB
37
DATA STRUCTURE LAB
Program 8 : Write a program to insert the elements {5,7,0,6,3,9} into circular queue and delete 6,9 &
5 from it(using linked list implementation).
FLOW CHART
38
DATA STRUCTURE LAB
ALGORITHM
39
DATA STRUCTURE LAB
Step 4: EXIT
Step 1: IF FRONT = -1
Write " UNDERFLOW "
Go to Step 4
[END of IF]
Step 4: EXIT
40
DATA STRUCTURE LAB
SOURCE CODE
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
struct node *f = NULL;
struct node *r = NULL;
void enqueue(int d) //Insert elements in Queue
{
struct node* n;
n = (struct node*)malloc(sizeof(struct node));
n->data = d;
n->next = NULL;
if((r==NULL)&&(f==NULL))
{
f = r = n;
r->next = f;
}
else
{
r->next = n; r = n;
n->next = f;
}
}
Void dequeue()
{
struct node* t;
t = f;
if((f==NULL)&&(r==NUL
L))
printf("\nQueue is Empty");
else if(f == r)
{
f = r = NULL;
free(t);
41
DATA STRUCTURE LAB
else{
f = f->next;
r->next = f;
free(t);
}
}
void print()
{
// Print the elements of Queue
struct node* t;
t = f;
if((f==NULL)&&(r==NULL))
printf("\nQueue is Empty");
else
{
do{
printf("\n%d",t->data);
t = t->next;
}while(t != f);
}
}
int main()
{
int opt,n,i,data;
printf("Enter Your Choice:-");
do{
printf("\n\n1 for Insert the Data in Queue\n2 for show the Data in Queue \n3 for
Delete the data from the Queue\n0 for Exit");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\nEnter the number of data");
scanf("%d",&n);
printf("\nEnter your data");
i=0;
while(i<n)
{
scanf("%d",&data);
42
DATA STRUCTURE LAB
enqueue(data); i++;
}
break;
case 2: print();
break;
case 3 :dequeue();
break;
case 0: break;
default : printf(“\n incorrect
choice”);
}
}while(opt!=0);
return 0
}
OUTPUT
43
DATA STRUCTURE LAB
FLOW CHART
44
DATA STRUCTURE LAB
ALGORITHM
SOURCE CODE
#include<stdio.h>
#include<string.h>
#include<conio.h>
#define substr
45
DATA STRUCTURE LAB
void main()
{
int len;
clrscr();
printf("\nLength of string 1 is :%d",strlen(string1));
%s",strcat(string1,string2));
printf("\n sub string %s found at loc %d",sub,substr(string1,2,3));
replaceSubstring(string2,replace_str,new_str);
printf("\nThe string after replacing : %s\n",string2);
getch();
}
subLen=strlen(sub);
newLen=strlen(new_str);
for(i=0;i<stringLen;i++)
{
flag=0;
start=i;
for(j=0;string[i]==sub[j];j++,i++)
if(j==subLen-1) flag=1;
end=i;
if(flag==0) i-=j;
else
{
for(j=start;j<end;j++)
{
46
DATA STRUCTURE LAB
for(k=start;k<stringLen;k++) string[k]=string[k+1];
stringLen--;
i--;
}
for(j=start;j<start+newLen;j++)
{
for(k=stringLen;k>=j;k--)
string[k+1]=string[k];
string[j]=new_str[j-start];
stringLen++;
i++;
}
}
}
}
OUTPUT
47
DATA STRUCTURE LAB
FLOW CHART
48
DATA STRUCTURE LAB
49
DATA STRUCTURE LAB
ALGORTHIM
SOURCE CODE
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#definemax20
Void push(charelement);
Intpop();
Intpreced(charelement);
Chars[max];
Inttop=0;
Voidmain()
{
int i=0,j=0;
char ch,element;
char infix[max],post[max];
clrscr();
printf("\n\tEnteravalidinfixExpression:");
scanf("%s",&infix);
push('#');
for(i=0;i<strlen(infix);i++)
{
50
DATA STRUCTURE LAB
ch=infix[i];
if(isalnum(ch))
post[j++]=ch;
else
{
if(ch=='(')
push(ch);
else if(ch==')')
{
while(s[top]!='(') post[j++]=pop();
element=pop();
printf("%c",element);
}
else
{
while(preced(s[top])>=preced(ch))
post[j++]=pop();
push(ch);
}
}
}
while(s[top]!='#')
post[j++]=pop();
post[j]=NULL;
printf("\n\tResultantpostfixExpressionis:%s\n",post);
getch();
}
Intpreced(char element)
{
switch(element)
{
case'+':
case'-':return(1);
case'*':
case'/':return(2);
case'^':return(3);
case'(':
case'#':return(0);
}
return(0);
}
void push(char element)
{
s[++top]=element;
51
DATA STRUCTURE LAB
return;
}
int pop()
{
char element; element=s[top--];
return(element);
}
OUTPUT
52
DATA STRUCTURE LAB
Program 11 :
Write a program to evaluate a postfix expression 5 3 +8 2 - *.
FLOW CHART
53
DATA STRUCTURE LAB
ALGORITHM
Step3: a)If the scanned character is an operand, push it into the stack.
b)If the scanned character is an operator, POP 2 operands from stack and perform operation and PUSH the
result back to the stack.
Step5:When the expression is ended, the number in the stack is the final result.
SOURCE CODE
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<ctype.h>
#include<string.h>
#define max 20
int a[max],top=0;
int pop();
void push(int element);
Void main()
{
Char posfix[max],ch;
Int i,op1,op2,res;
clrscr();
printf("\n\t\tProgramtoEvalutepostfixExpression.");
printf("\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\nEnterthepostfixexpression:");
scanf("%s",&posfix);
for(i=0;i<strlen(posfix);i++)
{
ch=posfix[i];
if(isdigit(ch)) push(ch-'0');
else
{
op2=pop(); op1=pop(); switch(ch)
{
Case '+':res=op1+op2;
break;
case '-':res=op1-op2;
break;
case '*':res=op1*op2;
55
DATA STRUCTURE LAB
break;
case '/':res=op1/op2;
break;
case '^':res=pow(op1,op2);
break;
default :printf("\n Enter the valid option:");
}
push(res);
}
}
printf("Resultofaboveexpressionis:%d\n",pop());
getch();
}
void push(int element)
{
a[++top]=element;
}
int pop()
{
int element;
element=a[top--];
return(element);
}
OUTPUT
56
DATA STRUCTURE LAB
FLOW CHART
ALGORITHM
57
DATA STRUCTURE LAB
root=>left = insert(root=>left,key)
Step3: Finally, return the original root pointer to the calling function.
SOURCE CODE
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *left,*right;
};
struct node *root; void insert(int x)
{
struct node *p,*previous,*current;
p=(struct node *)malloc(sizeof(struct node));
if(p==NULL)
{
printf("\n Out of memory");
}
p->data=x;
p->left=NULL;
p->right=NULL;
if(root=NULL)
{
root=p;
return;
}
previous=NULL;
current=root;
while(current!=NULL)
{
previous=current;
if(p->data<current->data) current=current->left;
else
current=current->right;
}
if(p->data<previous->data) previous->left=p;
58
DATA STRUCTURE LAB
else
previous->right=p;
}
voidinorder(struct node *t)
{
if (t!=NULL)
{
inorder(t->left);
printf("\n %5d",t->data);
inorder (t->right);
}
}
void del(int x)
{
int tright=0,tleft=0;
struct node *ptr=root;
struct node *parent=root;
struct node *t1=root;
struct node *temp=root;
while(ptr!=NULL&&ptr->data!=x)
{
parent=ptr;
if (x<ptr->data)
ptr=ptr->left;
else
ptr=ptr->right;
}
if (ptr==NULL)
{
printf("\n Delete element not found");
return ;
}
else if(t1->data==x && (t1->left ==NULL || t1-
>right==NULL))
if(t1->left==NULL) t1=t1->right;
else
t1=t1->left;
else if (ptr->left==NULL)
if (x<parent->data)
parent->left=ptr->right;
else
parent->right=ptr->right;
else if (ptr->right==NULL)
59
DATA STRUCTURE LAB
if (x<parent->data)
parent->left=ptr->left;
else
parent->right=ptr->left;
else
{
temp=ptr;
parent=ptr;
if((ptr->left)>=(ptr->right))
{
ptr=ptr->left;
while(ptr->right!=NULL)
{
tright=1;
parent=ptr;
ptr=ptr->right;
}
temp->data=ptr->data;
if(tright)
parent->right=ptr->left;
else
parent->left=ptr->left;
}
else
{
ptr=ptr->right;
while (ptr->left!=NULL)
{
tleft=1;
parent=ptr;
ptr=ptr->left;
}
temp->data=ptr->data;
if(tleft)
parent->left=ptr->right;
else
parent->right=ptr->right;
}
free(ptr);
}
}
void main()
{
60
DATA STRUCTURE LAB
intop,n,srchno;
root=(struct node *)malloc(sizeof(struct node));
root->data=30;
root->right=root->left=NULL;
clrscr();
do
{
printf("\n 1.Insertion");
printf("\n 2.Deletion");
printf("\n 3.Inorder");
printf("\n 4.Quit");
printf("\n Enter your choice\n");
scanf("%d",&op);
switch (op)
{
case 1: printf("\n Enter the element to insert\n");
scanf("%d",&n);
insert(n);
break;
case 2: printf("\n Enter the element to be deleted\n");
scanf("%d",&srchno);
del(srchno);
break;
case 3: printf("\n The inorder elements are\n");
inorder(root);
getch();
break;
default: exit(0);
}
}while(op<4);
getch();
OUTPUT
1 5
61
DATA STRUCTURE LAB
0 3 9
0123569
62
DATA STRUCTURE LAB
Program 13 : Write a program to create binary search tree with the elements {2,5,1,3,9,0,6} and
perform inorder, preorder and post order traversal.
FLOW CHART
63
DATA STRUCTURE LAB
ALGORITHM
Preorder Traversal:
Step1:Visit or print the root.
Step2:Traverse the left sub tree.
Step3:Traverse the right sub tree.
Postorder Traversal:
Step1:Traverse the left sub tree.
Step2:Traverse the right sub tree.
Step3:Visit or print the root.
Inorder Traversal:
Step1:Traverse the left sub tree.
Step2:Visit or print the root.
Step3:Traverse the right sub tree.
SOURCE CODE
64
DATA STRUCTURE LAB
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
struct node *left;
int info;
struct node *right;
};
void insert(struct node**,int);
void inorder(struct node *);
void postorder(struct node *);
void preorder(struct node *);
void main()
{
struct node *ptr;
int n,i,item,ch;
ptr=NULL;
clrscr();
while(1)
{
printf("\nbinary search tree");
printf("\n1.creation of bst");
printf("\n2.prorder traversal");
printf("\n3.inorder traversal");
printf("\n4.postorder traversal");
printf("\n5.exit");
printf("\n enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("enter number of terms to add to tree");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the element:");
scanf("%d",&item);
65
DATA STRUCTURE LAB
insert(&ptr,item);
}
break;
case 2:printf("preorder traversal");
preorder(ptr);
break;
case 3: printf("inorder traversal");
inorder(ptr);
break;
case 4:printf("postorder traversal");
postorder(ptr);
break;
case 5:exit(0);
}
}
}
void insert(struct node **p,int item)
{
if((*p)==NULL)
{
printf("\n node created\n");
(*p)=malloc(sizeof(struct node));
(*p)->left=NULL;
(*p)->right=NULL;
(*p)->info=item;
return;
}
else
if(item<(*p)->info)
{
printf("\n directed to left link");
insert(&((*p)->left),item);
}
else
if(item==(*p)->info)
{
printf("key found!!value rejected");
return;
66
DATA STRUCTURE LAB
}
else
{
printf("\n directed to right link");
insert(&((*p)->right),item);
}
return;
}
void inorder(struct node *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("\n%d",p->info);
inorder(p->right);
}
else
return;
}
void preorder(struct node *p)
{
if(p!=NULL)
{
printf("\n%d",p->info);
preorder(p->left);
preorder(p->right);
}
else
return;
}
void postorder(struct node *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("\n%d",p->info);
}
67
DATA STRUCTURE LAB
else
return;
}
68
DATA STRUCTURE LAB
Program 14: : Write a program to sort the following elements using heap sort
{9,16,32,8,4,1,5,8,0}.
FLOW CHART
69
DATA STRUCTURE LAB
ALGORITHM
HEAP_SORT(ARR, N)
70
DATA STRUCTURE LAB
Step 3: END
SOURCE CODE
#include<stdio.h>
#include<conio.h>
void heap(int a[],int n)
{
inti,j,k,item;
for(k=1;k<n;k++)
{
item=a[k];
i=k;
j=(i-1)/2;
while(i>0&&item>a[j])
{
a[i]=a[j];
i=j;
j=(i-1)/2;
}
a[i]=item;
}
}
void adjust(int a[],int n)
{
inti,j,item;
j=0;
item=a[j];
i=2*j+1;
while(i<=n-1)
{
if(i+1<=n-1)
if(a[i]<a[i+1])i++; if(item<a[i])
{
71
DATA STRUCTURE LAB
}
a[j]=item;
}
voidheapsort(int a[],int n)
{
inti,temp; heap(a,n); for(i=n-1;i>0;i--)
{
temp=a[0]; a[0]=a[i]; a[i]=temp; adjust(a,i);
}
}
void main()
{
int a[20],i,n,temp; clrscr();
printf("\n\n\tEnter number of elements to sort:");
scanf("%d",&n);
printf("\n\tEnter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
heapsort(a,n);
printf("\n\tThe sorted List:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}
OUTPUT
72
DATA STRUCTURE LAB
73 | P a g e
DATA STRUCTURE LAB
74 | P a g e