Hi there!
Maybe anyone know how to solve this problem?
First of all, here is a code:
static void Main(string[] args)
{
int[] array = { 25, 47, 47, 55, 59, 69, 72, 89, 93, 117 };
int element = 47;
int index = findWithBisection(element, array);
Console.WriteLine("Element " + element + " is one the place: " + index);
}
static int findWithBisection(int el, int[] a)
{
if (a.Length == 0)
{
return -1;
}
if (a != null)
{
int right = a.Length - 1; // right searching zone
int left = 0; // left searching zone
int half; // index of halved array
while (left <= right)
{
half = (left + right) / 2;
while (left <= half)
{
if (a[left] != el)
left++;
else
return left;
}
if (a[half] == el)
return half;
else if (a[half] < el)
{
left = half + 1;
}
else
{
right = half - 1;
}
}
}
return -1;
}
With this code I get result: Element 47 is on the place: 1
But we have two 47 numbers in array so currently code is searching FIRST repeating of element.
And I need to find LAST repeating of element
So in this case, the result should be: Element 47 is on the place: 2 (cuz thats the last repeating of element in array). How to change the code then?
Any suggestion is the most welcome!
Best regards,
Tr1umPr0