Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Ds program-print

275

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

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

• Be the first to like this

Views
Total Views
275
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
15
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

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