1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#include<iostream.h>
#include<conio.h>
void enterarr(int [], int);
void displayarr(int [], int);
void ssortarr(int [], int);
void bsearcharr(int [], int);
void main()
{
clrscr();
int A[100], n;
do
{
cout<<"Enter no of elements of the array <=100: ";
cin>>n;
}while(n>100);
cout<<endl<<endl;
enterarr(A,n);
displayarr(A,n);
ssortarr(A,n);
bsearcharr(A,n);
getch();
}
void enterarr(int B[], int n)
{
cout<<"Enter array elements:"<<endl<<endl;
for(int i=0; i<n; i++)
cin>>B[i];
cout<<endl<<endl;
}
void displayarr(int B[], int n)
{
cout<<"The array you entered is:"<<endl<<endl;
for(int i=0; i<n; i++)
cout<<B[i]<<" ";
cout<<endl<<endl;
}
void ssortarr(int B[], int n)
{
for(int i=0; i<(n-1); i++)
{int small=i;
for(int j=i+1; j<n; j++)
if(B[j]<B[small])
small=j;
if(small!=i)
{
int temp=B[i];
B[i]=B[small];
B[small]=temp;
}
}
cout<<"The sorted array is: "<<endl<<endl;
for(i=0; i<n; i++)
cout<<B[i]<<" ";
cout<<endl<<endl;
}
void bsearcharr(int B[], int n)
{
int mid, lbound=0, ubound=n-1, found=0, data;
cout<<endl;
cout<<"Enter element to be searched: ";
cin>>data;
cout<<endl<<endl;
while((lbound<=ubound) && !(found))
{
mid=(lbound+ubound)/2;
if(data>B[mid])
lbound=mid+1;
else if(data<B[mid])
ubound=mid-1;
else;
found++;
}
if(found)
cout<<data<<" found at position "<<mid+1<<".";
else if(!found)
cout<<data<<" not found in the array. ";
}
|