Sunteți pe pagina 1din 23

Date :-25 Jan.

2012

Prog.No. 3

Aim:- To write a menu driven program for insertion and selection sort, and calculate their time complexicity. Language Used:- C++ Language Code :#include<iostream.h> #include<conio.h> #include<process.h> #include<time.h> #include<dos.h> void selsort(int a[],int size) { int small,pos,temp; for(int i=0;i<size;i++) { small=a[i]; pos=i; for(int j=i+1;j<size;j++) { if(a[j]<small) { small=a[j]; pos=j; } } temp=a[i]; a[i]=a[pos]; a[pos]=temp; cout<<"\n Array after pass "<<i+1<<" is : "; for(j=0;j<size;j++) cout<<a[j]<<" "; } } int main() { clrscr(); clock_t start,end;
Rahul 051963031 10

int n,i,ar[20],j,code,k; cout<<"Enter the number of elements in linear array\n"; cin>>n; cout<<"enter the array\n"; for(i=0;i<n;i++) { cout<<"a["<<i<<"]:"; cin>>ar[i]; } cout<<"Array inserted\n"; for(i=0;i<n;i++) { cout<<ar[i]<<"\t"; } cout<<"\nWELCOME TO SORT MENU\n"; cout<<"1. Insertion Sort\n"; cout<<"2. Selection Sort\n"; cout<<"3. Exit\n"; cin>>code; switch(code) { case 1: cout<<"You opted for INSERTION SORT\n"; start=clock(); for(j=1;j<n;j++) { k=ar[j]; for (i=j-1;i>=0 && k<ar[i];i--) ar[i+1]=ar[i]; ar[i+1]=k; cout<<"Elements inserted in the proper place"; for(i=0;i<n;i++) cout<<ar[i]<<endl; } cout<<"Sorted list of elements:\n"; for(i=0;i<n;i++) { cout<<ar[i]<<endl; } delay(100); end=clock(); cout<<"\nTime complexicity of insertion sort"<<((end-start)/ (CLK_TCK));
Rahul 051963031 10

break; case 2: start=clock(); cout<<"You opted for SELECTION SORT\n"; selsort(ar,n); cout<<"\n\nThe sorted array is : \n"; for(i=0;i<n;i++) { cout<<ar[i]<<" "; } delay(100); end=clock(); cout<<"\nTime complexicity of selection sort"<<((end-start)/ (CLK_TCK)); break; case 3: { exit(1); } return 0; }}

Date :-18 Jan.2012

Prog.No. 2

Aim:- To write a menu driven program for quick sort and bubble sort, and calculate their time complexicity. Language Used:- C++ Language Code :#include<iostream.h> #include<conio.h>
Rahul 051963031 10

#include<process.h> #include<time.h> #include<dos.h> void quick(int *,int,int); int main() { clrscr(); clock_t start,end; int n,i,a[20],j,code,l,u,pass=0; cout<<"Enter the number of elements in linear array\n"; cin>>n; cout<<"enter the array\n"; for(i=0;i<n;i++) { cout<<"a["<<i<<"]:\n"; cin>>a[i]; } cout<<"Array inserted\n"; for(i=0;i<n;i++) { cout<<a[i]<<endl; } cout<<"\nWELCOME TO SORT MENU\n"; cout<<"1. Bubble Sort\n"; cout<<"2. Quick Sort\n"; cout<<"3. Exit\n"; cin>>code; switch(code) { case 1: cout<<"You opted for BUBBLE SORT\n"; start=clock(); for( i=0;i<n;i++) { for(int j=0;j<n-i-1;j++) { if(a[j]>a[j+1]) { int t; t=a[j]; a[j]=a[j+1]; a[j+1]=t; } }
Rahul 051963031 10

