0% found this document useful (0 votes)
5 views21 pages

Mcse 102

The document is a lab manual for the Advanced Data Structure and Algorithm course at Gyan Ganga Institute of Technology and Sciences, detailing various programming experiments. It includes a list of experiments focusing on array operations, stack operations, and different sorting and searching algorithms. Each experiment provides code examples and instructions for implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views21 pages

Mcse 102

The document is a lab manual for the Advanced Data Structure and Algorithm course at Gyan Ganga Institute of Technology and Sciences, detailing various programming experiments. It includes a list of experiments focusing on array operations, stack operations, and different sorting and searching algorithms. Each experiment provides code examples and instructions for implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 21

GYAN GANGA INSTITUTE OF TECHNOLOGY AND SCIENCES, JABALPUR

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

LAB MANUAL

Advanced Data Structure and Algorithm

(MCSE-102)

NAME:

KIRAN SINGH VERMA

ENROLLMENT NUMBER:

0206CS24MT06

TEACHER IN CHARGE: - Prof. SHIVENDU DUBEY

DECEMBER 2024

Page 1 of 21
List of Experiment

S.No. Name of Experiment Dates

1. Write a program to perform following operations on


the given array: -
a) Insertion b) Deletion c) Searching d)
Sorting e) Traversing
2. Write a program to perform following operations on
the given Stack: a) Push b) Pop
3. Write a program to perform Linear Search in an
Array.
4. Write a program to perform Binary Search in an
Array.
5. Write a program to perform Bubble Sort in an Array.

6. Write a program to perform Selection Sort in an Array

7. Write a program to perform Insertion Sort in an Array.

8. Write a program to perform Merge Sort in an Array.

9. Write a program to perform Quick Sort in an Array.

10. Write a program to perform Shell Sort in an Array.

Page 2 of 21
Lab Experiment No.1

Write a program to perform following operations on the given array:

a) Insertion b) Deletion c) Searching d) Sorting e) Traversing


#include <stdio.h>

#define MAX_SIZE 100


int a[MAX_SIZE]; // Define an array with a fixed size
int size = 0; // Track the number of elements in the array

void insertion();
void search();
void sort();
void deletion();
void traverse();

int main() {
int ch;
do {
printf("\n Enter 1 for insertion");
printf("\n Enter 2 for search");
printf("\n Enter 3 for sort");
printf("\n Enter 4 for deletion");
printf("\n Enter 5 for traversal");
printf("\n Enter 0 for Exit");
printf("\n Enter choice: ");
scanf("%d", &ch);

switch (ch) {
case 1:
insertion();
break;
case 2:
search();
break;
case 3:
sort();
break;
case 4:
deletion();
break;
case 5:
traverse();
break;

Page 3 of 21
case 0:
printf("\nExit from program");
break;
default:
printf("\n Invalid choice");
}
} while (ch != 0);

return 0;
}

void insertion() {
int n, i;
printf("\n Enter number of elements to be inserted: ");
scanf("%d", &n);

if (size + n > MAX_SIZE) {


printf("\n Not enough space in the array");
return;
}

printf("\n Enter elements: ");


for (i = 0; i < n; i++) {
scanf("%d", &a[size]);
size++;
}
}

void search() {
int s, i, f = 0;
printf("\n Enter element to search: ");
scanf("%d", &s);

for (i = 0; i < size; i++) {


if (a[i] == s) {
printf("\n %d element is found at position %d", s, i + 1);
f = 1;
break;
}
}

if (f == 0) {
printf("\n Element does not exist");
}
}

Page 4 of 21
void traverse() {
int i;
if (size == 0) {
printf("\n The array is empty");
return;
}
printf("\n Array elements: ");
for (i = 0; i < size; i++) {
printf("%d ", a[i]);
}
}

