Data Structures and Algorithms Lab Record Writeup
Data Structures and Algorithms Lab Record Writeup
Data Structures and Algorithms Lab Record Writeup
Recursion:
Recursion is an amazing technique with the help of which we can reduce the
will occur.
(1) Write a Program to find GCD of two given integers using Recursive
Function:
#include<stdio.h>
if (b==0)
return a;
else
int main()
int a, b, g;
printf(“Enter two numbers:”);
g = gcd(a, b);
return 0;
OUTPUT:
GCD is 2
function.
#include<stdio.h>
int fib(int n)
{
if(n==1 || n==2)
return (n-1);
else
return fib(n-1) + fib(n-2);
}
int main()
{
int n, t;
printf(“Enter a number:”);
scanf(“%d”, &n);
t = fib(n);
printf(“Nth term is %d”, t);
return 0;
}
OUTPUT:
Enter a number: 8
Nth term is 13
(3) Write a program for Towers of Hanoi using recursion: N disks are
Peg.
#include<stdio.h>
void toh(char src, char aux, char dst, int n)
{
if(n == 0)
return;
else
{
toh (src, dst, aux, n-1);
printf (“Move disc %d from %c to %c\n", n, src, dst);
toh (aux, src, dst, n-1);
}
}
int main()
{
int n;
printf("Enter number of disks:");
scanf("%d", &n);
toh('A', 'B', 'C', n);
return 0;
}
OUTPUT:
Enter number of disks: 3
Searching:
The process of finding the desired information from the set of items stored in
Linear Search
If any element is found equal to the key, the search is successful and
If no element is found equal to the key, the search yields “No match
found”.
#include<stdio.h>
{
int i;
if(key == a[i])
return (i+1);
return -1;
int main()
scanf(“%d”, &n);
scanf(“%d”, &a[i]);
scanf(“%d”, &key);
if(pos != -1)
else
return 0;
OUTPUT:
Enter 5 elements: 5 2 4 1 3
Binary Search
In this algorithm,
Divide the search space into two halves by finding the middle index
“mid”.
Compare the middle element of the search space with the key.
If the key is not found at middle element, choose which half will be
used as the next search space.
If the key is smaller than the middle element, then the left side
is used for next search.
If the key is larger than the middle element, then the right side
is used for next search.
This process is continued until the key is found or the total search
space is exhausted.
#include<stdio.h>
low = 0;
high = n-1;
while(low <=high)
mid = (low+high)/2;
return -1;
int main()
scanf(“%d”, &n);
scanf(“%d”, &a[i]);
scanf(“%d”, &key);
if(pos != -1)
else
return 0;
OUTPUT:
Enter 5 elements: 1 2 3 4 5
In this algorithm,
traverse from left and compare adjacent elements and the higher one is
In this way, the largest element is moved to the rightmost end at first.
This process is then continued to find the second largest and place it
#include<stdio.h>
int i, j, t;
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
int main()
int i, n, a[30];
scanf(“%d”, &n);
scanf(“%d”, &a[i]);
bubblesort(a, n);
scanf(“%d ”, a[i]);
return 0;
OUTPUT:
Enter 5 elements: 3 1 5 4 2
repeatedly selecting the smallest (or largest) element from the unsorted
portion of the list and moving it to the sorted portion of the list.
The algorithm repeatedly selects the smallest (or largest) element from
the unsorted portion of the list and swaps it with the first element of
the unsorted part. This process is repeated for the remaining unsorted
#include<stdio.h>
void selectionsort(int a[], int n)
{
int i, j, t, min;
OUTPUT:
Enter the range: 5
Enter 5 elements: 3 1 5 4 2
Elements after sorting: 1 2 3 4 5
way you sort playing cards in your hands. The array is virtually split
into a sorted and an unsorted part. Values from the unsorted part are
To sort an array of size N in ascending order iterate over the array and
compare the current element (key) to its predecessor, if the key element
the greater elements one position up to make space for the swapped
element.
#include<stdio.h>
void insertionsort()
int i, j;
t = a[j];
a[j] = a[j-1];
a[j-1] = t;
}
int main()
int i, n, a[30];
scanf(“%d”, &n);
scanf(“%d”, &a[i]);
insertionsort(a, n);
scanf(“%d ”, a[i]);
return 0;
OUTPUT:
Enter 5 elements: 3 1 5 4 2
array around the picked pivot by placing the pivot in its correct position
correct position in the sorted array and put all smaller elements to the
left of the pivot, and all greater elements to the right of the pivot.
Partition is done recursively on each side of the pivot after the pivot is
placed in its correct position and this finally sorts the array.
#include<stdio.h>
int i, j, t, pivot;
pivot = first;
i = first+1;
j = last;
while(i<=j)
{
while(a[i] <= a[pivot] && i<=last))
i++;
j--;
if(i<j)
t = a[i];
a[i] = a[j];
a[j] = t;
t = a[pivot];
a[pivot] = a[j];
a[j] = t;
int main()
{
int i, n, a[50];
scanf(“%d”, &a[i]);
quicksort(a, 0, n-1);
printf(“%d “, a[i]);
return 0;
OUTPUT:
Enter 5 elements: 3 1 5 4 2