Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab Manual: Kadi Sarva Vishwavidyalaya, Gandhinagar

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

KADI SARVA VISHWAVIDYALAYA,

GANDHINAGAR

IT504-N
Fundamentals of Algorithms

LAB MANUAL
SEMESTER:5

ENROLLMENT NO :

NAME :

BRANCH :

1
CERTIFICATE

This is to certify that Mr./Ms. ________________________________


Enrollment No. _________________of____________________
branch has satisfactory completed the his/her work in
_____________________________________ during the academic year
_____________________at Vidush Somany Institute of Technology and
Research, GANDHINAGAR.

Date of Submission: _________________________________

Faculty In-Charge : _________________________________

Head of the Department: _________________________________

2
Enrollment No : 21BEIT54043

INDEX

SR.NO PRACTICAL LIST MARKS DATE SIGN

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.

10 Implementation Radix sort.

11 Implementation Quick sort.

12 Implementation Binary sort.

13 Implementation Depth first search.

14 Implementation Depth first search.

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

for(i=top; i>=0; i--)


printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}

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>

int fact(int n){


if (n == 1)
return 1;

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

for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed


{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}

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]);
}

int largest(int a[])


{
int larger=a[0], i;
for(i=1;i<10;i++)
{
if(a[i]>larger)
larger = a[i];
}
return larger;
}
void radix_sort(int a[])
{
int bucket[10][10], bucket_count[10];
int i, j, k, remainder, NOP=0, divisor=1, larger, pass;
larger = largest(a);
while(larger>0)
{
NOP++;
larger/=10;
}
for(pass=0;pass<NOP;pass++) // Initialize the buckets
{
for(i=0;i<10;i++)
bucket_count[i]=0;
for(i=0;i<10;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>

using namespace std;

int partition (int a[], int start, int end)


{
int pivot = a[end];
int i = (start - 1);

for (int j = start; j <= end - 1; j++)


{

if (a[j] < pivot)


{
i++;
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int t = a[i+1];
a[i+1] = a[end];
a[end] = t;
return (i + 1);
}

void quick(int a[], int start, int end)


{
if (start < end)
{
int p = partition(a, start, end);
quick(a, start, p - 1);
quick(a, p + 1, end);
}
}

void printArr(int a[], int n)

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

int output[size], outputPtr=0, stack[size], top=-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=1,temp,isthere,alreadyAnElementPushed;
void push(int);
push(start);
while(top!=-1)
{
temp=pop();
printf("%d\t",temp);
output[outputPtr++]=temp;
if(outputPtr>=size)break;
for(j=0; j<size; ++j)
{
alreadyAnElementPushed=0;
if(matrix[temp-1][j]==1)
{
isthere=0;
for(k=0; k<outputPtr; ++k)
{
if(output[k]==j+1)isthere=1;
}
if(top!=-1)
for(k=0; k<=top; ++k)
{

21
=
Enrollment no.21BEIT54043

if(stack[k]==j+1)isthere=1;
}
if(isthere==0)
{
push(j+1);
alreadyAnElementPushed=1;
}
}
if(alreadyAnElementPushed==1)break;
}
}
}

void push(int node)


{
stack[++top]=node;
}

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);
}
}
}
}

void enqueue(int node)


{
if(front==-1 && rear==-1)front=rear=0;
else rear++;
queue[rear]=node;
}

int dequeue(void)
{
int temp=queue[front];
if(front==0 && rear==0){front=rear=-1;}
else front++;
return temp;
}

OUTPUT:

24

You might also like