Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1993
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1993

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

Search the Archive

Searching in Mathematica

  • To: mathgroup at yoda.physics.unc.edu
  • Subject: Searching in Mathematica
  • From: Knut M|rken <knutm at ifi.uio.no>
  • Date: Wed, 17 Mar 93 16:20:27 +0100

Given a list t of numbers, sorted in increasing order, and a number x,
what is the best way to find an integer m such that t[[m]] <= x < t[[m]]?

The obvious (?) way is to use the function Position as in

Last[ Position[t, _?( # <= x& )] ] [[1]]

and this works OK, but is slow if the list is long, since Position doesn't
know that the list is sorted. I have written a binary search routine
that does this much faster for long t, but somehow feel that searching
in a sorted list is so fundamental that there ought to be a builtin function
for this operation.

Any comments?

Knut Morken

Dept. of Informatics
University of Oslo
Norway
email: knutm at ifi.uio.no





  • Prev by Date: Mathematica mailing list
  • Next by Date: Factoring polynomials over the reals
  • Previous by thread: Mathematica mailing list
  • Next by thread: Factoring polynomials over the reals