program To Show Binary Search
program To Show Binary Search
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";}
}
#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;
}
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;
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];
#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";
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;
}
return 0;
}
return 0;}