I am trying to search a descending sorted array using this binary search code. However, after I sort it, and try to search, it doesn\'t come back with any result, just a loading
There is a binary search in the Array
class:
int index = Array.BinarySearch(mynumbers, target);
For descending order, this can be easily accomplished with a ReverseComparer
which is easy to write like:
public class ReverseComparer : IComparer
{
public int Compare(T x, T y)
{
return Comparer.Default.Compare(y, x);
}
}
Then:
int index = Array.BinarySearch(numbers, 7, new ReverseComparer());
If this is an academic exercise and you must use a custom search, of course, this won't apply. If it's got to be a custom algorithm for a class, then the problems are that you must break out of the loop when found, and the index is at mid
, not at mynumbers[mid]
:
//for a sorted array with descending values
while (first<=last)
{
mid = (first + last) / 2;
if (target < mynumbers[mid])
{
first = mid + 1;
}
if (target > mynumbers[mid])
{
last = mid - 1;
}
else
{
// the index is mid, not mynumbers[mid], and you need to break here
// once found or it's an infinite loop once it finds it.
Label11.Text = "Target " + item + " was found at index " + mid;
break;
}
}
And actually, I'd probably set a bool flag instead to keep the algorithm pure and not mix the find with the output concerns, this will also make it easier to tell what happened if you exit the loop with not found:
bool found = false;
//for a sorted array with descending values
while (!found && first<=last)
{
mid = (first + last) / 2;
if (target < mynumbers[mid])
{
first = mid + 1;
}
if (target > mynumbers[mid])
{
last = mid - 1;
}
else
{
// You need to stop here once found or it's an infinite loop once it finds it.
found = true;
}
}
Label11.Text = found
? "Item " + item + " was found at position " + mid
: "Item " + item + " was not found";