Вы находитесь на странице: 1из 22

VIDYA BHAVAN COLLEGE FOR ENGINEERING TECHNOLOGY, RAUTAPUR, CHAUBEYPUR, KANPUR

PRACTICAL FILE
SUBJECT:- DESIGN & ANALYSIS OF ALGORITHMS SUBJECT CODE:- ECS-554
SUBMITTED BY:SUBMITTED TO:SUDEEP DUBEY AWASTHI BRANCH:- CS(3rd year) ROLL NO. 0841810413 LECTURER:- VIMAL

S.no. 1.

Experiment Name

Issuing Date

Submission Date

Remarks

write a program of bubble sort with algorithm. write a program merge sort with algorithm
write a program of insertion sort with algorithm write a program of shell sort with algorithm write a program of matrix chain multiplication with algorithm write a program of selection sort with algorithm write a program of heap sort write a program of quick sort

2.

3.

4.

5.

6.

7.

8.

9.

10.

EXPERIMENT NO-1
OBJECT :- write a program of bubble sort with algorithm. ALGORITHM:void bubbleSort(int a[], int n) /* Sorts in increasing order the array A of the size N */ { int k; int bound = n-1; int t; int last_swap; while (bound) { last_swap = 0; for ( k=0; k<bound; k++ ) t = a[k]; /* t is a maximum of A[0]..A[k] */ if ( t > a[k+1] ) { a[k] = a[k+1]; a[k+1] = t; /*swap*/ last_swap = k; /* mark the last swap position */ }//if }//for bound=last_swap; /*elements after bound are already sorted */ }//while } // bubbleSort

Program:#include<stdio.h> #include<stdio.h> void main() { int a[100],n,i; clrscr(); printf("how many elements in array : "); scanf("%d",&n); printf("enter the elements:\n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } bubblesort(a,n); printf("sorted lines as follows:"); for(i=0;i<n;i++)

printf("\n%d",a[i]); getch(); } int bubblesort(int a[],int n) { int i,j,temp; for(i=0;i<n;i++) { for(j=0;j<n-i-1;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } } OUTPUT:how many elements in array : 5 enter the elements: 54 23 11 40 32 sorted lines as follows: 11 23 32 40 54

EXPERIMENT NO-2
OBJECT:- write a program merge sort with algorithm .

ALGORITHM:MERGE-SORT (A, p, r) 1. 2. 3. 4. 5. IF p < r THEN q = FLOOR[(p + r)/2] MERGE (A, p, q) MERGE (A, q + 1, r) MERGE (A, p, q, r) // Check for base case // Divide step // Conquer step. // Conquer step. // Conquer step.

Program:#include<stdio.h> #include<conio.h> void main() { int d[5],e[5],f[10],i,j,k,t; clrscr(); printf("\t\t\t\tMERGE SORT.\n"); printf("\nEnter the value for the first array: \n"); for(i=0;i<5;i++) { scanf("%d",&d[i]); } printf("\nEnter the value for the second array: \n"); for(i=0;i<5;i++) { scanf("%d",&e[i]); } printf("\nFirst array before sorting: \n"); for(i=0;i<5;i++) { printf("\n%d\t",d[i]); } printf("\nSecond array before sorting: \n"); for(i=0;i<5;i++) { printf("\n%d\t",e[i]); } for(i=0;i<4;i++) { for(j=i+1;j<5;j++) { if(d[i]>d[j]) { t=d[i]; d[i]=d[j];

d[j]=t; } if(e[i]>e[j]) { t=e[i]; e[i]=e[j]; e[j]=t; } } } for(i=j=k=0;i<10;) { if(d[j]<=e[k]) f[i++]=d[j++]; else f[i++]=e[k++]; if(j==5||k==5) break; } for(;j<5;) f[i++]=d[j++]; for(;k<5;) f[i++]=e[k++]; printf("\nArray after sorting:: \n"); for(i=0;i<10;i++) { printf("%d\n",f[i]); } getch(); }

