Sunteți pe pagina 1din 31

DEV BHOOMI INSTITUTE

OF TECHNOLOGY
DEHRADUN
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING

PRACTICAL FILE
DESIGN & ANALYSIS OF ALGORITHMS
(TCS-503)

Submitted To: Submitted By:

MR. DHAJVIR SINGH Name:- MS. PRAGYA


PRIYADARSHANI
HOD CSE DEPARTMENT,
DBIT Branch:-COMPUTER SCIENCE

Year/Sem: 3rd/5th. Sec:

1
INDEX
S.NO. PRACTICAL NAME DATE PAGE. NO SIGNATURE

1. Write a Program to
Implement Insertion Sort in C
programming language

2. Write a Program to
implement quick sort in C
programming language.

3. Write a Program to
implement merge sort in C
programming language.

4. Write a Program to
Implement Heap sort in C
programming language.

5 Write a Program to
Implement Bubble sort in C
programming language.
6. Write a Program to
Implement Searching:
Sequential and Binary Search.

7. Write a Program to implement


Greedy method Fractional
knapsack problem in C

8. Write a Program to implement


Dynamic programming
traveling sales person
problem..

9. Write a Program to implement


Back tracking (n-queens
problem).

10. Write a Program to implement


Back tracking(Hamiltonian
cycles)

2
PRACTICAL NO. 1

Objective: Write a Program to Implement Insertion Sort in C programming language.

Program:

#include<stdio.h>
int main()
{
int i, j, num, temp, arr[20];
printf("Enter total elements: ");
scanf("%d", &num);
printf("Enter %d elements: ", num);
for (i = 0; i < num; i++) {
scanf("%d", &arr[i]);
}
for (i = 1; i < num; i++) {
temp = arr[i];
j = i - 1;
while ((temp < arr[j]) && (j >= 0))
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = temp;
}
printf("After Sorting: ");
for (i = 0; i < num; i++) {
printf("%d", arr[i]);
}

return 0;
}

3
Output:

4
PRACTICAL NO. 2

Objective: Write a Program to implement quick sort in C programming language.


Program:
#include<stdio.h>

void quicksort(int [10],int, int);


