Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Design Analysis of Algorithm1

Uploaded by

SACHIN VERMA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Design Analysis of Algorithm1

Uploaded by

SACHIN VERMA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

DESIGN ANALYSIS OF ALGORITHMS

LAB ASSIGNMENTS
ASSIGNMENT NO – 1

Que1. Write a program for implementing Bubble Sort Technique.


Sol:

#include<stdio.h>
#include<conio.h>
int main(){

int s,temp,i,j,a[20];

printf("Enter total numbers of elements: ");


scanf("%d",&s);

printf("Enter %d elements: ",s);


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

//Bubble sorting algorithm


for(i=s-2;i>=0;i--)
{
for(j=0;j<=i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

printf("After sorting: ");


for(i=0;i<s;i++)
printf(" %d",a[i]);
return 0;
}
Output
Enter total numbers of elements: 5
Enter 5 elements: 7
4
3
0
2
After sorting: 0 2 3 4 7

Que2. Write a program for implementing Insertion Sort Technique.


Sol:

#include<stdio.h>
#include<conio.h>
int main()
{

int i,j,s,temp,a[20];
clrscr();
printf("Enter total elements: ");
scanf("%d",&s);

printf("Enter %d elements: ",s);


for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=1;i<s;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0)){
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("After sorting: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);

return 0;
getch();
}
Output
Enter total elements: 5
Enter 5 elements: 7
4
3
0
2
After sorting: 0 2 3 4 7

Que3. Write a program for implementing Heap Sort Technique.


Sol:

#include <stdio.h>
#include <conio.h>
void makeheap ( int [ ], int ) ;
void heapsort ( int [ ], int ) ;
void main( )
{
int arr[10] = { 11, 2, 9, 13, 57, 25, 17, 1, 90, 3 } ;
int i ;
clrscr( ) ;
printf ( "Heap Sort.\n" ) ;

makeheap ( arr, 10 ) ;
printf ( "\nBefore Sorting:\n" ) ;
for ( i = 0 ; i <= 9 ; i++ )
printf ( "%d\t", arr[i] ) ;
heapsort ( arr, 10 ) ;
printf ( "\nAfter Sorting:\n" ) ;
for ( i = 0 ; i <= 9 ; i++ )
printf ( "%d\t", arr[i] ) ;
getch( );
}
void makeheap ( int x[ ], int n )
{
int i, val, s, f ;
for ( i = 1 ; i < n ; i++ )
{
val = x[i] ;
s=i;
f=(s-1)/2;
while ( s > 0 && x[f] < val )
{
x[s] = x[f] ;
s=f;
f=(s-1)/2;
}
x[s] = val ;
}
}

void heapsort ( int x[ ], int n )


{
int i, s, f, ivalue ;
for ( i = n - 1 ; i > 0 ; i-- )
{
ivalue = x[i] ;
x[i] = x[0] ;
f=0;

if ( i == 1 )
s = -1 ;
else
s=1;

if ( i > 2 && x[2] > x[1] )


s=2;

while ( s >= 0 && ivalue < x[s] )


{
x[f] = x[s] ;
f=s;
s=2*f+1;

if ( s + 1 <= i - 1 && x[s] < x[s + 1] )


s++ ;
if ( s > i - 1 )
s = -1 ;
}
x[f] = ivalue ;
}
}

OUTPUT
Heap Sort.

Before Sorting:
90 57 25 13 11 9 17 1 2 3

After Sorting:
1 2 3 9 11 13 17 25 57 90

ASSIGNMENT NO – 2

Que1. Write a program for implementing Quick Sort Technique.


Sol:

#include<stdio.h>

void quicksort(int x[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];
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);

}
}
OUTPUT
Enter size of the array: 6
Enter 6 elements: 56
43
24
12
4
1
Sorted elements: 1 4 12 24 43 56

Que2. Write a program for implementing Randomized Quick Sort Technique.