void deletion() {
int t, i, j, f = 0;
printf("\n Enter element to be deleted: ");
scanf("%d", &t);

for (i = 0; i < size; i++) {


if (a[i] == t) {
f = 1;
printf("\n Element found and deleted");
for (j = i; j < size - 1; j++) {
a[j] = a[j + 1];
}
size--;
break;
}
}

if (f == 0) {
printf("\n Element does not exist in the list");
}
}

void sort() {
int i, j, temp;
if (size == 0) {
printf("\n Array is empty, no sorting needed");
return;
}

for (i = 0; i < size - 1; i++) {


for (j = 0; j < size - i - 1; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];

Page 5 of 21
a[j + 1] = temp;
}
}
}
printf("\n Array sorted successfully");
}
Output:

Page 6 of 21
Lab Experiment No.2

Write a program to perform following operations on the given Stack:

a) Push b) Pop
#include <stdio.h>
#define SIZE 5
int stack[SIZE], top = -1;
void push();
void pop();
int main() {
int ch;
do {
printf("\n Enter 1 for push");
printf("\n Enter 2 for pop");
printf("\n Enter 3 for Exit");
printf("\n Enter Operation Choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
printf("\n Exiting program...\n");
break;
default:

Page 7 of 21
printf("\n Invalid Choice");
}
} while (ch != 3);
return 0;
}
void push() {
if (top == SIZE - 1) {
printf("\n Stack is Full, overflow");
} else {
top++;
printf("\n Enter element: ");
scanf("%d", &stack[top]);
printf("\n Element %d pushed onto stack", stack[top]);
}
}
void pop() {
if (top == -1) {
printf("\n Stack is Empty, underflow");
} else {
printf("\n Element %d is popped", stack[top]);
top--;
}
}

Page 8 of 21
Output: -

Lab Experiment No.3

Page 9 of 21
Write a program to perform Linear Search in an Array.
#include<stdio.h>
#include<conio.h>
#define size 10
int a[size];
void main()
{
void linearsearch(int k);
int i,key;
printf("\n Enter 10 Elements in Array");
for(i=0;i<size;i++)
scanf("%d",&a[i]);
printf("\Enter Key Element to search");
scanf("%d",&key);
linearsearch(key);
getch();
}

void linearsearch(int k)
{
int j,f=0;
for(j=0;j<size;j++)
{
if(k==a[j])
{
f=1;
printf("\n Key Element %d is found at %d position in Array",k,j+1);
break;
}
}
if(f==0)
printf("\n Key Element does not exist in Array");
}

Output:

Lab Experiment No.4

Page 10 of 21
Write a program to perform Binary Search in an Array.
#include<stdio.h>
#include<conio.h>
#define size 10
int a[size];
void main()
{
void bsearch(int x[],int k);
int i,key;
printf("\n Enter 10 Elements in Sorted order in Array");
for(i=0;i<size;i++)
scanf("%d",&a[i]);
printf("\Enter Key Element to search");
scanf("%d",&key);
bsearch(a,key);
getch();
}

void bsearch(int x[],int k)


{
int f=0,low=0,high=size-1,mid;
while(low<=high)
{
mid=(low+high)/2;
if(k==x[mid])
{
f=1;
printf("\n Key Element %d is found at %d position in Array",k,mid+1);
break;
}
else
{
if(k<x[mid])
{
high=mid-1;
}
else
{
low=mid+1;
}

}
}

Page 11 of 21
if(f==0)
printf("\n Key Element does not exist in Array");
}

Output:

Lab Experiment No.5

Page 12 of 21
Write a program to perform Bubble Sort in an Array.
#include <stdio.h>
#include <conio.h>
#define size 10
void main()
{
void bubblesort(int a[]);
int array[size],i;
printf("\n enter 10 Elements");
for(i=0;i<size;i++)
scanf("%d",&array[i]);
bubblesort(array);
getch();
}

void bubblesort(int a[])


