Solved

# Why BinarySearch return a wrong value ? ( -4)

Posted on 2004-08-31
267 Views
I found this problem in my web application, then I tested the following code in a windows application, same problem.

ArrayList list1 = new ArrayList();

ArrayList list2 = new ArrayList();

int nIdx=list1.BinarySearch("3");

for ( int j=0 ; j< list2.Count;j++)
{
string strNew= (string)list2[j];
if ( list1.BinarySearch(strNew) <0)
{
}
}

I expect list1 has object "3", "1", "2", but the result is "3","1","2","3"
the last time when strNew =="3", list1.BinarySearch(strNew) return -4. Normally if
it didn't find the object, it always return -1.

0
Question by:jackiechen858
• 4
• 3

LVL 37

Accepted Solution

gregoryyoung earned 100 total points
you are missing a ...

"Uses a binary search algorithm to locate a specific element in the sorted ArrayList or a portion of it."

key here is sorted

using System;
using System.Collections ;
namespace ConsoleApplication23
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
ArrayList list1 = new ArrayList();

ArrayList list2 = new ArrayList();

int nIdx=list1.BinarySearch("3");

for ( int j=0 ; j< list2.Count;j++) {
string strNew= (string)list2[j];
if ( list1.BinarySearch(strNew) <0) {
list1.Sort();
}
}
Console.WriteLine("list1.Count=" + list1.Count);
for(int i=0;i<list1.Count;i++) {
Console.WriteLine("List1 [" + i + "] = " + list1[i]);
}
}
}
}

gives the expected behavior ... you would be better off using a linear search most likely though if you are adding data this frequently (or using a sortedlist instead of an arraylist)
0

LVL 7

Author Comment

Thanks for your quick response Gregory.

I didn't read the document too carefully :-(

0

LVL 37

Expert Comment

the way binary searches work you need sorted data (are you familiar with the algorithm ?)

basically binary searches use a divide and conquer methodology it tosses out 1/2 of the possible entries everytime it runs (since you are dealing with whole numbers that goes by pretty quickly) ...

imagine you have a 1000 page dictionary and someone asks you to find the word "Visual"

open to page 500
first word is "mother"
you flip to page 750 (obviously its not in the first 500 pages)
first word is "testing"
you flip to page 875
you flip to page 813 and find it midway through ...

this is real life example ..

now imagine if after flipping up from mother you found the word "aardvark" ... it would really screw up how you were searching.

0

LVL 7

Author Comment

Yeah, I knew the sort algorithm. I  didn't realize binary search need
sorted data ( should know it but I just didn't think about it too much, maybe
they can  use some magic hash table so no need for sorting:-) ). acturally
I can't use binary search in my code now because I don't want to adjust the
object's sequence in the list. I use the arraylist as a kind of stack.

0

LVL 37

Expert Comment

why not use the stack object as a stack :)
0

LVL 7

Author Comment

Because  I didn't know there are Stack and Quere object in C# :-(
0

LVL 37

Expert Comment

0

## Featured Post

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.