//from main
for(int i = 0; i < LIM; i++)
{
cout << ">> ";
cin >> num;
input(orderedList, num, i);
//ordered list is an array of float
}
//function
void input(float list[], float item, int iter)
{
int first = 0;
int last = iter; //serves as the length of the array
unsignedint mid;
bool found = false;
if(iter == 0)
{
list[0] = item;
return;
}
elsewhile(first <= last )
{
mid = (first + last) / 2;
if(list[mid] > item) //**error
last = mid - 1;
else
first = mid + 1;
}
if(list[mid] <= item)
mid++;
if(list[mid] > item)
for(int i = iter; i > mid; i--) // moves every element to one space
list[i] = list[i-1];
list[mid] = item;
}
the code is fine is my input is: 64 32 1 8 12
and if my input is: 32 64 1 8 12 it goes wrong
I already isolated my problem, which is if my first input is less than the second input it will be s***, otherwise it's okay.
Can someone help me fix my algorithm, i've been thinking of a fix for hours.
NOTE: The requirement is using binary search populate the ordered array.