delay(1000); end=clock(); cout<<"\n"; Cout<<"Final sorted array:\n"; for(i=0;i<n;i++) { cout<<a[i]<<endl; } cout<<"\nTime complexicity of bubble sort"<<((end-start)/ (CLK_TCK)); break; case 2: cout<<"You opted for QUICK SORT\n"; l=0; u=n-1; quick(a,l,u); cout <<"\n\n\tFinal Sorted Array:"; for(i=0;i<n;i++) cout <<a[i]<<" "; getch(); } void quick(int a[],int l,int u) { int p,temp,k; start=clock(); if(l<u) { p=a[l]; i=l; j=u; while(i<j) { while(a[i] <= p && i<j ) i++; while(a[j]>p && i<=j ) j--; if(i<=j) { temp=a[i]; a[i]=a[j]; a[j]=temp;} } temp=a[j];
Rahul 051963031 10

a[j]=a[l]; a[l]=temp; cout <<"\n"; cout<<"\n\tArray After Pass "<<++pass<<":"; for(i=0;i<n;i++) { cout <<a[i]<<" "; } quick(a,l,j-1); quick(a,j+1,u); } } delay(1000); end=clock(); cout<<"Time complexicity of quick sort"<<((end-start)/(CLK_TCK)); cout<<" break; case 3: { exit(1); } return 0; }}

Rahul 051963031 10

Date :-11 Jan.2012

Prog.No. 1

Aim:- To write a menu driven program for linear and binary search, and calculate their time complexicity. Language Used:- C++ Language Code :#include<iostream.h> #include<conio.h> #include<dos.h> #include<time.h> #include<process.h> int main() { clrscr(); clock_t start,end; int arr[20],n,item,i,code; cout<<"Enter the number of elements in an linear array\n"; cin>>n; cout<<"\nEnter the "<<n<<" elements\n"; cout<<"\nEnter SORTED array if applying BINARY SEARCH\n"; for(i=0;i<n;i++) { cin>>arr[i]; } cout<<"Array inserted\n"; for(i=0;i<n;i++) {
Rahul 051963031 10

cout<<"\narr["<<i<<"]:"<<arr[i]; } cout<<"\nEnter the item to be searched "; cin>>item; cout<<"\nEnter your choice\n"; cout<<"1. Linear search\n"; cout<<"2. Binary search\n"; cout<<"3. Exit\n"; cin>>code; switch(code) { case 1: cout<<"You selected the LINEAR SEARCH\n"; int flag=0; start=clock(); for(i=0;i<n;i++) { if(arr[i]==item) { flag=1; } } if(flag==1) { cout<<"element found successfully by linear search"; } else { cout<<"Element could not be found\n"; } delay(100); end=clock(); cout<<"\n\nTIME COMPLEXICITY OF LINEAR SEARCH\n"; cout<<"\ntime complexicity of linear search is"<<((end-start)/ (CLK_TCK)); break; case 2: { cout<<"You selected the BINARY SEARCH\n"; int flag=0; int mid; int lb=0;
Rahul 051963031 10

int ub=n-1; start=clock(); while(lb<=ub) { mid=((lb+ub)/2); if(item==arr[mid]) { flag=1; break; } else if(arr[mid]>item) { ub=mid-1; } else if(arr[mid]<item) { lb=mid+1; } } if(flag==1) { cout<<"element found at position is"<<mid; } else { cout<<"element not found by binary search"; } delay(100); end=clock(); cout<<"\n\nTIME COMPLEXICITY OF BINARY SEARCH\n"; cout<<"\nTime complexicity of binary search is"<<((end-start)/ (CLK_TCK)); break; case 3: { exit(1); } } return 0; } }

Rahul 051963031 10

OUTPUT
Enter the number of elements in a linear array 6 Enter the 6 elements Enter SORTED array if applying BINARY SEARCH 10 20 30 40 50 60 Array inserted arr[0]:10 arr[1]:20 arr[2]:30 arr[3]:40 arr[4]:50 arr[5]:60 Enter the item to be searched 50 Enter your choice 1. Linear search 2. Binary search 3. Exit 2 You selected the BINARY SEARCH element found at position :4 TIME COMPLEXICITY OF BINARY SEARCH Time complexicity of binary search is0.10889

