MathGroup Archive 2005

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

Search the Archive

Re: Re: Finding Position in an ordered list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg57800] Re: [mg57770] Re: [mg57720] Finding Position in an ordered list
  • From: Andrzej Kozlowski <andrzej at akikoz.net>
  • Date: Thu, 9 Jun 2005 05:17:33 -0400 (EDT)
  • References: <200506060821.EAA12541@smc.vnet.net> <76e8f81805060620315232bc54@mail.gmail.com> <200506080721.DAA11546@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

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