Upcoming SlideShare
×

# Ds program-print

470 views

Published on

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### Ds program-print

1. 1. LINEAR SEARCH#include<iostream.h>#include<conio.h>#include<stdio.h>int search;class linear_s{public: int flag; input(int*,int,int); int n,key,list[200]; void linear_search(int*,int,int); void display(int*,int);};void linear_s::linear_search(int l[],int n,int element){flag=1;for(int k=0;k<n-1;k++){if(l[k]==element){cout<<"nSEARCH IS SUCCESSFULLn"<<"n";cout<<"ELEMENT:t"<<element<<"tFOUND AT LOCATIONt"<<(k+1)<<"n";flag=0;}}if(flag)cout<<"nSEARCH IS UNSUCCESSFULLn";}void linear_s::display(int list[],int n){for(int i=0;i<n;i++){cout<<" "<<list[i];}}linear_s::input(int list[],int number,int key){cout<<"INPUT THE NUMBER OF ELEMENTS IN THE LIST:";cin>>number;cout<<"NUMBER OF ELEMENTS IN THE LIST IS:"<<number<<"n";for(int i=0;i<number;i++){
2. 2. cout<<"INPUT THE ELEMENTS OF THE LIST:"<<i+1<<":";cin>>list[i];}cout<<"ENTER THE ELEMENT TO BE SEARCHED:";cin>>key;search=key;return number;}void main(){linear_s sort;int number,key,list[200];clrscr();number=sort.input(list,number,key);key=search;cout<<"nENTERED LIST AS FOLLOWS:n";sort.display(list,number);sort.linear_search(list,number,key);cout<<"nIN THE FOLLOWING LIST n";sort.display(list,number);getch();}
3. 3. OUTPUT: LINEAR SEARCHINPUT THE NUMBER OF ELEMENTS IN THE LIST: 5NUMBER OF ELEMENTS IN THE LIST IS: 5INPUT THE ELEMENTS OF THE LIST:1INPUT THE ELEMENTS OF THE LIST:2INPUT THE ELEMENTS OF THE LIST:3INPUT THE ELEMENTS OF THE LIST:4INPUT THE ELEMENTS OF THE LIST:5ENTER THE ELEMENT TO BE SEARCHED: 4ENTERED LIST AS FOLLOWS:1 2 3 4 5SEARCH IS SUCCESSFULELEMENT 4 FOUND AT LOCATION 4 IN THE FOLLOWING LIST 1 2 3 4 5INPUT THE NUMBER OF ELEMENTS IN THE LIST: 5NUMBER OF ELEMENTS IN THE LIST IS: 5INPUT THE ELEMENTS OF THE LIST:1INPUT THE ELEMENTS OF THE LIST:2INPUT THE ELEMENTS OF THE LIST:3INPUT THE ELEMENTS OF THE LIST:4INPUT THE ELEMENTS OF THE LIST:5ENTER THE ELEMENT TO BE SEARCHED: 14ENTERED LIST AS FOLLOWS:1 2 3 4 5SEARCH IS UNSUCCESSFUL
4. 4. BINARY SEARCH#include<iostream.h>#include<conio.h>class binary_s{public: void binary_search(int,int,int*); void sort(int*,int); void display(int*,int);};void binary_s::binary_search(int element,int n,int l[]){int flag=1,low,mid,high;low=0,high=n-1;while(low<=high){mid=(low+high)/2;if(element<l[mid])//ELEMENTS IN UPPER HALFhigh=mid-1;else if(element>l[mid])//ELEMENT IN LOWER HALFlow=mid+1;else if(element==l[mid]){cout<<"nSEARCH IS SUCCESSFULLn";cout<<"ELEMENT:t"<<element<<"tFOUND AT LOCATION:t"<<(mid+1);flag=0;break;}}if(flag){cout<<"n SEARCH IS UNSUCCESSFULL";}}void binary_s::sort(int list[],int number){for(int j=0;j<number;j++)for(int k=0;k<number-1;k++){if(list[k]>list[k+1]){int temp=list[k];list[k]=list[k+1];
5. 5. list[k+1]=temp;}}}void binary_s::display(int list[],int number){cout<<"nENTERED LIST AS FOLLOWS IN ASCENDING ORDERn";for(int i=0;i<number;i++)cout<<" "<<list[i];}void main(){binary_s search;int number,key,list[200];clrscr();cout<<"ENTER NUMBER OF ELEMENTS IN THE LIST:";cin>>number;cout<<"NUMBER OF ELEMENTS IN THE LIST IS :"<<number<<"n";for(int i=0;i<number;i++){cout<<"INPUT THE ELEMENTS OF THE LIST:"<<(i+1)<<":";cin>>list[i];}cout<<"ENTERED LIST IS AS FOLLOWS";for(i=0;i<number;i++)cout<<" "<<list[i];cout<<"nENTER THE ELEMENT TO BE SEARCHED:";cin>>key;search.sort(list,number);search.display(list,number);search.binary_search(key,number,list);getch();}
6. 6. OUTPUT: BINARY SEARCHINPUT THE NUMBER OF ELEMENTS IN THE LIST: 5NUMBER OF ELEMENTS IN THE LIST IS: 5INPUT THE ELEMENTS OF THE LIST:11INPUT THE ELEMENTS OF THE LIST:2INPUT THE ELEMENTS OF THE LIST:13INPUT THE ELEMENTS OF THE LIST:4INPUT THE ELEMENTS OF THE LIST:15ENTER THE ELEMENT TO BE SEARCHED: 4ENTERED LIST AS FOLLOWS IN ASCENDING ORDER: 2 4 11 13 15SEARCH IS SUCCESSFULINPUT THE NUMBER OF ELEMENTS IN THE LIST: 5NUMBER OF ELEMENTS IN THE LIST IS: 5INPUT THE ELEMENTS OF THE LIST:1INPUT THE ELEMENTS OF THE LIST:2INPUT THE ELEMENTS OF THE LIST:3INPUT THE ELEMENTS OF THE LIST:4INPUT THE ELEMENTS OF THE LIST:5ENTER THE ELEMENT TO BE SEARCHED: 14ENTERED LIST AS FOLLOWS IN ASCENDING ORDER:2 4 11 13 15SEARCH IS UNSUCCESSFUL
7. 7. Insertion sort#include<iostream.h>#include<stdio.h>#include<conio.h>class insertion{private: int i,k;public: void insert(int*,int); void display(int*,int);};void insertion::insert(int l[],int n){l[0]=-0;for(int i=1;i<=n;i++){int pointer=i-1;int temp=l[i];while(temp<l[pointer]){l[pointer+1]=l[pointer];pointer--;}l[pointer+1]=temp;cout<<"nSTEP="<<i;for(k=1;k<=n;k++)cout<<" "<<l[k];}}void insertion::display(int l[],int n){cout<<"n SORTED LIST IS AS FOLLOWS:";for(i=1;i<=n;i++)cout<<" "<<l[i];}void main(){insertion inssort;int number,list[100];clrscr();cout<<"ENTER NUMBER OF ELEMENTS:";cin>>number;for(int i=1;i<=number;i++)cin>>list[i];
8. 8. inssort.insert(list,number);inssort.display(list,number);getch();}OUTPUT: INSERTION SORTENTER NUMBER OF ELEMENTS: 511214233SORTED LIST IS AS FOLLOWS:2 3 11 14 23
9. 9. BUBBLE SORT#include<iostream.h>#include<conio.h>#include<stdio.h>class bubble{public: void bsort(int,int*); void display(int*,int);};void bubble::bsort(int n,int list[]){int flag=1;for(int i=0;i<n-1;i++){for(int j=0;j<n-i-1;j++){if(list[j]>list[j+1]){int temp=list[j];list[j]=list[j+1];list[j+1]=temp;flag=0;}}if(flag) break;else flag=1;}}void bubble::display(int list[],int num){for(int i=0;i<num;i++)cout<<" "<<list[i];}void main(){bubble sort;int n,key,list[100];clrscr();cout<<"ENTER NUMBER OF ELEMENTS:";cin>>n;cout<<"ENTER THE ELEMENTS:";for(int i=0;i<n;i++){cin>>list[i];
10. 10. cout<<"n";}cout<<"BEFORE SORTING ENTERED LIST IS AS FOLLOWS:n";sort.display(list,n);sort.bsort(n,list);cout<<"nAFTER SORTING THE LIST IS AS FOLLOWS:n";sort.display(list,n);getch();}OUTPUT: BUBBLE SORTENTER NUMBER OF ELEMENTS: 5ENTER THE ELEMENTS:11214233BEFORE SORTING ENTERED LIST IS AS FOLLOWS:11 2 14 23 3AFTER SORTING THE LIST IS AS FOLLOWS:2 3 11 14 23
11. 11. SELECTION SORT#include<iostream.h>#include<conio.h>#include<stdio.h>class selection{private:int temp,current,j;public: void select(int*,int); void display(int*,int);};void selection::select(int a[],int size){for(current=0;current<size-1;current++)for(j=current+1;j<size;j++)if(a[current]>a[j]){temp=a[current];a[current]=a[j];a[j]=temp;}}void selection::display(int list[],int n){cout<<"SORTED LIST IS AS FOLLOWS:n";for(int i=0;i<n;i++){cout<<" "<<list[i];}}void main(){selection sort;int list[30],number;clrscr();cout<<"ENTER NUMBER OF ELEMENTS:";cin>>number;for(int i=0;i<number;i++){cout<<"ENTER THE VALUES FOR:"<<i+1<<"t";cin>>list[i];}sort.select(list,number);sort.display(list,number);getch();}
12. 12. OUTPUT: SELECTION SORTENTER NUMBER OF ELEMENTS: 5ENTER THE VALUES FOR: 1 11ENTER THE VALUES FOR: 2 2ENTER THE VALUES FOR: 3 14ENTER THE VALUES FOR: 4 23ENTER THE VALUES FOR: 5 3SORTED LIST IS AS FOLLOWS:2 3 11 14 23
13. 13. QUICK SORT#include<iostream.h>#include<conio.h>int a[10],l,u,i,j;void quick(int *,int,int);void main(){clrscr();cout <<"ENTER THE VALUES OF 10 ELEMENTS:";for(i=0;i<10;i++)cin >> a[i];l=0;u=9;quick(a,l,u);cout <<"SORTED ELEMENTS ARE:";for(i=0;i<10;i++)cout << a[i] << " ";getch();}void quick(int a[ ],int l,int u){ int p,temp; 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]; a[j]=a[l]; a[l]=temp;
14. 14. cout <<"n";for(i=0;i<10;i++)cout <<a[i]<<" ";quick(a,l,j-1);quick(a,j+1,u);}}OUTPUT: QUICK SORTENTER THE VALUES OF 10 ELEMENTS1122314358913322SORTED ELEMENTS ARE:1 2 3 5 11 14 22 23 33 89
15. 15. MERGE SORT#include<stdio.h>#include<conio.h>#include<iostream.h>void mergesort(int *,int,int);void merge(int *,int,int,int);int a[20],i,n,b[20];void main(){ clrscr(); cout<<"nENTER NUMBER OF ELEMENTS:"; cin>>n; cout<<"nENTER THE VALUES OF THE ELEMENTS:"; for(i=0;i<n;i++) cin>>a[i]; mergesort(a,0,n-1); cout<<"nENTERED VALUES AFTER SORT:"; for(i=0;i<n;i++) cout<<a[i]<<" "; getch();}void mergesort(int a[],int i,int j){ int mid; if(i<j) { mid=(i+j)/2; mergesort(a,i,mid); mergesort(a,mid+1,j); merge(a,i,mid,j); }}void merge(int a[],int low,int mid,int high){ int h,i,j,k; h=low; i=low; j=mid+1; while(h<=mid&&j<=high) { if(a[h]<=a[j]) b[i]=a[h++]; else
16. 16. b[i]=a[j++]; i++; } if(h>mid) for(k=j;k<=high;k++) b[i++]=a[k]; else for(k=h;k<=mid;k++) b[i++]=a[k]; cout<<"n"; for(k=low;k<=high;k++) { a[k]=b[k]; cout<<a[k]<<" "; }}OUTPUT: MERGE SORTENTER NUMBER OF ELEMENTS: 5ENTER THE VALUES OF THE ELEMENTS: 11 2 14 23 3ENTERED VALUES AFTER SORT:2 3 11 14 23