MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Finding Position in an ordered list


On 8 Jun 2005, at 16:21, J?nos TîTH wrote:
>
> Dear All,
>
> 0. Now I disclose that I am looking for a word in a dictionary.
> 1. Thank for the BinarySearch idea. It is much faster than Position.
> 2. The idea of interpolation in the case of numbers is fantastic.
> 3. However, I tried BinarySearch[{a,b,c},b] without being evaluated.
>
> Thus, I need further help.
>
> Thank for all of you.
>
> Janos

BinarySearch in the Combinatorica package is written so that it  
accepts only numeric input.  It is very easy to change this:

BinarySearch[list_, elem_] := Module[{
     n0 = 1, n1 = Length[list], m}, While[n0 ² n1, m = Floor[(n0 +  
n1)/2];
       If[list[[m]] == elem,
     Return[m]]; If[OrderedQ[{list[[m]], elem}], n0 = m + 1, n1 = m -  
1] ];
     0  ]


BinarySearch[{a,b,c,},b]


2


BinarySearch[{a,b,c,},e]


0

0 means "not found".

Andrzej Kozlowski

Chiba, Japan




  • Prev by Date: Re: Solve with assumptions
  • Next by Date: Re: goto and label (cont)
  • Previous by thread: Re: Finding Position in an ordered list
  • Next by thread: Re: Finding Position in an ordered list