We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5
1.
//Binary search
#include<iostream.h> int found=0;
int Binary_Search(int list[],int); do{ int main() mid=(left+right)/2; { if(key==list[mid]) int list[]={2,3,4,5,7,8,9,13}; found=1; int k; else{ cout<<"eneter the value to search"; if(key<list[mid]) cin>>k; right=mid-1; int x=Binary_Search(list, k); else if (x>=0) left=mid+1; cout<<"the item is found at index"<<" "<<x; } else }while(found==0 && left<=right); cout<<"the item is not in the list"; if(found==0) } index= -1; int Binary_Search(int list[],int key) else { index=mid; int left=0,index,n=8,mid; return index; int right=n-1; }
2. //linear search that finds the item in the declared array
#include<iostream.h> //cout<<"the value was found at";
void Linear_Search(int list[], int); } int main() void Linear_Search(int list[], int key) { { int list[]={3,5,6,7,8}; int index=0; int value,indx; int found=0; cout<<"enter the key value to search"; do{ cin>>value; if(key==list[index]) Linear_Search(list,value); found=1; else } index++; else }while(found==0 && index<5); { if(found==0) cout<<"found at index"<<" { "<<index; index=-1; }} cout<<"the key was not found";
3. /* Program of sorting using bubble sort */
#include <iostream.h > {
#include <stdio.h> temp = arr[j]; int main() arr[j] = arr[j+1]; { int arr[ 20],i,j,k,temp,n,xchanges; arr[j+1] = temp; cout<<"Enter the number of elements : "; xchanges++; cin>>n; //scanf("%d",&n); }/*End of if*/ for (i = 0; i < n; i++) }/*End of inner for loop*/ { cout<<"Enter element %d : if(xchanges==0) /*If list is sorted*/ ";//,i+1); break; cin>> arr[i]; cout<<"After Pass %d elements are : } ";//,i+1); cout<<"Unsorted list is :\n"; for (k = 0; k < n; k++) for (i = 0; i < n; i++) cout<<arr[k]<<" "; cout<<arr[i]<<" "; cout<<"\n"; cout<<"\n"; }/*End of outer for loop*/ /* Bubble sort*/ for (i = 0; i < n-1 ; i++) cout<<"Sorted list is :\n"; {xchanges=0; for (i = 0; i < n; i++) for (j = 0; j <n-1-i; j++) cout<<arr[i]<<" "; { cout<<"\n"; if (arr[j] > arr[j+1]) }/*End of main()*/ 4. /* Program of sorting using insertion sort */ #include<iostream.h> #include <stdio.h> #define MAX 20 int main() { int arr[MAX],i,j,k,n; cout<<"Enter the number of elements : "; cin>>n; for (i =0; i <n; i++) { cout<<"Enter element %d : ";//,i+1); cin>>arr[i]; } cout<<"Unsorted list is :\n"; for (i = 0; i < n; i++) cout<<arr[i]<<" "; cout<<"\n"; /*Insertion sort*/ for(j=1;j<n;j++) { k=arr[j]; /*k is to be inserted at proper place*/ for(i=j-1;i>=0 && k<arr[i];i--) arr[i+1]=arr[i]; arr[i+1]=k; cout<<"Pass , Element inserted in proper place: \n";//,j,k); for (i = 0; i < n; i++) cout<<arr[i]<<" "; cout<<"\n"; } cout<<"Sorted list is :\n"; for (i = 0; i < n; i++) cout<<arr[i]<<" "; cout<<"\n"; }/*End of main()*/ 5. For selction sort #include<iostream> using namespace std; int findSmallest (int[],int); int main () { int myarray[10] = {11,5,2,20,42,53,23,34,101,22}; int pos,temp,pass=0; cout<<"\n Input list of elements to be Sorted\n"; for(int i=0;i<10;i++) { cout<<myarray[i]<<"\t"; } for(int i=0;i<10;i++) { pos = findSmallest (myarray,i); temp = myarray[i]; myarray[i]=myarray[pos]; myarray[pos] = temp; pass++; } cout<<"\n Sorted list of elements is\n"; for(int i=0;i<10;i++) { cout<<myarray[i]<<"\t"; } cout<<"\nNumber of passes required to sort the array: "<<pass; return 0; } int findSmallest(int myarray[],int i) { int ele_small,position,j; ele_small = myarray[i]; position = i; for(j=i+1;j<10;j++) { if(myarray[j]<ele_small) { ele_small = myarray[j]; position=j; } } return position; }