Hi can anyone help me doing a binary search..?

example:
the number of input is 10.
then the inputted number 20, 10, 90, 23, 21, 32, 87, 45, 76, 56
search value: 32

output:
Lower:
Middle:
Higher:
Value not found!
(Until the value found or the loop end)


sorry for bad english... :)

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);

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.

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..

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

Comments
He, nice instructive code!
This article has been dead for over six months. Start a new discussion instead.