Sol:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int n;
void swap(int *a, int *b)
{
int x;
x = *a;
*a = *b;
*b = x;
}
void quicksort(int s[], int l, int h)
{
int p; /* index of partition */
if ((h - l) > 0) {
p = partition(s, l, h);
quicksort(s, l, p - 1);
quicksort(s, p + 1, h);
}
}
int partition(int s[], int l, int h)
{
int i;
int p; /* pivot element index */
int firsthigh; /* divider position for pivot element */
p = l + (random(n) % (h - l + 1));
swap(&s[p], &s[h]);
firsthigh = l;
for (i = l; i < h; i++)
if(s[i] < s[h]) {
swap(&s[i], &s[firsthigh]);
firsthigh++;
}
swap(&s[h], &s[firsthigh]);

return(firsthigh);
}
void main()
{
int s[20],i;
clrscr();

printf("\nRandomized Quick Sort");


printf("\nEnter the no. of elements");
scanf("%d", &n);
printf("\nEnter the elements one by one");
for(i=0;i<n;i++)
scanf("%d",&s[i]);
quicksort(s,0,n-1);
printf("\nAfter sorting…\n");
for(i=0;i<n;i++)
printf("%d\t",s[i]);
getch();
}

OUTPUT
Randomized Quick Sort
Enter the no. of elements4
Enter the elements one by one45
23
34
44
After sortingà
23 34 44 45
Que3. Write a program for implementing Merge Sort Technique.
Sol:

#include<stdio.h>
#define MAX 50
void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);

int main(){

int merge[MAX],i,n;

printf("Enter the total number of elements: ");


scanf("%d",&n);

printf("Enter the elements which to be sort: ");


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

partition(merge,0,n-1);

printf("After merge sorting elements are: ");


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

return 0;
}

void partition(int arr[],int low,int high){

int mid;

if(low<high){
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}

void mergeSort(int arr[],int low,int mid,int high){

int i,m,k,l,temp[MAX];

l=low;
i=low;
m=mid+1;

while((l<=mid)&&(m<=high)){

if(arr[l]<=arr[m]){
temp[i]=arr[l];
l++;
}
else{
temp[i]=arr[m];
m++;
}
i++;
}

if(l>mid){
for(k=m;k<=high;k++){
temp[i]=arr[k];
i++;
}
}
else{
for(k=l;k<=mid;k++){
temp[i]=arr[k];
i++;
}
}

for(k=low;k<=high;k++){
arr[k]=temp[k];
}
}

OUTPUT
Enter the total number of elements: 5
Enter the elements which to be sort: 28
0
65
34
12
After merge sorting elements are: 0 12 28 34 65

ASSIGNMENT NO – 3
Que1. Write a program for implementing Linear Search Technique.
Sol:

#include<stdio.h>
int main()
{
int a[10],i,n,m,c=0;
printf("Enter the size of an array: ");
scanf("%d",&n);

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


for(i=0;i<=n-1;i++){
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&m);
for(i=0;i<=n-1;i++){
if(a[i]==m){
c=1;
break;
}
}
if(c==0)
printf("The number is not in the list");
else
printf("The number is found");

return 0;
}
OUTPUT
Enter the size of an array: 6
Enter the elements of the array: 34
12
0
22
11
5
Enter the number to be search: 1
The number is not in the list
OR
Enter the size of an array: 6
Enter the elements of the array: 86
34
56
23
0
12
Enter the number to be search: 56
The number is found

Que2. Write a program for implementing Binary Search Technique.


Sol:

#include<stdio.h>
int main(){

int a[10],i,n,m,c=0,l,u,mid;

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


scanf("%d",&n);

printf("Enter the elements in ascending order: ");


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

printf("Enter the number to be search: ");


scanf("%d",&m);
l=0,u=n-1;
while(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;
break;
}
else if(m<a[mid]){
u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("The number is not found.");
else
printf("The number is found.");

return 0;
}

OUTPUT
Enter the size of an array: 5
Enter the elements in ascending order: 87
45
34
2
4
Enter the number to be search: 34
The number is found.

ASSIGNMENT NO – 4

1. Write a program for implementing Knapsack problem using Greedy Method.


# include<stdio.h>
# include<conio.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("\n The result vector is:- ");


for(i=0;i<n;i++)
printf("%f\t",x[i]);

printf("\n Maximum profit is:- %f", tp);

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

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


scanf ("%d", &n);

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

printf ("\n enter the capacity of knapsack:- ");


scanf ("%f", &capacity);

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


{
ratio[i]=profit[i]/weight[i];
}

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


{
for(j=i+1;j<n; 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(n, weight, profit, capacity);


getch();
}
OUTPUT
Enter the no. of objects:- 7

Enter the wts and profits of each object:- 2 10


35
5 15
77
16
4 18
13
enter the capacity of knapsack:- 15
The result vector is:- 1.000000 1.000000 1.000000 1.000000
1.000000 0.666667 0.000000
Maximum profit is:- 55.333332

2. Write a program for implementing Job Sequencing Problem using Greedy Method.

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

void main()
{
int i,j,n,brust_time[10],start_time[10],end_time[10],wait_time[10],temp,tot;
float avg;
clrscr();
printf("Enter the No. of jobs:\n\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n \n Enter %d process burst time:\n",i);
scanf("%d",&brust_time[i]);
}

for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(brust_time[i]>brust_time[j])
{
temp=brust_time[i];
brust_time[i]=brust_time[j];
brust_time[j]=temp;
}
}

if(i==1)
{
start_time[1]=0;
end_time[1]=brust_time[1];
wait_time[1]=0;
}

else
{
start_time[i]=end_time[i-1];
end_time[i]=start_time[i]+brust_time[i];
wait_time[i]=start_time[i];
}
}
printf("\n\n BURST TIME \t STARTING TIME \t END TIME \t WAIT TIME\n");
printf("\n ************************************************************\n");
for(i=1;i<=n;i++)
{
printf("\n %5d %15d %15d %15d",brust_time[i],start_time[i],end_time[i],wait_time[i]);
}
printf("\n ************************************************************\n");
for(i=1,tot=0;i<=n;i++)
tot+=wait_time[i];
avg=(float)tot/n;
printf("\n\n\n AVERAGE WAITING TIME=%f",avg);
for(i=1,tot=0;i<=n;i++)
tot+=end_time[i];
avg=(float)tot/n;
printf("\n\n AVERAGE TURNAROUND TIME=%f",avg);
for(i=1,tot=0;i<=n;i++)
tot+=start_time[i];
avg=(float)tot/n;
printf("\n\n AVERAGE RESPONSE TIME=%f\n\n",avg);
getch();
}
OUTPUT

