ArrayList.BinarySearch

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pete Z

    #1

    ArrayList.BinarySearch

    Does anyone know why this snippet of code continues to return x with a value
    of -1?
    Is there an issue with ArrayList.Binar ySearch?

    ArrayList myAL = new ArrayList();

    myAL .Add(1);

    myAL .Add(2);

    myAL .Add(3);

    object oX;

    oX = 3;

    int x = myAL .BinarySearch(o X)



    According to the documentation, x should = 2. Being the index of where the
    value 3 exists in myAl.



    Regards

    Pete




  • William Ryan

    #2
    Re: ArrayList.Binar ySearch

    Pete:

    Stupid question but is this the actual example....ie is the list definitely
    sorted? If not, binary search does give some weird results...
    "Pete Z" <slim> wrote in message
    news:Of9bcAOpDH A.688@TK2MSFTNG P10.phx.gbl...[color=blue]
    > Does anyone know why this snippet of code continues to return x with a[/color]
    value[color=blue]
    > of -1?
    > Is there an issue with ArrayList.Binar ySearch?
    >
    > ArrayList myAL = new ArrayList();
    >
    > myAL .Add(1);
    >
    > myAL .Add(2);
    >
    > myAL .Add(3);
    >
    > object oX;
    >
    > oX = 3;
    >
    > int x = myAL .BinarySearch(o X)
    >
    >
    >
    > According to the documentation, x should = 2. Being the index of where[/color]
    the[color=blue]
    > value 3 exists in myAl.
    >
    >
    >
    > Regards
    >
    > Pete
    >
    >
    >
    >[/color]


    Comment

    • Pete Z

      #3
      Re: ArrayList.Binar ySearch

      Hi William,
      Does the ArrayList need to be sorted for BinarySearch to work?
      Is that the message I'm getting?

      Regards
      Pete

      "William Ryan" <dotnetguru@nos pam.comcast.net > wrote in message
      news:OgrHrVOpDH A.3040@TK2MSFTN GP11.phx.gbl...[color=blue]
      > Pete:
      >
      > Stupid question but is this the actual example....ie is the list[/color]
      definitely[color=blue]
      > sorted? If not, binary search does give some weird results...
      > "Pete Z" <slim> wrote in message
      > news:Of9bcAOpDH A.688@TK2MSFTNG P10.phx.gbl...[color=green]
      > > Does anyone know why this snippet of code continues to return x with a[/color]
      > value[color=green]
      > > of -1?
      > > Is there an issue with ArrayList.Binar ySearch?
      > >
      > > ArrayList myAL = new ArrayList();
      > >
      > > myAL .Add(1);
      > >
      > > myAL .Add(2);
      > >
      > > myAL .Add(3);
      > >
      > > object oX;
      > >
      > > oX = 3;
      > >
      > > int x = myAL .BinarySearch(o X)
      > >
      > >
      > >
      > > According to the documentation, x should = 2. Being the index of where[/color]
      > the[color=green]
      > > value 3 exists in myAl.
      > >
      > >
      > >
      > > Regards
      > >
      > > Pete
      > >
      > >
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: ArrayList.Binar ySearch

        <"Pete Z" <slim>> wrote:[color=blue]
        > Does the ArrayList need to be sorted for BinarySearch to work?
        > Is that the message I'm getting?[/color]

        It's the message from the documentation :)

        <quote>
        Uses a binary search algorithm to locate a specific element in the
        sorted ArrayList or a portion of it.
        </quote>

        If the list weren't sorted to start with, there's no way a binary
        search would work, if you think about how a binary search works.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Pete Z

          #5
          Re: ArrayList.Binar ySearch

          Thanks guys!
          Yes, it quite clearly states it on the first line of the documentation.
          Regards
          Pete


          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
          news:MPG.1a1558 44a3b04f51989a3 c@msnews.micros oft.com...[color=blue]
          > <"Pete Z" <slim>> wrote:[color=green]
          > > Does the ArrayList need to be sorted for BinarySearch to work?
          > > Is that the message I'm getting?[/color]
          >
          > It's the message from the documentation :)
          >
          > <quote>
          > Uses a binary search algorithm to locate a specific element in the
          > sorted ArrayList or a portion of it.
          > </quote>
          >
          > If the list weren't sorted to start with, there's no way a binary
          > search would work, if you think about how a binary search works.
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet
          > If replying to the group, please do not mail me too[/color]


          Comment

          Working...