Final Ds Lab File
Final Ds Lab File
Submitted By:
Student Name: ISHAN KHATRI
Enrolment no: 00217707223
Branch & Section: CSE A
Submitted To:
Dr. Nivedita Palia
1
VIDIT GHAI CSE-B vips-tc
2
VIDIT GHAI CSE-B vips-tc
3
VIDIT GHAI CSE-B vips-tc
EXPERIMENT 01
Problem Statement:
4
VIDIT GHAI CSE-B vips-tc
Perform Linear Search and Binary Search on an array by passing the array to a
function and then returning the position of the element from the function else
return -1 if the element is not found.
Programme code:
#include<stdio.h>
int linearsearch(int arr[], int n, int num){
int i=0;
for(i; i<n; i++){
if(arr[i]==num){
return i;
}
}
return -1;
}
int binarysearch(int arr[], int start, int end, int num){
while(start<=end){
int mid=(start+end-1)/2;
if(arr[mid]==num){
return mid;
}
if(arr[mid]<num){
start=mid+1;
}else{
end=mid-1;
}
}
return -1;
}
5
VIDIT GHAI CSE-B vips-tc
int main(){
int n;
int i=0;
printf("Enter the number of elements in sorted array: ");
scanf("%d",&n);
int arr[n];
for(i; i<n; i++){
printf("Enter the %d element of array: ",i+1);
scanf("%d",&arr[i]);
}
int num;
printf("Enter the element for searching:\n ");
scanf("%d",&num);
int result=binarysearch(&arr[0], 0, n-1, num);
printf("Searched element by linear search:\n ");
if(result==-1){
printf("Element not found!");
}else{
printf("Element found at %d position",result+1);
}
printf("\n");
result=linearsearch(&arr[0], n, num);
printf("Searched element by linear search:\n ");
if(result==-1){
printf("Element not found!");
}else{
printf("Element found at %d position",result+1);
6
VIDIT GHAI CSE-B vips-tc
}
return 0;
}
Algorithm:
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
7
VIDIT GHAI CSE-B vips-tc
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
Output:
Learning Outcome:
________________________________________________________
________________________________________________________
________________________________________________________
EXPERIMENT 02
Problem Statement:
8
VIDIT GHAI CSE-B vips-tc
Programme Code:
#include <stdio.h>
#include <stdlib.h>
// Traversal of Array.
printf("Entered array : ");
for(i=0; i<n; i++){
printf("%d\t", array[i]);
}
// Reversal of Array
reverse(n, array);
return 0;
}
Algorithm:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
11
VIDIT GHAI CSE-B vips-tc
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
Output:
12
VIDIT GHAI CSE-B vips-tc
Learning Outcome:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
Problem Statement:
Merging 2 arrays.
Programme Code:
#include<stdio.h>
void mergeArrays(int arr1[], int arr2[], int n1, int n2, int arr3[]){
int i = 0, j = 0, k = 0;
while (i<n1 && j <n2){
if (arr1[i] < arr2[j]){
arr3[k++] = arr1[i++];
13
VIDIT GHAI CSE-B vips-tc
}else{
arr3[k++] = arr2[j++];
}
}
while (i < n1){
arr3[k++] = arr1[i++];
}
while (j < n2){
arr3[k++] = arr2[j++];
}
printf("Arrays after merging: \t");
for (i=0; i < n1+n2; i++){
printf("%d\t",arr3[i]);
}
}
int main(){
int n,k,i,j;
printf("Enter the number of elements in first array : ");
scanf("%d",&n);
printf("Enter the number of elements in second array : ");
scanf("%d",&k);
int arr1[n],arr2[k];
for(i; i<n; i++){
printf("Enter the %d element of first array : ",i+1);
scanf("%d",&arr1[i]);
}
for(i=0; i<k; i++){
14
VIDIT GHAI CSE-B vips-tc
Algorithm:
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
15
VIDIT GHAI CSE-B vips-tc
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
Output:
Learning Outcome:
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
EXPERIMENT 03
16
VIDIT GHAI CSE-B vips-tc
Problem Statement:
Create a linked list with nodes having information about a student and
perform:
Reversal of that linked list.
Insert a new node at specified position.
Delete of a node with the roll no. of specified student.
Programme Code:
#include <stdio.h>
#include <stdlib.h>
struct node{
char name[20];
int rollno;
struct node * next;
};
17
VIDIT GHAI CSE-B vips-tc
18
VIDIT GHAI CSE-B vips-tc
int main() {
int pos,roll;
struct node* head;
struct node* s1;
struct node* s2;
head = (struct node *)malloc (sizeof(struct node));
s1 = (struct node *)malloc (sizeof(struct node));
s2 = (struct node *)malloc (sizeof(struct node));
printf("Enter the name of first student: ");
scanf("%s",&head->name);
head->rollno=1;
19
VIDIT GHAI CSE-B vips-tc
head->next=s1;
printf("Enter the name of second student: ");
scanf("%s",&s1->name);
s1->rollno=2;
s1->next=s2;
printf("Enter the name of third student: ");
scanf("%s",&s2->name);
s2->rollno=3;
s2->next=NULL;
printf("Before reversal\n");
linkedlisttraversal(head);
printf("After reversal\n");
head=linkedlistreversal(head);
linkedlisttraversal(head);
printf("Insertion\n");
printf("Enter index number where you want to insert details of 4th student: ");
scanf("%d",&pos);
head=insertAtIndex(head,pos);
linkedlisttraversal(head);
printf("Deletion\n");
printf("Enter roll number of the student,to remove his/her information: ");
scanf("%d",&roll);
head=deleteAtValue(head,roll);
linkedlisttraversal(head);
return 0;
}
20
VIDIT GHAI CSE-B vips-tc
Algorithm:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
21
VIDIT GHAI CSE-B vips-tc
Output:
Learning Outcome:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
EXPERIMENT 04
Problem Statement:
22
VIDIT GHAI CSE-B vips-tc
23
VIDIT GHAI CSE-B vips-tc
while(temp!=NULL){
printf("%d\t",temp->data);
temp=temp->next;
}
printf("\n");
}
};
void peek(struct node* top){
printf("Element at the top is: %d",top->data);
};
struct node* pop(struct node* top){
struct node* temp=top;
if (temp==NULL){
printf("Underflow\n");
}else{
top=top->next;
free(temp);
return top;
}
};
int main(){
struct node *top,*node1,*node2;
top=NULL;
printf("Pushing Elements into stack...\n");
top=push(top);
top=push(top);
top=push(top);
24
VIDIT GHAI CSE-B vips-tc
top=push(top);
top=push(top);
printf("Displaying stack...\n");
display(top);
printf("Popping stack...\n");
top=pop(top);
printf("Displaying stack after popping...\n");
display(top);
peek(top);
return 0;
}
Algorithm:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
25
VIDIT GHAI CSE-B vips-tc
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
Output:
Learning Outcome:
26
VIDIT GHAI CSE-B vips-tc
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
EXPERIMENT 05
27
VIDIT GHAI CSE-B vips-tc
Problem Statement:
Create doubly linked list with nodes having information about an employee and
perform insertion at front of doubly linked list and perform deletion at end of
that doubly linked list.
Programme Code:
#include<stdio.h>
#include<stdlib.h>
struct employee{
struct employee *prev;
int id;
char name[20];
float sal;
struct employee *next;
};
28
VIDIT GHAI CSE-B vips-tc
29
VIDIT GHAI CSE-B vips-tc
}else{
if ((*lptr)->prev != NULL) {
Emp *temp = *lptr;
*lptr = (*lptr)->prev;
free(temp);
(*lptr)->next = NULL;
printf("Entry deleted from last!!!\n");
} else {
free(*lptr);
*lptr = NULL;
*sptr = NULL;
printf("Entry deleted from last!!!\n");
}
}
}
int main(){
Emp *start = NULL;
Emp *last = NULL;
printll(start);
insert(&start, &last);
insert(&start, &last);
insert(&start, &last);
printll(start);
delete(&last, &start);
printll(start);
delete(&last, &start);
delete(&last, &start);
delete(&last, &start);
printll(start);
return 0;
}
Algorithm:
30
VIDIT GHAI CSE-B vips-tc
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
31
VIDIT GHAI CSE-B vips-tc
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
Output:
Learning Outcome:
32
VIDIT GHAI CSE-B vips-tc
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
EXPERIMENT 06
33
VIDIT GHAI CSE-B vips-tc
Problem Statement:
Implement two stacks using in a single array.
Programme Code:
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,top1,top2,ch=1,a,i,arr[100];
printf("Enter size of array you want to use\n");
scanf("%d",&n);
top1=-1;
top2=n;
while(ch!=0){
printf("What do u want to do?\n");
printf("Enter 1 to Push element in stack 1\n");
printf("Enter 2 to Push element in stack 2\n");
printf("Enter 3 to Pop element from stack 1\n");
printf("Enter 4 to Pop element from stack 2\n");
printf("Enter 5 to Display stack 1\n");
printf("Enter 6 to Display stack 2\n");
printf("Enter 0 to EXIT\n");
scanf("%d",&ch);
switch(ch){
case 1:
printf("Enter the element\n");
scanf("%d",&a);
if(top1!=(top2-1))
arr[++top1]=a;
else
printf("Overflow\n");
break;
case 2:
printf("Enter the element\n");
scanf("%d",&a);
if(top2!=(top1+1))
arr[--top2]=a;
else
printf("Overflow\n");
34
VIDIT GHAI CSE-B vips-tc
break;
case 3:
if(top1==-1)
printf("Stack1 is empty\n");
else{
a=arr[top1--];
printf("%d\n",a);
}
break;
case 4:
if(top2==n)
printf("Stack2 is empty\n");
else{
a=arr[top2++];
printf("%d\n",a);
}
break;
case 5:
if(top1==-1)
printf("Stack1 is empty\n");
else{
printf("Stack1 is-->>\n");
for(i=0;i<=top1;i++)
printf("%d ",arr[i]);
printf("\n");
}
break;
case 6:
if(top2==n)
printf("Stack2 is empty\n");
else{
printf("Stack2 is-->>\n");
for(i=(n-1);i>=top2;i--)
printf("%d ",arr[i]);
printf("\n");
}
break;
case 0:
35
VIDIT GHAI CSE-B vips-tc
break;
}
}
}
Algorithm:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
36
VIDIT GHAI CSE-B vips-tc
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
Output:
37
VIDIT GHAI CSE-B vips-tc
Learning outcome:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
38
VIDIT GHAI CSE-B vips-tc
EXPERIMENT 07
Problem Statement:
Create a linear queue using linked list and implement different operations such
as insertion, deletion and display the queue elements.
Programme Code:
#include <stdio.h>
#include <malloc.h>
struct node{
int data;
struct node *next;
};
struct queue{
struct node *front;
struct node *rear;
};
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("\nQUEUE IS EMPTY");
39
VIDIT GHAI CSE-B vips-tc
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("\nUNDERFLOW");
else{
q -> front = q -> front -> next;
printf("\nThe value being deleted is : %d", ptr ->
data);
free(ptr);
}
return q;
}
int main(){
int val, option;
struct queue *q;
q -> rear = NULL;
q -> front = NULL;
do{
printf("\n*****MAIN MENU*****");
printf("\nEnter 1 for INSERTION");
printf("\nEnter 2 for DELETION");
printf("\nEnter 3 to DISPLAY");
printf("\nEnter 4 to EXIT");
printf("\nEnter your option : ");
scanf("%d", &option);
switch(option){
case 1:
40
VIDIT GHAI CSE-B vips-tc
Algorithm:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
41
VIDIT GHAI CSE-B vips-tc
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
42
VIDIT GHAI CSE-B vips-tc
Output:
Learning Outcome:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
43
VIDIT GHAI CSE-B vips-tc
EXPERIMENT 08
Problem Statement:
Create a binary tree and perform tree traversal using recursion.
Programme Code:
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct node{
int data;
struct node *left;
struct node *right;
};
void preorderTraversal(struct node *root){
if(root != NULL){
printf("\t%d\t", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}
void inorderTraversal(struct node *root){
if(root != NULL){
inorderTraversal(root->left);
printf("\t%d\t", root->data);
inorderTraversal(root->right);
}
}
void postorderTraversal(struct node *root){
if(root != NULL){
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("\t%d\t", root->data);
}
}
int main(){
struct node *root , *one ,*two ,*three , *four ,*seven;
root = (struct node *)malloc(sizeof(struct node));
44
VIDIT GHAI CSE-B vips-tc
45
VIDIT GHAI CSE-B vips-tc
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
46
VIDIT GHAI CSE-B vips-tc
Output:
Learning Outcome:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
47
VIDIT GHAI CSE-B vips-tc
EXPERIMENT 09
Problem statement:
Write a program to evaluate
3. Prefix Expression
4. Postfix Expression
Programme Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#define MAX 100 // maximum size of the stack
// stack structure
struct stack {
int top;
int items[MAX];
};
// push operation
void push(struct stack *s, int value) {
if (s->top == MAX - 1) {
printf("Stack is full\n");
exit(1);
}else {
s->top++;
s->items[s->top] = value;
}
}
// pop operation
int pop(struct stack *s) {
if (s->top == -1) {
printf("Stack is empty\n");
exit(1);
48
VIDIT GHAI CSE-B vips-tc
}else {
int value = s->items[s->top];
s->top--;
return value;
}
}
49
VIDIT GHAI CSE-B vips-tc
break;
}
push(&s, result);
}else if (c == ' ') {
}else{
printf("Invalid character\n");
exit(1);
}
i++;
}
return pop(&s);
}
50
VIDIT GHAI CSE-B vips-tc
}
else {
printf("Invalid character\n");
exit(1);
}
i--;
}
return pop(&s);
}
int main(){
int val, ch;
char exp[100];
do{
printf("\n****MENU****");
printf("\n1.Evaluate Postfix Expression");
printf("\n2.Evaluate Prefix Expression");
printf("\n3.Exit");
printf("\nEnter choice : ");
scanf(" %d",&ch);
switch(ch){
case 1:
printf("\nEnter any postfix expression : ");
scanf(" %[^\n]",&exp);
val = evaluatePostfixExp(exp);
51
VIDIT GHAI CSE-B vips-tc
case 2:
printf("\nEnter any prefix expression : ");
scanf(" %[^\n]",&exp);
val = evaluatePrefixExp(exp);
printf("Value of the prefix expression =
%d",val);
break;
case 3:
break;
default:
printf("\nINVALID CHOICE");
}
printf("\n");
}while(ch!=3);
return 0;
}
Algorithm:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
52
VIDIT GHAI CSE-B vips-tc
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
53
VIDIT GHAI CSE-B vips-tc
Output:
Learning Outcome:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
54
VIDIT GHAI CSE-B vips-tc
EXPERIMENT 10
Problem Statement:
Implement selection sort, bubble sort, insertion sort and heap sort using array as
a data structure.
Programme Code:
#include<stdio.h>
#include<stdlib.h>
54
VIDIT GHAI CSE-B vips-tc
if (largest != index) {
swap(&arr[index], &arr[largest]);
heapify(arr, size, largest);
}
55
VIDIT GHAI CSE-B vips-tc
void printchoice(){
printf("\nSORTING METHODS AVAILABLE:\n");
printf("1.Bubble sort\n2.Insertion sort\n3.Selection
sort\n4.Heap sort\n0.Exit\n");
}
int main(){
56
VIDIT GHAI CSE-B vips-tc
int choice;
int *array;
int size;
do{
printchoice();
printf("Enter your choice : ");
scanf("%d",&choice);
switch (choice){
case 1:
array=createArray(&size);
printf("Initial array:\n");
printArray(array,size);
Bubblesort(array,size);
printf("Sorted array:\n");
printArray(array,size);
free(array);
break;
case 2:
array=createArray(&size);
printf("Initial array:\n");
printArray(array,size);
Insertionsort(array,size);
printf("Sorted array:\n");
printArray(array,size);
free(array);
break;
case 3:
array=createArray(&size);
printf("Initial array:\n");
printArray(array,size);
Selectionsort(array,size);
printf("Sorted array:\n");
printArray(array,size);
free(array);
57
VIDIT GHAI CSE-B vips-tc
break;
case 4:
array=createArray(&size);
printf("Initial array:\n");
printArray(array,size);
Heapsort(array,size);
printf("Sorted array:\n");
printArray(array,size);
free(array);
break;
case 0:
break;
default:
printf("Invalid choice...\n");
break;
}
}while(choice!=0);
return 0;
}
Algorithm:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
58
VIDIT GHAI CSE-B vips-tc
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
59
VIDIT GHAI CSE-B vips-tc
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
60
VIDIT GHAI CSE-B vips-tc
Output:
Learning Outcome:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
61
VIDIT GHAI CSE-B vips-tc
62