0% found this document useful (0 votes)
27 views36 pages

DSLabpart A

The document contains multiple C programs demonstrating various algorithms and data structures, including selection sort, insertion sort, Tower of Hanoi, merge sort, quick sort, binary search, stack operations, and queue operations. Each program includes code snippets, explanations, and sample outputs. The programs illustrate fundamental concepts in computer science and programming.

Uploaded by

Zaharan Zaharan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views36 pages

DSLabpart A

The document contains multiple C programs demonstrating various algorithms and data structures, including selection sort, insertion sort, Tower of Hanoi, merge sort, quick sort, binary search, stack operations, and queue operations. Each program includes code snippets, explanations, and sample outputs. The programs illustrate fundamental concepts in computer science and programming.

Uploaded by

Zaharan Zaharan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Program to sort the given list using selection sort


technique.

#include<stdio.h>
#include<conio.h>
#define size 6
int min(int a[],int k,int n)
{
int loc=k;
int j;
for(j=k+1;j<=n;j++)
{
if(a[j]<a[loc])
{
loc=j;
}
}
return loc;
}

void selection (int a[],int n)


{
int loc,temp, k;
for(k=0;k<n-1;k++)
{
loc=min(a,k,n-1);
temp=a[k];
a[k]=a[loc];
a[loc]=temp;
}
}

void printarray(int a[],int n)