OUTPUT:MERGE SORT. Enter the value for the first array: 8 34

22 32 54 Enter the value for the second array: 21 34 99 11 10 First array before sorting: 8 34 22 32 54 Second array before sorting: 21 34 99 11 10 Array after sorting:: 8 10 11 21 22 32 34 34 54 99

EXPERIMENT 3
OBJECT:-write a program of insertion sort with algorithm. ALGORITIHM:void insertionSort(int a[], int array_size) { int i, j, current;

for (i=1; i < array_size; i++) { current = a[i]; j = i; # index of the end of sorted region while ((j > 0) && (a[j-1] > current)) { a[j] = a[j-1]; j = j - 1; } a[j] = current; } } PROGRAM:-

#include<stdio.h> void main() { int A[20],N,Temp,i,j; clrscr(); printf("\n--------------------------------------------------------"); printf("\n\n PROGRAM TO SORT THE NUMBERS USING INSERTION SORT "); printf("\n\n--------------------------------------------------------"); printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d",&N); printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=1; i<=N; i++) { gotoxy(25,11+i); scanf("\n\t\t%d",&A[i]); } for(i=2; i<=N; i++) { Temp = A[i]; j = i-1; while(Temp<A[j] && j>=1) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; }

printf("\n\tTHE ASCENDING ORDER LIST IS...:\n"); for(i=1; i<=N; i++) printf("\n\t\t\t%d",A[i]); printf("\n\n----------------------------------------------------------"); getch(); } Output:PROGRAM TO SORT THE NUMBERS USING INSERTION SORT ENTER THE NUMBER OF TERMS...:5 ENTER THE ELEMENTS OF THE ARRAY...: 5 3 6 4 8 THE ASCENDING ORDER LIST IS...: 3 4 5 6 8

EXPERIMENT 4
OBJECT:-write a program of shell sort with algorithm. ALGORITHM:void shellsort (int[] a, int n) { int i, j, k, h, v; int[] cols = {1391376, 463792, 198768, 86961, 33936, 13776, 4592, 1968, 861, 336, 112, 48, 21, 7, 3, 1} for (k=0; k<16; k++) { h=cols[k]; for (i=h; i<n; i++)

{ v=a[i]; j=i; while (j>=h && a[j-h]>v) { a[j]=a[j-h]; j=j-h; } a[j]=v; } } } PROGRAM:-

#include< stdio.h> #include< conio.h> void shellsort(int a[],int n) { int j,i,k,m,mid; for(m = n/2;m>0;m/=2) { for(j = m;j< n;j++) { for(i=j-m;i>=0;i-=m) { if(a[i+m]>=a[i]) break; else { mid = a[i]; a[i] = a[i+m]; a[i+m] = mid; } } } } } main() { int a[10],i,n; clrscr(); printf("Enter The number Of Elements\t: "); scanf("%d",&n); for(i=0;i< n;i++)

{ printf("\nElement %d\t: ",i+1); scanf("%d",&a[i]); } printf("\nArray Befor Sorting : "); for(i=0;i< n;i++) printf("%5d",a[i]); shellsort(a,n); printf("\nArray After Sorting : "); for(i=0;i< n;i++) printf("%5d",a[i]); getch(); return 0; }

OUTPUT:Enter The number Of Elements : 5 Element 1 : 21 Element 2 : 36 Element 3 : 54 Element 4 : 2 Element 5 : 0 Array Befor Sorting : 21 36 54 2 0 Array After Sorting : 0 2 21 36 54