OUTPUT
Enter the number of elements in linear array
Rahul 051963031 10

6 enter the array a[0]:60 a[1]:50 a[2]:40 a[3]:30 a[4]:20 a[5]:10 Array inserted 60 50 40

30

20

10

WELCOME TO SORT MENU 1. Bubble Sort 2. Quick Sort 3. Exit 1 You opted for BUBBLE SORT Final sorted array: 10 20 30

40

50

60

Time complexicity of bubble sort 0.989011

OUTPUT
Enter the number of elements in linear array 7 enter the array a[0]:90 a[1]:110
Rahul 051963031 10

a[2]:40 a[3]:20 a[4]:200 a[5]:70 a[6]:30 Array inserted 90 110 40 20 WELCOME TO SORT MENU 1. Insertion Sort 2. Selection Sort 3. Exit 2 You opted for SELECTION SORT Array Array Array Array Array Array Array after after after after after after after pass pass pass pass pass pass pass 1 2 3 4 5 6 7 is is is is is is is : : : : : : : 20 20 20 20 20 20 20

200

70

30

110 40 90 200 70 30 30 40 90 200 70 110 30 40 90 200 70 110 30 40 70 200 90 110 30 40 70 90 200 110 30 40 70 90 110 200 30 40 70 90 110 200

The sorted array is : 20 30 40 70 90 110 200 Time complexicity of selection sort0.10989

Rahul 051963031 10

Aim:- To write a menu driven program for Merge sort and calculate their time complexicity.
CODING#include <iostream.h> #include<dos.h> #include<time.h> #include <conio.h> int a[50]; clock_t start,end; void merge(int,int,int); void merge_sort(int low,int high) { int mid; if(low<high) { mid=(low+high)/2; merge_sort(low,mid); merge_sort(mid+1,high); merge(low,mid,high); }
Rahul 051963031 10

} void merge(int low,int mid,int high) { int h,i,j,b[50],k; h=low; i=low; j=mid+1; start=clock(); while((h<=mid)&&(j<=high)) { if(a[h]<=a[j]) { b[i]=a[h]; h++; } else { b[i]=a[j]; j++; } i++; } if(h>mid) { for(k=j;k<=high;k++) { b[i]=a[k]; i++; } } else { for(k=h;k<=mid;k++) { b[i]=a[k]; i++; } } delay(100); end=clock(); cout<<"\ntime complexicity of MERGE SORT is"<<((end-start)/ (CLK_TCK));
Rahul 051963031 10

for(k=low;k<=high;k++) a[k]=b[k]; } void main() { int num,i; clrscr(); cout<<" MERGE SORT PROGRAM "<<endl; cout<<endl<<endl; cout<<"Please Enter THE NUMBER OF ELEMENTS you want to sort "<<endl; cin>>num; cout<<endl; for(i=1;i<=num;i++) { cin>>a[i] ; } merge_sort(1,num); cout<<endl; cout<<"So, the sorted list (using MERGE SORT) will be :"<<endl; cout<<endl<<endl; for(i=1;i<=num;i++) cout<<a[i]<<" "; cout<<endl; getch(); }

Rahul 051963031 10

OUTPUT

MERGE SORT PROGRAM

Please Enter THE NUMBER OF ELEMENTS you want to sort 4

1 2 3 4

Time complexicity of MERGE SORT is0.054945 So, the sorted list (using MERGE SORT) will be :

Rahul 051963031 10

Date :-08 Feb.2012

Prog.No. 5