{
int i,j,temp;
for(i=0;i<size;i++)
{
for(j=0;j<size-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}}}
printf("\n Sorted Order Array");
for(i=0;i<size;i++)
printf("\n %d",a[i]);
}

Output:

Lab
Experiment No.6
Page 13 of 21
Write a program to perform Selection Sort in an Array.
#include <stdio.h>
#include <conio.h>
#define size 10
void main()
{
void selectionsort(int a[]);
int array[size],i;
printf("\n enter 10 Elements");
for(i=0;i<size;i++)
scanf("%d",&array[i]);
bubblesort(array);
getch();
}

void selectionsort(int a[])


{
int i,j,temp;
for(i=0;i<size-1;i++)
{
for(j=i+1;j<size;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}}}
printf("\n Sorted Order Array");
for(i=0;i<size;i++)
printf("\n %d",a[i]);}

Output:

Lab
Experiment No.7
Page 14 of 21
Write a program to perform Insertion Sort in an Array.
#include <stdio.h>
#include <conio.h>
#define size 10
void main()
{
void insertionsort(int a[]);
int array[size],i;
printf("\n enter 10 Elements");
for(i=0;i<size;i++)
scanf("%d",&array[i]);
insertionsort(array);
getch();
}
void insertionsort(int arr[])
{
int i, key, j;
for (i = 1; i < size; i++)
{
key = arr[i];
j = i-1;
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
printf("\n Sorted Order Array");
for(i=0;i<size;i++)
printf("\n %d",a[i]);}

Output:

Lab Experiment
No.8

Page 15 of 21
Write a program to perform Merge Sort in an Array.

#include<stdio.h>
#include<conio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);

void main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");

for(i=0;i<n;i++)
scanf("%d",&a[i]);

mergesort(a,0,n-1);
printf("\nSorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}

void mergesort(int a[],int i,int j)


{
int mid;

if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid); //left recursion
mergesort(a,mid+1,j); //right recursion
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]; //array used for merging
int i,j,k;
i=i1; //beginning of the first list

Page 16 of 21
j=i2; //beginning of the second list
k=0;

while(i<=j1 && j<=j2) //while elements in both lists


{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}

while(i<=j1) //copy remaining elements of the first list


temp[k++]=a[i++];

while(j<=j2) //copy remaining elements of the second list


temp[k++]=a[j++];

//Transfer elements from temp[] back to a[]


for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}

Output:

Lab Experiment No.9


Page 17 of 21
Write a program to perform Quick Sort in an Array.

#include<stdio.h>
void main()
{
void quicksort(int number[],int first,int last);
int i, count, no[25];
printf("How many elements are you going to enter?: ");
scanf("%d",&count);

printf("Enter %d elements: ", count);


for(i=0;i<count;i++)
scanf("%d",&no[i]);

quicksort(no,0,count-1);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",no[i]);

void quicksort(int number[],int first,int last)


{
int i, j, pivot, temp;

if(first<last)
{
pivot=first;
i=first;
j=last;

while(i<j)
{
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j)
{
temp=number[i];

Page 18 of 21
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}

Output:

Lab Experiment No.10

Write a program to perform Shell Sort in an Array.


Page 19 of 21
#include<stdio.h>
#include<conio.h>
int list[]={0};
void main()
{
int i,j,k,d,temp,n;
printf("\nenter the number of elements : ");
scanf("%d",&n);
printf("\nenter elements : ");
for(i=0;i<n;i++)
{
scanf("%d",&list[i]);
}
d=n/2;
for(i=d;i>=1;i=i/2)
{
for(j=1;j<n;j++)
{
temp=list[j];
k=j-1;
while(k>=0&&temp<list[k])
{
list[k+1]=list[k];
k=k-1;
}
list[k+1]=temp;
}
}
printf("\nsorted list : ");
for(i=0;i<n;i++)
{
printf("\n %d",list[i]);
}
getch();
}

Output:

Page 20 of 21
Page 21 of 21

You might also like