Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

program To Show Binary Search

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

//Program to show binary search

#include<iostream>
#include<conio.h
void bubbleSort(int array[], int size);
bool binarySearch(int array[], int size,int key);
void main(){
cout<<"Enter 5 numbers randomly : "<<endl;
int array[5];
for(int i=0; i<5; i++)
{cout<<"\t"; cin>>array[i];
}
bubbleSort(array,5);
cout<<"\n\t\t\tEnter Key To Search: ";
int key;
cin>>key;
int result=binarySearch(array,5,key);
if(result==1)
cout<<"\n\t\t\tKey Found in Array "<<endl;
else
cout<<"\n\t\t\tKey NOT Found in Array "<<endl;
return 0;
}
void bubbleSort(int array[], int size){
cout<<" Input array is: "<<endl;
for(int j=0; j<size; j++)
{ cout<<"\t\t\tValue at "<<j<<" Index: "<<array[j]<<endl; }cout<<endl;
int temp;
for(int i2=0; i2<size; i2++) {
for(int j=0; j<size-1; j++)

{
if(array[j]>array[j+1])
{ temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}} }
cout<<" Sorted Array is: "<<endl;
for(int i3=0; i3<size; i3++)
{ cout<<"\t\t\tValue at "<<i3<<" Index: "<<array[i3]<<endl;
}}
bool binarySearch(int array[],int size, int key){
int start=1, end=size;
int mid=(start+end)/2;
while(start<=end&&array[mid]!=key){
if(array[mid]<key){
start=mid+1;
}
else{
end=mid-1;
}
mid=(start+end)/2;}
if(array[mid]==key)
return true;
else
return false
cout<<"\n\n\n";}
}

//program to show INSERTION sort


#include<iostream>

#include<conio.h>
void s_sort(T a[],int n)
{
int i,j,t;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[j]<a[i]))
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}

int main()
{
int a[100],i,n;
cout<<"Enter The number of Element:\n";
cin>>n;
cout<<"\nEnter Elements:\n";
for(i=0;i<n;i++)
{

cout<<"\nEnter:";

cin>>a[i];
}
s_sort(a,n);
cout<<"\nAfter Sorting\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
getch();
return 0;
}

/SELECTION SORT
#include<iostream.h>
#include<conio.h

int main() {
cout<<"Enter The Size Of Array: ";
int size;
cin>>size;

for(int j=0;j<size;j++){
cout<<"Enter "<<j<<" Element: ";
cin>>array[j];
}

for(int a=0;a<size;a++){
cout<<"array[ "<<a<<" ] = ";
cout<<array[a]<<endl;
}

cout<<"Enter Key To Search in Array";


cin>>key;

for(i=0;i<size;i++){
if(key==array[i]){
cout<<"Key Found At Index Number : "<<i<<endl;
break;
}
}
if(i != size){
cout<<"KEY FOUND at index : "<<i;
}

else{
cout<<"KEY NOT FOUND in Array ";
}
return 0;
}

//linear search
#include<iostream.h>
#include<conio.h

int main() {
cout<<"Enter The Size Of Array: ";
int size;

cin>>size;

int array[size], key,i;


for(int j=0;j<size;j++){
cout<<"Enter "<<j<<" Element: ";
cin>>array[j];
}
for(int a=0;a<size;a++){
cout<<"array[ "<<a<<" ] = ";
cout<<array[a]<<endl;
}

cout<<"Enter Key To Search in Array";


cin>>key;

for(i=0;i<size;i++){
if(key==array[i]){
cout<<"Key Found At Index Number : "<<i<<endl;
break;
}
}

if(i != size){
cout<<"KEY FOUND at index : "<<i;
}
else{
cout<<"KEY NOT FOUND in Array ";

}
return 0;
}

//bubbblesort
#include<iostream.h>
#include<conio.h
Void main()

