Question: can you rewrite the selection _ sort portion without using max index, and use only my code for selection _ sort. correct my code please,
can you rewrite the selectionsort portion without using max index, and use only my code for selectionsort. correct my code please, while sticking to how i wrote it I suspect my issue is with the sorting algorithm not working. Here is the code with purpose to randomly populate an array using unseeded rand and sort the array in DESCENDING order, then have an output that says if key number exists or not.
#include iostream
using namespace std;
bool binarysearchint arrayxint size, int key;
void selectionsortint arrayxint size;
void printarrayint arrayxint size;
int main
int key;
int key;
const int size ;
int arrayxsize;
cout Enter key endl;
cin key;
printarrayarrayxsize;
selectionsortarrayx size;
binarysearcharrayxsize key;
cout Enter key endl;
cin key;
binarysearcharrayx size, key;
printarrayarrayx size;
cout printarray;
selectionsortarrayxsize;
cout selectionsort;
void printarrayint arrayxint size
for int i ; i size; i
arrayxirand;
cout arrayxi;
void selectionsortint arrayxint size
int temp;
for int i ; i size ; i
for int j i; j size; j
if arrayxjarrayxi
temp arrayxi;
arrayxiarrayxj;
arrayxjtemp;
bool binarysearchint arrayxint size, int key
bool found false;
int low ;
int high size ;
int middle high low ;
while low high&& found false
if arrayxmiddlekey
found true;
cout
The key exists.";
else
if arrayxmiddlekey
low middle ;
else
high middle ;
middle high low ;
return found;
if found false
cout The key does not exist ;
else
cout The key exists";
bool binarysearchint arrayxint size, int key
bool found false;
int low ;
int high size ;
int middle high low ;
while low high&& found false
if arrayxmiddlekeyfound true;
cout
The key exists.";
else
if arrayxmiddlekey
low middle ;
else
high middle ;
middle high low ;
return found;
if found false
cout The key does not exist ;
else
cout The key exists";
Please disregard brackets and other small syntax errors because the copying messed with the code.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
