write a program that implements the enhanced Binary Search. Pass the ascending o
ID: 3630554 • Letter: W
Question
write a program that implements the enhanced Binary Search. Pass the ascending or descending order flag to a revised Binary Search function that can be defined like this:int BinarySearch2(int a[], int first, int last, int value, bool ascending);
and the variable min should be renamed as MinOrMax !!
Based on this ascending true or false, you update your first or last index to go next half of array to continue Binary Search. If make fill_array_Rand to initialize your test data.
sample output:
An enhanced version of Selection Sort: Do you want an ascending order? (y/n) y
Fill out 20 Random numbers:
10 93 73 58 70 77 97 67 77 66 84 34 37 3 66 22 70 49 97 56
In sorted order the numbers are:
3 10 22 34 37 49 56 58 66 66 67 70 70 73 77 77 84 93 97 97
Binary Search: Enter a number to search for (-1 to exit): 77
77 is indexed at 14
Binary Search: Enter a number to search for (-1 to exit): 66
66 is indexed at 9
Binary Search: Enter a number to search for (-1 to exit): -1
Try Again? (y/n) y
An enhanced version of Selection Sort: Do you want an ascending order? (y/n) n
Fill out 20 Random numbers:
78 27 75 2 97 88 74 6 71 12 56 46 16 46 15 53 90 89 44 35
In sorted order the numbers are:
97 90 89 88 78 75 74 71 56 53 46 46 44 35 27 16 15 12 6 2
Binary Search: Enter a number to search for (-1 to exit): 97
97 is indexed at 0
Binary Search: Enter a number to search for (-1 to exit): 2
2 is indexed at 19
Binary Search: Enter a number to search for (-1 to exit): 22
22 is not on the list.
Binary Search: Enter a number to search for (-1 to exit): -1
Try Again? (y/n) n
Explanation / Answer
please rate - thanks
#include<iostream>
using namespace std;
void fill_array(int a[],int number_used)
{int i;
cout<<"Fill out "<<number_used<<" Random numbers: ";
for(i=0;i<number_used;i++)
cin>>a[i];
}
void selectionsort(int a[], int n,bool ascend)
{ int i,j,MinOrMax,index,temp,k;
for(i=0;i<n-1; i++)
{index=i;
MinOrMax=a[i];
for(j=i+1;j<n;j++)
{if(ascend)
{
if(MinOrMax>a[j])
{index=j;
MinOrMax=a[j];
}
}
else
{
if(MinOrMax<a[j])
{index=j;
MinOrMax=a[j];
}
}
}
temp=a[i];
a[i]=a[index];
a[index]=temp;
}
}
int BinarySearch2(int a[],int low,int max,int key,bool ascend)
{int mid;
while(low<=max )
{mid=(low+max)/2;
if(a[mid]<key)
if(ascend)
low = mid + 1;
else
max=mid-1;
else
{if(a[mid]>key )
if(ascend)
max=mid-1;
else
low=mid+1;
else
{return mid;
}
}
}
return -1;
}
int main()
{int a[20];
int number_used=20,i,n;
char y;
bool ascend;
do
{
cout<<"An enhanced version of Selection Sort: Do you want an ascending order? (y/n) ";
cin>>y;
if(y=='y')
ascend=true;
else
ascend=false;
fill_array(a,number_used);
cout<<"In sorted order the numbers are: ";
selectionsort(a,number_used,ascend);
for(i=0;i<number_used;i++)
cout<<a[i]<<" ";
cout<<endl;
cout<<"Binary Search: Enter a number to search for (-1 to exit): ";
cin>>n;
while(n!=-1)
{i=BinarySearch2(a,0,20,n,ascend);
if(i==-1)
cout<<n<<" is not on the list ";
else
cout<<n<<" is indexed at "<<i<<endl;
cout<<"Binary Search: Enter a number to search for (-1 to exit): ";
cin>>n;
}
cout<<"Try Again? (y/n) ";
cin>>y;
}while(y=='y');
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.