EXPERIMENT 5
OBJECT:-write a program of matrix chain multiplication with algorithm. ALGORITHM:Matrix-Chain-Order(int p[]) { n = p.length - 1; for (i = 1; i <= n; i++) m[i,i] = 0; for (L=2; L<=n; L++) { // L is chain length for (i=1; i<=n-L+1; i++) { j = i+L-1; m[i,j] = MAXINT; for (k=i; k<=j-1; k++) { q = m[i,k] + m[k+1,j] + p[i-1]*p[k]*p[j];//Matrix Ai has the dimension p[i1] x p[i]. if (q < m[i,j]) { m[i,j] = q; s[i,j] = k;

} } } } } PROGRAM:# include <stdio.h> # include <conio.h> # include <stdlib.h> # define sz 20 # define INF 200000 void print(unsigned long s[][sz], int i, int j) { if (i == j) printf(" A%d ",i); else { printf(" ( "); print(s, i, s[i][j]); print(s, s[i][j] + 1, j); printf(" ) "); } } void printm(unsigned long m[][sz], int n) { int i,j; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf("%5d",m[i][j]); } printf("\n\n"); } printf("\nThe No. of multiplication required is : %d",m[1][n]); } void Matrix_Chain_Order(int p[],int num) { unsigned long m[sz][sz] = {0}; unsigned long s[sz][sz] = {0}; unsigned int q = 0; int i, j, k, l; int n = num; for(i = 1; i <= n; i++) m[i][i] = 0;

for(l = 2; l <= n; l++) for(i = 1; i <= (n - l + 1); i++) { j = i + l - 1; m[i][j] = INF; for(k = i; k <= j - 1; k++) { q = m[i][k] + m[k+1][j] + p[i-1] * p[k] * p[j]; if(q < m[i][j]) { m[i][j] = q; s[i][j] = k; } } } print(s, i-1, j); printf("\n\n"); printf("The Minimum No. of Multiplication Required is:\n\n"); printm(m,n); } void main() { int i,num=0,p[sz]={0}; clrscr(); printf("Enter the number of matrix : "); scanf("%d",&num); printf("Enter %d no. of order sequence for %d matrix :\n",num+1,num); for(i=0;i<=num;i++) scanf("%d",&p[i]); printf("\n\n"); printf("MULTIPLICATION SEQUENCE IS : "); printf("\n\n\t"); Matrix_Chain_Order(p,num); getch(); }

Output:Enter the number of matrix : 2 Enter %d no. of order sequence for %d matrix : 6 4 7 MULTIPLICATION SEQUENCE IS : 0 168 0 0

The Minimum No. of Multiplication Required is: 168

