Function Repository Resource:

SortedPosition

Source Notebook

Return the position of an item in a sorted list

Contributed by: Ed Pegg Jr

ResourceFunction["SortedPosition"][l,item]

finds the position of item in a sorted list l, returning bounding indices if it is not present.

Details

ResourceFunction["SortedPosition"] is faster than Position or FirstPosition for sufficiently long, sorted lists, but slower for shorter lists. For longer lists, Position operates linearly (O(n)), while ResourceFunction["SortedPosition"] operates logarithmatically (O(logn)).
ResourceFunction["SortedPosition"] only works reliably on sorted lists where Sort[list] = list.

Examples

Basic Examples (4) 

Find the position of 7 in a sorted list:

In[1]:=
ResourceFunction["SortedPosition"][{1, 2, 5, 5, 7, 12}, 7]
Out[1]=

Find the bounding indices of 6 in a sorted list without 6:

In[2]:=
ResourceFunction["SortedPosition"][{1, 2, 5, 5, 7, 12}, 6]
Out[2]=

Compare this to the behavior of BinarySearch:

In[3]:=
ResourceFunction["BinarySearch"][{1, 2, 5, 5, 7, 12}, 6]
Out[3]=

Subsets returns ordered results; use this to find the position of a subset:

In[4]:=
subs = Subsets[Range[7], {3}];
ResourceFunction["SortedPosition"][subs, {2, 4, 6}]
Out[5]=

Check it:

In[6]:=
subs[[21]]
Out[6]=

From a sorted wordlist, find the position of "position":

In[7]:=
words = WordList[];
ResourceFunction["SortedPosition"][words, "position"]
Out[8]=

Check it:

In[9]:=
words[[25902]]
Out[9]=

Find the bounding indices of a non-word not in the list:

In[10]:=
ResourceFunction["SortedPosition"][words, "positiottttttt"]
Out[10]=

Show the words above and below:

In[11]:=
words[[%]]
Out[11]=

Scope (5) 

A sorted list of 10! or 3628800 items:

In[12]:=
perms = Permutations[Range[10]];

Choose a random permutation:

In[13]:=
rand = RandomSample[Range[10]]
Out[13]=

Find it using SortedPosition:

In[14]:=
RepeatedTiming[ResourceFunction["SortedPosition"][perms, rand]]
Out[14]=

Use Position to find a single permutation and time it:

In[15]:=
RepeatedTiming[Position[perms, rand]]
Out[15]=

Use FirstPosition to find a single permutation and time it:

In[16]:=
RepeatedTiming[FirstPosition[perms, rand]]
Out[16]=

Possible Issues (4) 

For a sorted list with repeats, only one position is returned:

In[17]:=
ResourceFunction["SortedPosition"][{1, 2, 4, 4, 4, 4, 7, 9, 12}, 4]
Out[17]=

An item above the list is framed by an index one larger than the length of the list:

In[18]:=
ResourceFunction["SortedPosition"][{1, 2, 3}, 9]
Out[18]=

An item below the list is framed by zero and one:

In[19]:=
ResourceFunction["SortedPosition"][{1, 2, 3}, -9]
Out[19]=

An unsorted list:

In[20]:=
list = RandomSample[Range[1000], 20]
Out[20]=

Finding items in an unsorted list produces spurious results:

In[21]:=
ResourceFunction["SortedPosition"][list, #] & /@ list
Out[21]=

Finding an item in an empty list returns an index frame outside of the list:

In[22]:=
ResourceFunction["SortedPosition"][{{}}, 4]
Out[22]=

Requirements

Wolfram Language 14.0 (January 2024) or above

Version History

  • 1.0.0 – 03 January 2025

Related Resources

License Information