int main(){
int x[20],size,i;

printf("Enter size of the array: ");


scanf("%d",&size);
printf("Enter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);

printf("Sorted elements: ");


for(i=0;i<size;i++)
printf(" %d",x[i]);
return 0;
}

void quicksort(int x[10],int first,int last){


int pivot,j,temp,i;

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

while(i<j)
{

while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;

if(i<j)
{
temp=x[i];

5
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;

quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}

6
Output:

7
PRACTICAL NO. 3

Objective: Write a Program to implement merge sort in C programming language.

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

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

return 0;
}

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

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

9
Output:

10
PRACTICAL NO. 4

Objective: Write a Program to Implement Heap sort in C programming language.

Program:
#include<stdio.h>

void create(int []);


void down_adjust(int [],int);

void main()
{
int heap[30],n,i,last,temp;
printf("Enter no. of elements:");
scanf("%d",&n);
printf("\nEnter elements:");
for(i=1;i<=n;i++)
scanf("%d",&heap[i]);

//create a heap
heap[0]=n;
create(heap);

//sorting
while(heap[0] > 1)
{
//swap heap[1] and heap[last]
last=heap[0];
temp=heap[1];
heap[1]=heap[last];
heap[last]=temp;
heap[0]--;
down_adjust(heap,1);
}

//print sorted data


printf("\nArray after sorting:\n");
for(i=1;i<=n;i++)
printf("%d ",heap[i]);
}

11
void create(int heap[])
{
int i,n;
n=heap[0]; //no. of elements
for(i=n/2;i>=1;i--)
down_adjust(heap,i);
}

void down_adjust(int heap[],int i)


{
int j,temp,n,flag=1;
n=heap[0];

while(2*i<=n && flag==1)


{
j=2*i; //j points to left child
if(j+1<=n && heap[j+1] > heap[j])
j=j+1;
if(heap[i] > heap[j])
flag=0;
else
{
temp=heap[i];
heap[i]=heap[j];
heap[j]=temp;
i=j;
}
}
}

12
Output:

13
PRACTICAL NO. 5

Objective: Write a Program to Implement Bubble sort in C programming language.

Program:
#include<stdio.h>

int main()
{
int a[50],n,i,j,temp;
printf("Enter the size of array: ");
scanf("%d",&n);
printf("Enter the array elements: ");

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

for(i=1;i<n;++i)
for(j=0;j<(n-i);++j)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf("\nArray after sorting: ");
for(i=0;i<n;++i)
printf("%d ",a[i]);

return 0;
}

14
Output:

15
PRACTICAL NO. 6

Objective: Write a Program to Implement Searching: Sequential and Binary Search.


Program:
/* C program for binary search and the sequential search on * the same sorted array */

#include <stdio.h>
void sequential_search(int array[ ], int size, int n) /* Function for sequential search */
{
int i;
for (i = 0; i < size; i++)
{
if (array[i] == n)
{
printf("%d found at location %d. ", n, i+1);
break;
}
}
if (i == size)
printf("Not found! %d is not present in the list. ", n);
}
/* End of sequential_search() */

/* Function for binary search */


void binary_search(int array[], int size, int n)
{
int i, first, last, middle;
first = 0;
last = size - 1;
middle = (first+last) / 2;

while (first <= last) {


if (array[middle] < n)
first = middle + 1;
else if (array[middle] == n) {
printf("%d found at location %d. ", n, middle+1);
break;
}
else
last = middle - 1;

middle = (first + last) / 2;


}
if ( first > last )

16
printf("Not found! %d is not present in the list. ", n);
}
/* End of binary_search() */

/* The main() begins */


int main()
{
int a[200], i, j, n, size;
printf("Enter the size of the list:");
scanf("%d", &size);
printf("Enter %d Integers in ascending order ", size);
for (i = 0; i < size; i++)
scanf("%d", &a[i]);
printf("Enter value to find ");
scanf("%d", &n);
printf("Sequential search ");
sequential_search(a, size, n);
printf("Binary search ");
binary_search(a, size, n);
return 0;
}

17
Output:

18
PRACTICAL NO. 7

Objective: Write a Program to implement Greedy method Fractional knapsack problem in C.


Program:
# include<stdio.h>
void knapsack(int n, float weight[ ], float profit[ ], float capacity) {
float x[20], tp = 0;
int i, j, u;
u = capacity;

for (i = 0; i < n; i++)


x[i] = 0.0;

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


if (weight[i] > u)
break;
else {
x[i] = 1.0;
tp = tp + profit[i];
u = u - weight[i];
}
}
if (i < n)
x[i] = u / weight[i];
tp = tp + (x[i] * profit[i]);
printf(" The result vector is:- ");
for (i = 0; i < n; i++)
printf("%f ", x[i]);
printf(" Maximum profit is:- %f", tp);
}

int main()
{
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;

printf(" Enter the no. of objects:- ");


scanf("%d", &num);
printf(" Enter the wts and profits of each object:- ");
for (i = 0; i < num; i++) {
scanf("%f %f", &weight[i], &profit[i]);
}

19
printf(" Enter the capacityacity of knapsack:- ");
scanf("%f", &capacity);
for (i = 0; i < num; i++) {
ratio[i] = profit[i] / weight[i];
}
for (i = 0; i < num; i++) {
for (j = i + 1; j < num; j++) {
if (ratio[i] < ratio[j]) {
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;

temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}
knapsack(num, weight, profit, capacity);
return(0);
}

20
Output:

21
PRACTICAL NO. 8

Objective: Write a Program to implement Dynamic programming traveling sales person


problem..

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

int a[10][10],visited[10],n,cost=0;
void get()
{
int i,j;
printf("Enter No. of Cities: ");
scanf("%d",&n);
printf(" Enter Cost Matrix ");
for(i=0;i < n;i++)
{
printf(" Enter Elements of Row : %d ",i+1);
for( j=0;j < n;j++)
scanf("%d",&a[i][j]);
visited[i]=0;
}

printf(" The cost list is: ");


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

void mincost(int city)


{
int i,ncity;
visited[city]=1;
printf("%d -->",city+1);
ncity=least(city);
if(ncity==999)
{
ncity=0;
printf("%d",ncity+1);
cost+=a[city][ncity];
return;

22
}
mincost(ncity);
}

int least(int c)
{
int i,nc=999;
int min=999,kmin;
for(i=0;i < n;i++)
{
if((a[c][i]!=0)&&(visited[i]==0))
if(a[c][i] < min)
{
min=a[i][0]+a[c][i];
kmin=a[c][i];
nc=i;
}
}
if(min!=999)
cost+=kmin;
return nc;
}

void put()
{
printf(" Minimum cost:");
printf("%d",cost);
}

void main()
{
clrscr();
get();
printf(" The Path is: ");
mincost(0);
put();
getch();
}

23
Output:

24
PRACTICAL NO. 9

Objective: Write a Program to implement Back tracking (n-queens problem).

Program:

#include<stdio.h>
#include<math.h>

char a[10][10];
int n;

void printmatrix() {
int i, j;
printf(" ");

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


for (j = 0; j < n; j++)
printf("%c ", a[i][j]);
printf(" ");
}
}

int getmarkedcol(int row) {


int i;
for (i = 0; i < n; i++)
if (a[row][i] == 'Q') {
return (i);
break;
} }

int feasible(int row, int col) {


int i, tcol;
for (i = 0; i < n; i++) {
tcol = getmarkedcol(i);
if (col == tcol || abs(row - i) == abs(col - tcol))
return 0;
}
return 1;
}
void nqueen(int row) {
int i, j;
if (row < n) {
for (i = 0; i < n; i++) {
if (feasible(row, i)) {
a[row][i] = 'Q';

25
nqueen(row + 1);
a[row][i] = '.';
}}
} else {
printf(" The solution is:- ");
printmatrix();
}
}
int main() {
int i, j;

printf(" Enter the no. of queens:- ");


scanf("%d", &n);

for (i = 0; i < n; i++)


for (j = 0; j < n; j++)
a[i][j] = '.';
nqueen(0);
return (0);
}

Output :

26
PRACTICAL NO.10

Objective: Write a Program to implement Back tracking(Hamiltonian cycles)


Program:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#define V 5
using namespace std;

void printSolution(int path[]);


/* * check if the vertex v can be added at index 'pos' in the Hamiltonian Cycle */

bool isSafe(int v, bool graph[V][V], int path[], int pos)


{
if (graph [path[pos-1]][v] == 0)
return false;
for (int i = 0; i < pos; i++)
if (path[i] == v)
return false;
return true;
}
/* solve hamiltonian cycle problem */
bool hamCycleUtil(bool graph[V][V], int path[], int pos)
{
if (pos == V)
{
if (graph[ path[pos-1] ][ path[0] ] == 1)
return true;
else
return false;
}
for (int v = 1; v < V; v++)
{
if (isSafe(v, graph, path, pos))
{
path[pos] = v;
if (hamCycleUtil (graph, path, pos+1) == true)
return true;
path[pos] = -1;
}
}
return false;
}
27
/* solves the Hamiltonian Cycle problem using Backtracking.*/

bool hamCycle(bool graph[V][V])


{
int *path = new int[V];
for (int i = 0; i < V; i++)
path[i] = -1;
path[0] = 0;
if (hamCycleUtil(graph, path, 1) == false)
{
cout<<"\nSolution does not exist"<<endl;
return false;
}
printSolution(path);
return true;
}

/* Main */

void printSolution(int path[])


{
cout<<"Solution Exists:";
cout<<" Following is one Hamiltonian Cycle \n"<<endl;
for (int i = 0; i < V; i++)
cout<<path[i]<<" ";
cout<< path[0]<<endl;
}

int main()
{
/* Let us create the following graph
(0)--(1)--(2)
| / \ |
| / \ |
|/ \|
(3)-------(4)
*/ bool graph1[V][V] = {{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 1},
{0, 1, 1, 1, 0},
};
hamCycle(graph1);

28
/* Let us create the following graph
(0)--(1)--(2)
| /\ |
| / \ |
|/ \|
(3) (4)
*/bool graph2[V][V] = {{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 0},
{0, 1, 1, 0, 0},
};
hamCycle(graph2);
return 0;
}

29
Output :

$ g++ hamiltonian.cpp
$ a.out

30
31

S-ar putea să vă placă și