Program 1Program to implement insertion sort Source Code //Program to implement insertion sort #include<stdio.h> #include<conio.h> void main() { clrscr(); int n=0,a[20],key=0,j=0; printf("\t\tPROGRAM TO IMPLEMENT INSERTION SORT --- BY SUGANDHA BHATIA"); printf("\nEnter the number of elements :"); scanf("%d",&n); printf("\nEnter the elements to be sorted :\n"); for(int x=0;x<n;x++) scanf("%d",&a[x]); for(int i=1;i<n;i++) { key=a[i]; j=i-1; while(j>=0 && a[j]>key) { a[j+1]=a[j]; j=j-1; } a[j+1]=key ; } printf("Sorted array is "); for(int k=0;k<n;k++) printf("%d \t",a[k]); getch(); } Output . Amity School Of Engineering & Technology Analysis and Design of Algorithm Lab File Submitted To Submitted By Ms Sumita Gupta Tanu Kanvar A2305209006 7CSE1 2009-2013 . No Program Date Sign .Index S. h> #include <conio. for ( i = 0 . arr[j] = arr[j + 1] . i < n .n=0.i++) scanf("%d". j++ ) { if ( arr[j] > arr[j + 1] ) { temp = arr[j] .Program 2 Program to implement bubble sort Source Code //PROGRAM TO IMPLEMENT BUBBLE SORT #include <stdio. getch().1)-i .h> void main( ) { clrscr(). } } } printf ( "\n\nArray after sorting:\n") . i++ ) { for ( j = 0 .i<n.&n). arr[j + 1] = temp . for( i=0. i < n . printf("\t\t PROGRAM TO IMPLEMENT BUBBLE SORT-----BY SUGANDHA BHATIA").i. j < (n . int arr[20]. } . for ( i = 0 . printf("\n Enter the number of elements :"). printf("\nEnter the array elements :\n").&arr[i]).j.temp=0 . arr[i] ) . scanf("%d". i++ ) printf ( "%d\t". Output . i<n.h> #include<conio. a[i]=x. } } temp=a[i].Program 3 Program to implement selection sort Source Code //Program to implement selection sort #include<stdio.j++) { if(x>a[j]) { x=a[j].i++) { printf("%d". small=j. scanf("%d". printf("\t"). printf("\n\nEnter the number of elements :"). } printf("Elements after sorting are :\n").&n). for(i=0. .j<n. a[small]=temp. int a[20]. for(i=0.i++) { x=a[i].h> void main() { clrscr(). printf("\tProgram to implement selection sort-----By Sugandha Bhatia\n").i<n.a[i]). small=i.small=0.&a[i]).i.temp=0.j. for(i=0. printf("\nEnter array elements :\n") .x=0.n. for(j=i+1.i++) scanf("%d".i<n. } Output .} getch(). i<n.i=0. for(i=0. getch(). } . int a[20].i++) scanf("%d".n=0. flag=1. for(i=0. scanf("%d".Program 4 Program to implement linear search Source Code //Program to implement linear search #include<stdio.i<n.pos=0. } } if(flag==1) printf("Element found at position %d".i++) { if(a[i]==ele) { pos=i+1. printf("\nEnter the element to be searched :").&n).&a[i]).&ele).h> void main() { clrscr(). printf("\nEnter the size of array :").ele=0. scanf("%d".pos).flag=0. else printf("Element not found !!").h> #include<conio. printf("\n\tProgram to implement linear search----By Sugandha Bhatia\n"). printf("\nEnter the array elements:\n"). Output . i<n. scanf("%d". for(i=0.pos=0.last=0.ele=0. getch(). } } if(ele==a[mid]) printf("Element found at position %d". printf("\n\tProgram to implement binary search----By Sugandha Bhatia\n"). } else if(ele==a[mid]) { pos=mid. scanf("%d". beg=0.i=0. if(ele>a[mid]) { beg=mid+1. printf("\nEnter the size of array :").beg=0.h> void main() { clrscr(). int a[20]. } . last=n-1.h> #include<conio.Program 5 Program to implement binary search without using recursion Source code //Program to implement binary search #include<stdio.n=0.mid=0.pos+1). else printf("Element not found !!!") . } else if(ele<a[mid]) { last=mid-1.i++) { mid=(beg+last)/2 .&ele).&a[i]). printf("\nEnter the element to be searched :").i<n.&n).i++) scanf("%d". printf("\nEnter the array elements:\n"). for(i=0. Output . h> #include<conio. return 0. int a[50].n. scanf("%d".no-1). if (beg > last) return -1.beg.n.&no). printf("\nPROGRAM TO IMPLEMENT BINARY SEARCH USING RECURSION----BY SUGANDHA BHATIA"). printf("\nEnter the elements :\n").x++) scanf("%d". printf("\n\n\nEnter the number of terms : ").result. mid = (beg + last)/2. result = binarysearch(a. } .&a[x]). binarysearch(a. binarysearch(a.n.0. } void main() {clrscr().int n. } if(n > a[mid]) { beg = mid + 1.last).x.Program 6 Program to implement binary search using recursion Source code #include<stdio.1.beg. } if(n < a[mid]) { last = mid . printf("\nEnter the number to be searched : ").x<no. scanf("%d". } return 0. if(n == a[mid]) { printf("The element is at position %d\n".mid+1). for(x=0.int beg. getch().h> int binarysearch(int a[].last).int last) { int mid.&n). if(result == -1) printf("Element not found").no. int n. Output . j++) { div=ar[j].j<n. printf("\nEnter number of elements:").i<n. min=a. } . if(min>div) { int temp=min. exit(0).a). getch().n. void main() { clrscr(). int ar[20].int). div=temp. } a=gcd(min.a. for(int i=0.h> #include<stdlib. printf("\t\nPROGRAM TO FIND GCD OF N NUMBERS----BY SUGANDHA BHATIA"). for(int j=1. } if(min==0) { printf(" GCD is 1 !!"). } printf("GCD:%d ".h> #include<conio.h> int gcd(int.div). min=div. scanf("%d".Program 7 Program to find GCD of n numbers Source Code #include<stdio.min.&ar[i]).i++) scanf("%d". printf("\nEnter the numbers").div. min=ar[0]. getch().&n). int div) { int r=(div%min).int gcd(int min. min=r. } Output . } return min.div). min=gcd(min. if(r!=0) { div=min. h> #include<math.to. getch( ).h> void hanoi(int x.'B'). moves=pow(2. hanoi(x-1.moves).aux.'C'. printf("Enter the number of disks you want to play with:"). hanoi(disk. else { hanoi(x-1.aux. char aux) { if(x==1) printf("Move Disk From %c to %c\n".from.Program 8 Program to implement tower of Hanoi using recursion Source Code #include<stdio. clrscr().from. printf("Move Disk From %c to %c\n".disk)-1. } Output .from).to).h> #include<conio. char from. printf("\nThe No of moves required is=%d \n".&disk). int moves.from. scanf("%d".'A'.to). } } void main( ) { int disk.to). char to.