EXPERIMENT 6
OBJECT:-write a program of selection sort with algorithm. ALGORITHM: biinsertsort(int a[], int n) { int lb = 0; int ub = n-1; int j, imin, imax, amin, amax, aub; while ( lb < ub ) { imin = lb; amin = a[imin]; imax = ub; amax = a[imax]; for (j = lb; j <= ub; j++) { if ( a[j] < amin ) { imin = j; amin = a[imin]; } else if ( a[j] > amax ) { imax = j; amax = a[imax]; } } // for j // swap operations for minimum and maximum respectively aub=a[ub]; // can be damaged by minimum swap if ( imin != lb ) { a[imin] = a[lb]; a[lb] = amin; } if ( imax != ub ) { a[imax] = aub; a[ub] = amax; } lb++; ub--; } // while } // biinsertsort PROGRAM:-

#include<stdio.h> #include<conio.h> void main()

{ int a[100],n,i; clrscr(); printf("how many elements : "); scanf("%d",&n); printf("\n\nenter the elenents: \n"); for(i=0;i<n;i++) { scanf("\n%d",&a[i]); } selectionsort(a,n);; printf("sorted array in "); for(i=0;i<n;i++) { printf("\n%d",a[i]); } getch(); } int selectionsort(int a[],int n) { int min,loc,temp,i,j; min=a[0]; for(i=0;i<=n-1;i++) { min=a[i]; loc=i; for(j=i+1;j<=n-1;j++) { if(a[j]<min) { min=a[j]; loc=j; } } if(loc!=i) { temp=a[i]; a[i]=a[loc]; a[loc]=temp; } } } OUTPUT:how many elements : 4 enter the elenents: 32

34 20 11 sorted array in 11 20 32 34

EXPERIMENT 7
OBJECT:-write a program of heap sort.

ALGORITHM
BUILD-HEAP(A) n<- length for i= length[A] B[n]= A[1] Do exchange A[1] A[i] Heap-size[A] heap-size-1 Nn-1 Heapify (A,1)

Program:# include<stdio.h> void heap_sort(int *, int ); void create_heap(int *, int); void display(int *, int); /* Definition of the function */

void create_heap(int list[], int n ) { int k, j, i, temp; for(k = 2 ; k <= n; ++k) { i=k; temp = list[k];

j=i/2; while((i > 1) && (temp > list[j])) { list[i] = list[j]; i=j; j=i/2; if ( j < 1 ) j=1; } list[i] = temp ; } } /* End of heap creation function */ /* Definition of the function */ void heap_sort(int list[], int n) { int k, temp, value, j, i, p; int step = 1; for(k = n ; k >= 2; --k) { temp = list[1] ; list[1] = list[k]; list[k] = temp ; i=1; value = list[1]; j=2; if((j+1) < k) if(list[j+1] > list[j]) j ++; while((j <= ( k-1)) && (list[j] > value)) { list[i] = list[j]; i=j; j = 2*i ; if((j+1) < k) if(list[j+1] > list[j]) j++; else if( j > n) j=n; list[i] = value; } /* end of while statement */

printf("\n Step = %d ", step); step++; for(p = 1; p <= n; p++) printf(" %d", list[p]); } /* end for loop */ } /* Display function */ void display(int list[], int n) { int i; for(i = 1 ; i <= n; ++ i) { printf(" %d", list[i]); } } /* Function main */ void main() { int list[100]; int i, n ; printf("\n Size of the list:"); scanf(" %d", &n); for(i = 1 ; i <= n ; ++i) { list[i] = rand() % 100; } printf("\n Entered list is as follows:\n"); display(list, n); create_heap(list, n); printf("\n Heap\n"); display(list, n); printf("\n\n"); heap_sort(list,n); printf("\n\n Sorted list is as follows :\n\n"); display(list,n); }

Output:Size of the list:5 Entered list is as follows: 46 30 82 90 56

Heap 90 82 46 30 56 Step1=1 82 56 46 30 90 Step2=2 56 30 46 82 90 Step3=3 46 30 56 82 90 Step4=4 30 46 56 82 90 Sorted list is as follows: 30 46 56 82 90

EXPERIMENT 8
OBJECT:-write a program of quick sort. ALGORITHM QUICKSORT (A,p,r) If(p<r) Then q PARTITION (A,p,r) QUICKSORT (A,p,r) QUICKSORT(A, q+1, r) PARTITION(A,p,r) XA[p] Dn p Upr While(dn<up) While(A[dn]<=x) Dndn+1 While A[up]>x Upup-1 If(dn<up) then Exchange A[dn] A[up] Else return up

Program:#include<stdio.h> #include<conio.h> #define max 100 int a[max],n,i,h,l; void main() { void input(void); input(); getch(); } void input(void) {

void quicksort(int a[],int l,int h); void output(int a[],int n); printf("how many elements in the array:"); scanf("%d",&n); printf("\n"); printf("enter the elements : \n"); for(i=0;i<=n-1;i++) { scanf("%d",&a[i]); } l=0; h=n-1; quicksort(a,l,h); printf("sorted array:\n"); output(a,n); } void quicksort(int a[],int l,int h) { int temp,key,low,high; low=l; high=h; key=a[(low+high)/2]; do { while(key>a[low]) { low++; } while(key<a[high]) { high--; } if(low<=high) { temp=a[low]; a[low++]=a[high]; a[high--]=temp; } } while(low<=high); if(l<high) quicksort(a,l,high); if(low<h) quicksort(a,low,h); } void output(int a[],int n)

{ for(i=0;i<=n-1;i++) { printf("%d\n",a[i]); }} OUTPUT:how many elements in the array:6 enter the elements : 32 11 20 40 30 10 sorted array: 10 11 20 30 32 40

Вам также может понравиться