Enter the No. of jobs:


4
Enter 1 process burst time:
2
Enter 2 process burst time:
2
Enter 3 process burst time:
2
Enter 4 process burst time:
2

BURST TIME STARTING TIME END TIME WAIT TIME

************************************************************

2 0 2 0
2 2 4 2
2 4 6 4
2 6 8 6
************************************************************

AVERAGE WAITING TIME=3.000000

AVERAGE TURNAROUND TIME=5.000000

AVERAGE RESPONSE TIME=3.000000

3. Write a program for implementing Minimal Spanning Tree using Greedy Method.
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
void main()
{
clrscr();
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;
printf("\n");
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0 || visited[v]==0)
{
printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n Minimun cost=%d",mincost);
getch();
}
OUTPUT 3
Enter the number of nodes:3 4
4
Enter the adjacency matrix: 4
2
2
2 Edge 1:(1 2) cost:2
3 Edge 2:(1 3) cost:2
3 Minimun cost=4
ASSIGNMENT - 7
Que1. Write a program for finding minimum and maximum element of an Array.
Sol:

#include<stdio.h>
int findmax(int array[], int n){
int i, maximum;
maximum = array[0];
for(i = 0; i < n; i++){
if(maximum < array[i])
maximum = array[i];
}
return maximum;
}
int findmin(int array[], int n){
int i, minimum;
minimum = array[0];
for(i = 0; i < n; i++){
if(minimum > array[i])
minimum = array[i];
}
return minimum;
}
int main(){
int array[10]; //array declaration of maximum size 10
int max,min,n,i;
printf("Enter the no of element: ");
scanf("%d",&n);
printf("Enter the array: ");
for(i = 0; i < n; i++){
scanf("%d",&array[i]);
}
max = findmax(array, n);
min = findmin(array, n);
printf("\nThe maximum element is %d", max);
printf("\nThe minimum element is %d\n\n", min);
return 0;
}
OUTPUT
Enter the no of element: 5
Enter the array: 34
67
58
12
10

The maximum element is 67


The minimum element is 10
Que2. Write a program for finding the Kth smallest element of an array.
Sol:
public static int kthSmall(int[]a, int k){
if(k == a.length)
return a[a.length-1];
else
return kthSmall(a, k, 0, a.length-1);
}
public static int kthSmall(int[] a, int k, int start, int end){
int j = start;
int i = j-1;
int pivot = a[end];
for(;j<end;j++){
if(a[j] < pivot){
i++;
swap(a, i, j);
}}
i++;
swap(a, i, end);
int small = i-start+1;
if(small == k){
return a[i];
}else if(small > k){
return kthSmall(a, k, start, i-1);
}else{ //small is less than k
return kthSmall(a, k-small, i+1, end);
}
}
public static void swap(int[] a, int i, int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}

You might also like