Aim:- To write a program to implement Medians of Medians Algorithm , and calculate its time complexicity. Language Used:- C++ Language Code :#include<iostream.h> #include<conio.h> #include<time.h> #include<dos.h> void sort(int arr[], int num) { int temp=0; for(int i=0;i<num;i++) { for(int j=0;j<(num-i);j++) { if(arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } return; } void main() { clrscr(); clock_t start,end; int a[100], b[50], med[20]; int n,i,j; int k=0,m=0; cout<<"\nEnter no. of elements in the array: "; cin>>n; cout<<"\nEnter elements in the array: ";
Rahul 051963031 10

for(i=0;i<n;i++) { cout<<"\nElement "<<i+1<<" : "; cin>>a[i]; } start=clock(); for(i=0;i<n;i++) { for(j=0;j<5;j++) { b[j]=a[i]; i++; } i--; sort(b,5); med[k]=b[2]; cout<<"\nMedian as per pair"<< k+1<<" : "<<med[k]; k++; m=m+1; } sort(med,m); cout<<"\nThe final median is: "; int f; if(m%2!=0) { f=m/2; cout<<med[f]; } else { cout<<med[(m/2)-1]<<" and "<<med[(m/2)]; } delay(1000); end=clock(); cout<<"\n\nTime complexicity of algorithm MEDIAN OF MEDIANS is"<<(end-start)/CLK_TCK; getch(); return; }

Rahul 051963031 10

OUTPUT
Enter no. of elements in the array: 23 Enter elements in the array: Element 1 : 1 Element 2 : 2 Element 3 : 3 Element 4 : 4 Element 5 : 5 Element 6 : 6 Element 7 : 7 Element 8 : 8 Element 9 : 9 Element 10 : 10 Element 11 : 11 Element 12 : 12 Element 13 : 13 Element 14 : 14 Element 15 : 15 Element 16 : 16 Element 17 : 17 Element 18 : 18 Element 19 : 19 Element 20 : 20 Element 21 : 21 Element 22 : 22 Element 23 : 23 Median as per pair1 : 3 Median as per pair2 : 8 Median as per pair3 : 13 Median as per pair4 : 18 Median as per pair5 : 22 The final median is: 13
Rahul 051963031 10

Time complexicity of algorithm MEDIAN OF MEDIANS is0.989011

Date :-17 Feb.2012

Prog.No. 5

Aim:- To write a program to implement Lowest Common Sequence Algorithm . Language Used:- C++ Language Code :#include<iostream.h> #include<conio.h> #include<dos.h> #include<time.h> int main() { clrscr(); int n,n1,i,j; int c[20][20]; char b[20][20]; cout<<"Welcome to LCS Algorithm\n"; char arr[20]; char arr1[20]; cout<<"Enter the length of 1st string\n"; cin>>n; cout<<"Enter string 1\n"; for(i=0;i<n;i++) { cin>>arr[i]; } cout<<"\n"; for(i=0;i<n;i++) { cout<<arr[i]<<"\t"; } cout<<"\nEnter the length of 2nd string\n";
Rahul 051963031 10

cin>>n1; cout<<"Enter string 2\n"; for(j=0;j<n1;j++) { cin>>arr1[j]; } cout<<"\n"; for(j=0;j<n1;j++) { cout<<arr1[j]<<"\t"; } for(i=0;i<n;i++) { c[i][0]=0; } for(j=0;j<n1;j++) { c[0][j]=0; } for(i=1;i<=n;i++) { for(j=1;j<=n1;j++) if(arr[i]==arr1[j]) { c[i][j]=c[i-1][j-1]+1; b[i][j]='0'; } else if(arr[i]!=arr1[j]) { if(c[i-1][j]<=c[i][j-1]) { c[i][j]=c[i-1][j]; b[i][j]='1'; } else { c[i][j]=c[i][j-1]; b[i][j]='2'; } } } cout<<"\n\n\n"; cout<<"LCS\n\n";
Rahul 051963031 10

for(i=0;i<=n;i++) { for(j=0;j<=n1;j++) { if (b[i][j]=='0') { cout<<arr[i]<<"\t"; } } } return 0; }

Rahul 051963031 10

Rahul 051963031 10

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