int main(){
int array[5];

cout<<"Enter 5 numbers randomly : "<<endl;


for(int i=0; i<5; i++)
{
cin>>array[i];
}
cout<<endl;
cout<<"Input array is: "<<endl;
for(int j=0; j<5; j++)
{
cout<<"\t\t\tValue at "<<j<<" Index: "<<array[j]<<endl;
}
cout<<endl;
int temp;
for(int i2=0; i2<=4; i2++)
{
for(int j=0; j<4; j++)
{
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
cout<<" Sorted Array is: "<<endl;
for(int i3=0; i3<5; i3++)
{
cout<<"\t\t\tValue at "<<i3<<" Index: "<<array[i3]<<endl;
}
return 0;
}

//program to show stack operation using array


#include <iostream.h>
#include <conio.h>
#define MAX 10

#define MIN 0
void main(){
int choice;
stack obj;
while(1){
clrscr();
cout<<"\t\t 1) PUSH

\n";

cout<<"\t\t 2) POP

\n";

cout<<"\t\t 3) PEEP

\n";

cout<<"\t\t 4) CHANGE

\n";

cout<<"\t\t 5) STATUS

\n";

cout<<"\t\t 6) DISPLAY

\n";

cout<<"\t\t 7) EXIT

\n";

cout<<"\t\tEnter your Choice : ";


cin>>choice;
int item,index;
switch(choice){
case 1 : cout<<"\n\n\t\t*****PUSH*****\n";
cout<<"\t\tEnter item to be pushed : ";
cin>>item;
obj.push(item);
break;
case 2 : cout<<"\n\n\t\t*****POP*****\n";
item=obj.pop();
if(item!=-111){
cout<<"\n\t\tItem being poped out is : "<<item;
getch();
}
break;

case 3 : cout<<"\n\n\t\t*****PEEP*****\n";
cout<<"\t\tEnter index for item to be peeped : ";
cin>>index;
item=obj.peep(index);
if(item!=-111){
cout<<"\n\t\tItem to be peeped is : "<<item;
getch(); }
break;
case 4 : cout<<"\n\n\t\t*****CHANGE*****\n";
cout<<"\t\tEnter index for item to be changed : ";
cin>>index;
cout<<"\t\tEnter item value for it : ";
cin>>item;
obj.change(index,item);
break;
case 5 : cout<<"\n\n\t\t*****STATUS*****\n";
cout<<"\n\t\tTotal Elements are : "<<obj.status();
getch();
break;
case 6 : cout<<"\n\n\t\t*****DISPLAY*****\n";
obj.display();
break;
case 7 : gotoout;
default: cout<<"\n\n\t\tInvalid Choice\n\n"; getch();break;}
}
}
cout:
}

/count vowels,digits,consonants
#include<iostream.h>
#include<conio.h>
#include<string.h>
int main()
{
char line[150];

int i,v,c,ch,d,s,o;
o=v=c=ch=d=s=0;
cout << "Enter a line of string: " << endl;
cin.getline(line, 150);
for(i=0;line[i]!='\0';++i)
{
if(line[i]=='a'
|| line[i]=='e'
|| line[i]=='i'
|| line[i]=='o'
|| line[i]=='u'
|| line[i]=='A'
|| line[i]=='E'
|| line[i]=='I'
|| line[i]=='O'
|| line[i]=='U')
++v;
else if((line[i]>='a'&& line[i]<='z')
|| (line[i]>='A'&& line[i]<='Z'))
++c;
else if(line[i]>='0'&&c<='9')
++d;
else if (line[i]==' ')
++s;
}
cout << " Vowels: " << v << endl;
cout << " Consonants: " << c << endl;
cout << " Digits: " << d << endl;
cout << " White Spaces: " << d << endl;

return 0;
}

//program to add two array


#include<iostream>
#include<conio.h>
Void main()
{
int first[20], second[20], c, n;

cout << "Enter the number of elements in the array ";


cin >> n;

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


for ( c = 0 ; c < n ; c++ )
cin >> first[c];
cout << "Enter elements of second array " << endl;
for ( c = 0 ; c < n ; c++ )
cin >> second[c];
cout << "Sum of elements of two arrays " << endl;
for ( c = 0 ; c < n ; c++ )
cout << first[c] + second[c] << endl;

return 0;
}

//program to subtract two array


#include<iostream>
#include<conio.h>
Void main()
{
int first[20], second[20], c, n;

cout << "Enter the number of elements in the array ";


cin >> n;

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

for ( c = 0 ; c < n ; c++ )


cin >> first[c];

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

for ( c = 0 ; c < n ; c++ )


cin >> second[c];

cout << "Sum of elements of two arrays " << endl;

for ( c = 0 ; c < n ; c++ )


cout << first[c] + second[c] << endl;

return 0;}

// pointer with functions


#include<iostream.h
Void main()
int addition (int a, int b)
{ return (a+b); }
int subtraction (int a, int b)
{ return (a-b); }
int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}
int main ()
{
int m,n;
int (*minus)(int,int) = subtraction;

m = operation (7, 5, addition);


n = operation (20, m, minus);
cout <<n;
return 0;

You might also like