Lab Manual: Kadi Sarva Vishwavidyalaya, Gandhinagar
Lab Manual: Kadi Sarva Vishwavidyalaya, Gandhinagar
Lab Manual: Kadi Sarva Vishwavidyalaya, Gandhinagar
GANDHINAGAR
IT504-N
Fundamentals of Algorithms
LAB MANUAL
SEMESTER:5
ENROLLMENT NO :
NAME :
BRANCH :
1
CERTIFICATE
2
Enrollment No : 21BEIT54043
INDEX
1 Implementation of queue.
2 Implementation of Stack.
3 Implementation of factorial-iterative
approach.
4 Implementation of factorial-recursive
approach.
5 Implementation of Fibonacci-iterative
approach.
6 Implementation of Fibonacci-recursive
approach.
7 Implementation of Insertion.
8 Implementation of Selection.
9 Implementation of Linear.
3
Enrollment No : 21BEIT54043
PRACTICAL: 1
AIM: Implementation of
queue. INPUT: (Code)
#include<stdio.h>
#define n 5
void main(){
int i, j=1, queue[n], front=0, rear=0, x=n, ch=1;
printf("1. Insert \n2. Delete \n3. Display \n4.Exit");
while(ch){
printf("\nEnter Choice: ");
scanf("%d", &ch);
switch(ch){
case 1:
if(rear==x){
printf("\nQueue is full!!!");
// exit(0);
}else{
printf("\nEnter number
%d: ",j++); scanf("%d",
} &queue[rear++]);
break;
case 2:
if(front==rear){
printf("\nQueue is Empty!!!");
// exit(0);
}else{
printf("\n%d Deleted\n
",queue[front++]); x++;
}
break;
case 3:
if(front==rear){
printf("\nQueue is Empty!!!");
// exit(0);
}else{
printf("\nElement in Queue:\n");
for(i=front; i<rear; i++){
printf("%d\t",queue[i]);
4
=
Enrollment no.21BEIT54043
}
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("\nInvalid Choice!!!");
}
}
return 0;
}
OUTPUT:
5
=
Enrollment no.21BEIT54043
PRACTICAL: 2
AIM: Implementation of Stack.
INPUT:
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
//clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t ");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
6
=
Enrollment no.21BEIT54043
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
7
=
Enrollment no.21BEIT54043
OUTPUT:
8
=
Enrollment no.21BEIT54043
PRACTICAL: 3
AIM: Implementation of factorial-iterative approach.
INPUT:
#include<stdio.h>
void main(){
int i=1, fact=1, n;
printf("Factorial using Iterative Approch...\n");
printf("Enter Number: ");
scanf("%d",&n);
while(i<=n){
fact = fact*i;
i++;
}
printf("Factorial of %d is %d",n,fact);
}
OUTPUT:
9
=
Enrollment no.21BEIT54043
PRACTICAL: 4
AIM: Implementation of factorial-recursive approach.
INPUT:
#include<stdio.h>
return n*fact(n-1);
}
void main(){
int n;
printf("Factorial using Recursive Approch...\n");
printf("Enter Number: ");
scanf("%d",&n);
printf("Factorial of %d is %d",n,fact(n));
}
OUTPUT:
10
=
Enrollment no.21BEIT54043
PRACTICAL: 5
AIM: Implementation of Fibonacci-iterative approach.
INPUT:
#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);//printing 0 and 1
OUTPUT:
11
=
Enrollment no.21BEIT54043
PRACTICAL: 6
AIM: Implementation of Fibonacci-recursive approach.
INPUT: (Code)
#include<stdio.h>
void printFibonacci(int n){
static int n1=0,n2=1,n3;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf("%d ",n3);
printFibonacci(n-1);
}
}
int main(){
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n-2);//n-2 because 2 numbers are already printed
return 0;
}
OUTPUT:
12
=
Enrollment no.21BEIT54043
PRACTICAL: 7
AIM: Implementation of Insertion.
INPUT: (Code)
#include<stdio.h>
void main ()
{
int i,j, k,temp;
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
for(k=1; k<10; k++)
{
temp = a[k];
j= k-1;
while(j>=0 && temp <= a[j])
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
for(i=0;i<10;i++)
{
printf("%d\t",a[i]);
}
}
OUTPUT:
13
=
Enrollment no.21BEIT54043
PRACTICAL: 8
AIM: Implementation of Selection.
INPUT: (Code)
#include <stdio.h>
int main() {
int arr[10]={6,12,0,18,11,99,55,45,34,2};
int n=10;
int i, j, position, swap;
for (i = 0; i < (n - 1); i++) {
position = i;
for (j = i + 1; j < n; j++) {
if (arr[position] > arr[j])
position = j;
}
if (position != i) {
swap = arr[i];
arr[i] = arr[position];
arr[position] = swap;
}
}
for (i = 0; i < n; i++)
printf("%d\t", arr[i]);
return 0;
}
OUTPUT:
14
=
Enrollment no.21BEIT54043
PRACTICAL: 9
AIM: Implementation of Linear.
INPUT: (Code)
#include<stdio.h>
void main ()
{
int a[10] = {10, 23, 40, 1, 2, 0, 14, 13, 50, 9};
int item, i,flag;
printf("\nEnter number to search\n");
scanf("%d",&item);
for (i = 0; i< 10; i++)
{
if(a[i] == item)
{
flag = i+1;
break;
}
else
flag =
0;
}
if(flag != 0)
{
printf("\nItem found at location %d\n",flag);
}
else
{
printf("\nItem not found\n");
}
}
OUTPUT:
15
=
Enrollment no.21BEIT54043
PRACTICAL: 10
AIM: Implementation Radix sort.
INPUT: (Code)
#include <stdio.h>
int largest(int a[]);
void radix_sort(int a[]);
void main()
{
int i;
int a[10]={90,23,101,45,65,23,67,89,34,23};
printf(" Unsorted List :\n");
for(i=0;i<10;i++)
printf(" %d\t", a[i]);
radix_sort(a);
printf("\n\n The sorted array is: \n");
for(i=0;i<10;i++)
printf(" %d\t", a[i]);
}
16
=
Enrollment no.21BEIT54043
{
// sort the numbers according to the digit at passth place
remainder = (a[i]/divisor)%10;
bucket[remainder][bucket_count[remainder]] = a[i];
bucket_count[remainder] += 1;
}
// collect the numbers after PASS pass
i=0;
for(k=0;k<10;k++)
{
for(j=0;j<bucket_count[k];j++)
{
a[i] = bucket[k][j];
i++;
}
}
divisor *= 10;
}
}
OUTPUT:
17
=
Enrollment no.21BEIT54043
PRACTICAL: 11
AIM: Implementation Quick sort.
INPUT: (Code)
#include <iostream>
18
=
Enrollment no.21BEIT54043
{
int i;
for (i = 0; i < n; i++)
cout<<a[i]<< " ";
}
int main()
{
int a[] = { 23, 8, 28, 13, 18, 26 };
int n = sizeof(a) / sizeof(a[0]);
cout<<"Before sorting array elements are - \n";
printArr(a, n);
quick(a, 0, n - 1);
cout<<"\nAfter sorting array elements are - \n";
printArr(a, n);
return 0;
}
OUTPUT:
19
=
Enrollment no.21BEIT54043
PRACTICAL: 12
AIM: Implementation Binary sort.
INPUT: (Code)
#include<iostream>
using namespace std;
int binarySearch(int arr[], int beg, int end,int item){
int mid;
if(end >= beg){
mid = (beg + end) / 2;
if(arr[mid] == item){
return mid+1;
}
else if(arr[mid] < item){
return binarySearch(arr,mid+1,end,item);
}
else{
return binarySearch(arr,beg,mid-1,item);
}
} return -1; } int main ()
{
int arr[10] = {16, 19, 20, 23, 45, 56, 78, 90, 96, 100};
int item, location=-1;
cout<<"Enter the item which you want to search ";
cin>>item;
location = binarySearch(arr, 0, 9, item);
if(location != -1)
{
cout<<"Item found at location \t"<<location;
}
else
{
cout<<"Item not found";
}
}
OUTPUT:
20
=
Enrollment no.21BEIT54043
PRACTICAL: 13
AIM: Implementation Depth first search.
INPUT: (Code)
#include <stdio.h>
#define size 8
21
=
Enrollment no.21BEIT54043
if(stack[k]==j+1)isthere=1;
}
if(isthere==0)
{
push(j+1);
alreadyAnElementPushed=1;
}
}
if(alreadyAnElementPushed==1)break;
}
}
}
int pop(void)
{
return stack[top--];
}
OUTPUT:
22
=
Enrollment no.21BEIT54043
PRACTICAL: 14
AIM: Implementation Depth first search.
INPUT: (Code)
#include <stdio.h>
#define size 8
int output[size], outputPtr=0, queue[size], front=-1, rear=-1;
void main()
{
int matrix[size][size]=
{
{0,1,1,0,0,0,1,0},
{1,0,1,1,1,0,0,0},
{1,1,0,0,1,0,0,0},
{0,1,0,0,0,1,0,1},
{0,1,1,0,0,1,1,0},
{0,0,0,1,1,0,0,0},
{1,0,0,0,1,0,0,1},
{0,0,0,1,0,0,1,0}
};
int i, j, k, start=5,temp,isthere;
void enqueue(int);
enqueue(start);
while(!(front==-1 && rear==-1))
{
temp=dequeue();
printf("%d\t",temp);
output[outputPtr++]=temp;
if(outputPtr>=size)break;
for(j=0; j<size; ++j)
{
if(matrix[temp-1][j]==1)
{
isthere=0;
for(k=0; k<outputPtr; ++k)
{
if(output[k]==j+1)isthere=1;
}
if(!(front==-1 && rear==-1))
for(k=front; k<=rear; ++k)
{
23
=
Enrollment no.21BEIT54043
if(queue[k]==j+1)isthere=1;
}
if(isthere==0)enqueue(j+1);
}
}
}
}
int dequeue(void)
{
int temp=queue[front];
if(front==0 && rear==0){front=rear=-1;}
else front++;
return temp;
}
OUTPUT:
24