Documente Academic
Documente Profesional
Documente Cultură
OF TECHNOLOGY
DEHRADUN
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
PRACTICAL FILE
DESIGN & ANALYSIS OF ALGORITHMS
(TCS-503)
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.
2
PRACTICAL NO. 1
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
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
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);
return 0;
}
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;
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}
9
Output:
10
PRACTICAL NO. 4
Program:
#include<stdio.h>
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);
}
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);
}
12
Output:
13
PRACTICAL NO. 5
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
#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() */
16
printf("Not found! %d is not present in the list. ", n);
}
/* End of binary_search() */
17
Output:
18
PRACTICAL NO. 7
int main()
{
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;
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
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;
}
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
Program:
#include<stdio.h>
#include<math.h>
char a[10][10];
int n;
void printmatrix() {
int i, j;
printf(" ");
25
nqueen(row + 1);
a[row][i] = '.';
}}
} else {
printf(" The solution is:- ");
printmatrix();
}
}
int main() {
int i, j;
Output :
26
PRACTICAL NO.10
#include <iostream>
#include <cstdio>
#include <cstdlib>
#define V 5
using namespace std;
/* Main */
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