Not Yet Answered # Binary Search... help pls

__avd 1,826 ddanbe 2,503 Discussion Starter wil0022 __avd 1,826 Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...

0

Thanks ddanbe, Informative and **must read** article, indeed.

```
int[] no = { 20, 10, 90, 23, 21, 32, 87, 45, 76, 56 };
int result = Array.BinarySearch(no, 32);
```

0

Yes adatapost, and you pointed me to the fact that this is about search, not trees.:$

I am always told you could only perform a binary search in a sorted array. But try this:

```
int[] no = { 20, 10, 90, 23, 21, 32, 87, 45, 76, 56 };
int[] sortedno = { 10, 20, 21, 23, 32, 45, 56, 76, 87, 90 };
int result1 = Array.BinarySearch(no, 20); //I get -3 for result1
int result2 = Array.BinarySearch(sortedno, 10); //correct result2=0
```

The strange thing is that if I do this with 32(like you did) it is always correct:-O

We all know that 32 is some kind of "special" number, but I wonder why this works.

0

Yes adatapost, and you pointed me to the fact that this is about search, not trees.:$

I am always told you could only perform a binary search in a sorted array. But try this:`int[] no = { 20, 10, 90, 23, 21, 32, 87, 45, 76, 56 }; int[] sortedno = { 10, 20, 21, 23, 32, 45, 56, 76, 87, 90 }; int result1 = Array.BinarySearch(no, 20); //I get -3 for result1 int result2 = Array.BinarySearch(sortedno, 10); //correct result2=0`

The strange thing is that if I do this with 32(like you did) it is always correct:-O

We all know that 32 is some kind of "special" number, but I wonder why this works.

i mean, the input are not constant... they depends on the user input... sorry.. hehehe..

1

ddanbe,

There is no magic with number **32**. You know the algorithm of binarySearch very well. BinarySearch searches an entire one-dimensional sorted Array for a specific element, using the IComparable interface implemented by each element of the Array and by the specified object.

For the OP's concern, more attention is needed to study return value of BinarySearch method.

Text from the MSDN online page, http://msdn.microsoft.com/en-us/library/y15ef976.aspx

The index of the specified value in the specified array, if value is found.

`If value is not found and value is less than one or more elements in array, a negative number which is the bitwise complement of the index of the first element that is larger than value.`

If value is not found and value is greater than any of the elements in array, a negative number which is the bitwise complement of (the index of the last element plus 1).

```
int no = 0;
int[] a = { 20, 10, 90, 23, 21, 32, 87, 45, 76, 56 };
Array.Sort(a);
do
{
Console.WriteLine(string.Join(" ", Array.ConvertAll<int,string>(a,new Converter<int,string>(p=>p.ToString()))));
int.TryParse(Console.ReadLine(), out no);
Console.WriteLine(Array.BinarySearch(a, no));
} while (no != 0);
```

*Edited 7 Years Ago by __avd*: n/a

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

the function that I created to find the ...

Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.

This is the result of previous question answered :

code for the listbox - datagridview interaction

At the top of the code ...