{
int i;
for( i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
int main()
{
int a[size]={45,90,80,20,30,10};
clrscr();
printf("given array: ");
printarray(a,size);
selection(a,size);
printf("sorted array: ");
printarray(a,size);
getch();
return 0;
}

OUTPUT:

given array: 45 90 80 20 30 10
sorted array: 10 20 30 45 80 90
2. Program to sort the given list using insertion sort
technique

#include<stdio.h>
#include<conio.h>
#define size 5
void insertion(int a[],int n)
{
int temp,ptr;
int k;
for( k=1;k<n;k++)
{
temp=a[k];
ptr=k-1;
while (ptr>=0 && temp<a[ptr])
{
a[ptr+1]=a[ptr];
ptr--;
}
a[ptr+1]=temp;
}

}
void printarray(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
int main()
{
int a[size]={22,10,32,50,20};
clrscr();
printf("given array: ");
printarray(a,size);
insertion(a,size);
printf("sorted array: ");
printarray(a,size);
getch();
return 0;

}
OUTPUT:
given array: 22 10 32 50 20
sorted array: 10 20 22 32 50
3. Program to solve Tower of Hanoi using Recursion

#include<stdio.h>
#include<conio.h>

void tower(int n,char beg,char aux,char end)


{
if(n==1)
{
printf("move disk 1 from %c to %c\n",beg,end);
return;
}
tower(n-1,beg,end,aux);
printf("move disk %d from %c to %c\n",n,beg,end);
tower(n-1,aux,beg,end);
}
int main()
{

int n;
clrscr();

printf("enter the number of disks:\n");


scanf("%d",&n);
if(n<1)
{
printf("invalid value: number of disks should be
atleast 1\n");
getch();
return 0;

}
printf("stotal number of moves required for
n=%d\n\n",n);
tower(n,'B','A','E');
getch();
return 0;

OUTPUT:
enter the number of disks:
3
total number of moves required for n=3

move disk 1 from B to E


move disk 2 from B to A
move disk 1 from E to A
move disk 3 from B to E
move disk 1 from A to B
move disk 2 from A to E
move disk 1 from B to E
4. Program to sort the given list using merge sort
technique.

#include<stdio.h>
#include<conio.h>
#define n 14
void merge (int a[],int l,int lb,int b[],int l2,int lb2,int c[],int
lbc)
{
int i,j,k,end1,end2;
i=lb;
j=lb2;
k=lbc;
end1=lb+l;
end2=lb2+l2;
while(i<end1 && j<end2)
{
if(a[i]<=b[j])
c[k++]=a[i++];
else
c[k++]=b[j++];
}
while(i<end1)
c[k++]=a[i++];
while (j<end2)
c[k++]=b[j++];
}
void mergepass(int a[],int l,int b[])
{
int q,s,r,j;
int lb;
q=n/(2*l);
s=2*l*q;
r=n-s;
for(j=0;j<q;j++)
{
lb=j*(2*l);
merge(a,l,lb,a,l,lb+l,b,lb);
}
if(r<=l)
{
for(j=0;j<r;j++)
b[s+j]=a[s+j];
}
else
{
merge(a,l,s,a,r-l,s+l,b,s);
}
}
void mergesort(int a[])
{
int b[n],l;
l=1;
while(l<n)
{
mergepass(a,l,b);
mergepass(b,2*l,a);
l*=4;
}
}
void printarray(int a[])
{
int i;
for( i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
}

int main()
{
int a[n]={89,90,75,45,20,10,3,5,2,100,80,58,30,9};

clrscr();
printf("given array :\n");
printarray(a);
mergesort(a);
printf("sorted array:\n");
printarray(a);
getch();
return 0;
}

OUTPUT:
given array :
89 90 75 45 20 10 3 5 2 100 80 58 30 9
sorted array:
2 3 5 9 10 20 30 45 58 75 80 89 90 100
5. Program to sort the given list using quick sort technique

#include<stdio.h>
#include<conio.h>
#define size 7
void swap(int*x,int*y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
int partition(int a[],int p,int r)
{
int pivot,i,j;
pivot =a[r];
i=p-1;

for(j=p;j<r;j++)
{
if(a[j]<=pivot)
{
i++;
swap(&a[i],&a[j]);
}
}
swap(&a[i+1],&a[r]);

return(i+1);
}
void quicksort(int a[],int p,int r)
{
if(p<r)
{
int q=partition(a,p,r);
quicksort(a,p,q-1);
quicksort(a,q+1,r);
}
}
void printarray(int a[], int n)

{
int i;
for( i=0;i<n;i++)
{
printf("%d \t",a[i]);
}
printf("\n");
}

int main()
{
int a[]={10,40,80,60,20,50,30};
clrscr();
printf("given array :\n");
printarray(a,size);
quicksort(a,0,size-1);
printf("sorted array :\n");
printarray(a,size);
getch();
return 0;
}

OUTPUT:
given array :
10 40 80 60 20 50 30
sorted array :
10 20 30 40 50 60 80
6. Program to search an element using recursive binary
search technique.

#include<stdio.h>
#include<conio.h>
int bs(int a[],int beg,int end,int item)
{
int mid;
if(beg<=end)
{
mid=(beg+end)/2;
if(a[mid]==item)
return(mid);
if(item<a[mid])
{
end=mid-1;
return bs(a,beg,end ,item);
}
else
{
beg=mid+1;
return bs(a,beg,end,item);
}
}
return(-1);
}
void main()
{
int a[100],i,n,loc,item;
clrscr();
printf("enter the sizes of the array:\n");
scanf("%d",&n);
printf("enter the array elements :\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the elements to be searched :\n");
scanf("%d",&item);
loc=bs(a,0,n-1,item);
if(loc==-1)
printf("element not found");
else
printf("elements found in the location %d",loc+1);
getch();
}
OUTPUT:
enter the sizes of the array:
5
enter the array elements :
25
56
78
92
100
enter the elements to be searched :
92
elements found in the location 4

enter the sizes of the array:


5
enter the array elements :
14
22
56
65
76
enter the elements to be searched :
44
element not found
7. Program to implement Stack operations using arrays.

#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
#define SIZE 5// Maximum size of stack

int stack[SIZE]; // Stack array


int top = -1; // Indicates the top of the stack

// Function to push an element


void push(int value)
{
if (top >= SIZE - 1)
{
printf("Stack Overflow! Cannot push %d\n",
value);
}
else
{
top++;
stack[top] = value;
printf("Pushed %d into stack\n", value);
}
}
// Function to pop an element
void pop()
{
if (top == -1)
{
printf("Stack Underflow! Cannot pop\n");
}
else
{
printf("Popped %d from stack\n", stack[top]);
top--;
}
}

// Function to display stack elements


void display()
{
int i;
if (top == -1)
{
printf("Stack is empty\n");
}
else
{
printf("Stack contents (top to
bottom):\n");
for ( i = top; i >= 0; i--)
{
printf("%d\n", stack[i]);
}
}
}

int main()
{
int choice, value;
clrscr();
while (1)
{
printf("\n--- Stack Menu ---\n");
printf("1. Push\n2. Pop\n3. Display\n4.
Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0); // Exit the program
default:
printf("Invalid choice! Try
again.\n");
}
}

return 0;
}

OUTPUT:

--- Stack Menu ---


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter value to push: 2
Pushed 2 into stack

--- Stack Menu ---


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter value to push: 45
Pushed 45 into stack

--- Stack Menu ---


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter value to push: 55
Pushed 55 into stack

--- Stack Menu ---


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter value to push: 32
Pushed 32 into stack

--- Stack Menu ---


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter value to push: 44
Pushed 44 into stack

--- Stack Menu ---


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
Stack contents (top to bottom):
44
32
55
45
2

--- Stack Menu ---


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter value to push: 34
Stack Overflow! Cannot push 34

--- Stack Menu ---


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2
Popped 44 from stack

--- Stack Menu ---


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2
Popped 32 from stack
--- Stack Menu ---
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2
Popped 55 from stack

--- Stack Menu ---


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2
Popped 45 from stack

--- Stack Menu ---


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2
Popped 2 from stack

--- Stack Menu ---


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2
Stack Underflow! Cannot pop

--- Stack Menu ---


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
Stack is empty

--- Stack Menu ---


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 4
8. Program to implement Queue operations using arrays.

#include<stdio.h>
#include<conio.h>
#define size 6
int queue[size],front=-1,rear=-1;
void enqueue(int value)
{
if(rear==size-1)
printf("queue overflow \n");
else
{
if(front ==-1)front=0;
queue[++rear]=value;
}
}
void dequeue()
{
if(front==-1||front>rear)
printf("queue underflow \n");
else
printf("deleted element is: %d\n",queue[front++]);
}

void display()
{
if(front ==-1||front>rear)
printf("queue is empty \n");
else
{
int i;
for(i=front;i<=rear;i++)
printf("%d \n",queue[i]);
printf("\n");
}
}
int main()
{
int choice,value;
clrscr();
while(1)
{
printf("\[Link] \[Link] \[Link]
\[Link] \n");
printf("enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("enter the value: ");
scanf("%d",&value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
return 0;
}
}
}

OUTPUT:

[Link]
[Link]
[Link]
[Link]
enter your choice: 1
enter the value: 23

[Link]
[Link]
[Link]
[Link]
enter your choice: 1
enter the value: 33

[Link]
[Link]
[Link]
[Link]
enter your choice: 1
enter the value: 45

[Link]
[Link]
[Link]
[Link]
enter your choice: 1
enter the value: 11

[Link]
[Link]
[Link]
[Link]
enter your choice: 1
enter the value: 22

[Link]
[Link]
[Link]
[Link]
enter your choice: 1
enter the value: 21

[Link]
[Link]
[Link]
[Link]
enter your choice: 1
enter the value: 20
queue overflow

[Link]
[Link]
[Link]
[Link]
enter your choice: 3
23
33
45
11
22
21
[Link]
[Link]
[Link]
[Link]
enter your choice: 2
deleted element is: 23

[Link]
[Link]
[Link]
[Link]
enter your choice: 2
deleted element is: 33

[Link]
[Link]
[Link]
[Link]
enter your choice: 2
deleted element is: 45

[Link]
[Link]
[Link]
[Link]
enter your choice: 2
deleted element is: 11

[Link]
[Link]
[Link]
[Link]
enter your choice: 2
deleted element is: 22
[Link]
[Link]
[Link]
[Link]
enter your choice: 2
deleted element is: 21

[Link]
[Link]
[Link]
[Link]
enter your choice: 2
queue underflow

[Link]
[Link]
[Link]
[Link]
enter your choice: 3
queue is empty

[Link]
[Link]
[Link]
[Link]
enter your choice: 4

You might also like