You are on page 1of 8

LINEAR(lsearch) AND BINARY(bpsearch) SEARCH

#include<iostream.h>

#include<conio.h>

void lsearch(int arr[], int size, int num)

int i,flag=0;

for(i=0;i<size;i++)

if(num==arr[i])

flag=1;

cout<<"The number exists"<<endl;

break;

if(flag==0)

cout<<"The number does not exist"<<endl;

void bpsearch(int arr[], int size, int num)

int beg,end,mid,flag=0;

beg=0;

end=size-1;
while(beg<=end)

mid=(beg+end)/2;

if(num==arr[mid])

flag=1;

cout<<"The number exists"<<endl;

break;

else if(num>arr[mid])

beg=mid+1;

else

end=mid-1;

if(flag==0)

cout<<"The number does not exist"<<endl;

void main()

{
clrscr();

char ans;

int arr[1000],n,i,s,ch;

X:

cout<<"Enter the size of the array which is less than the number 1000"<<endl;

cin>>s;

cout<<"Enter elements for an array"<<endl;

for(i=0;i<s;i++)

cin>>arr[i];

cout<<"Enter the number"<<endl;

cin>>n;

cout<<"Select the option with which you want to exacute your search with:-"<<endl;

cout<<"1. Linear Search"<<endl;

cout<<"2. Binary Search"<<endl;

cin>>ch;

switch(ch)

case 1:

lsearch(arr, s, n);

break;

case 2:

bpsearch(arr, s, n);

break;
default:

cout<<"Invalid no. entered."<<endl;

break;

cout<<"Do you wish to continue?"<<endl;

cin>>ans;

if(ans=='Y'||ans=='y')

goto X;

getch();

INSERTION(isort),SELECTION(ssort), AND BUBBLE SORT(bsort)


#include<iostream.h>

#include<conio.h>

void isort(int arr[], int size)

int i,j,temp;

for(i=1;i<size;i++)

temp=arr[i];

j=i-1;

while(temp<arr[j] && j>=0)

arr[j+1]=arr[j];
j--;

arr[j+1]=temp;

for(i=0;i<size;i++)

cout<<arr[i]<<" ";

cout<<endl;

void ssort(int arr[], int size)

int i,j,small,pos;

for(i=0;i<size;i++)

small=arr[i];

pos=i;

for(j=i+1;j<size;j++)

if(small>arr[j])

small=arr[j];

pos=j;

}
arr[pos]=arr[i];

arr[i]=small;

for(i=0;i<size;i++)

cout<<arr[i]<<" ";

cout<<endl;

void bsort(int arr[], int size)

int i,j,temp;

for(i=0;i<size-1;i++)

for(j=0;j<size-i-1;j++)

if(arr[j]>arr[j+1])

temp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

for(i=0;i<size;i++)
{

cout<<arr[i]<<" ";

cout<<endl;

void main()

clrscr();

char ans;

int arr[1000],size,i,ch;

X:

cout<<"Enter the size of the array"<<endl;

cin>>size;

cout<<"Enter the elements of the array"<<endl;

for(i=0;i<size;i++)

cin>>arr[i];

cout<<"By which sort do you want to sort the array:-"<<endl;

cout<<"1.Selection Sort"<<endl;

cout<<"2.Insertion Sort"<<endl;

cout<<"3.Bubble Sort"<<endl;

cin>>ch;

switch(ch)

{
case 1:

ssort(arr,size);

break;

case 2:

isort(arr,size);

break;

case 3:

bsort(arr,size);

break;

default:

cout<<"You have entered an invalid number"<<endl;

break;

cout<<"Do you wish to continue?"<<endl;

cin>>ans;

if(ans=='y'||ans=='Y')

goto X;

getch();

You might also like