Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Return the position of an item in a sorted list
ResourceFunction["SortedPosition"][l,item] finds the position of item in a sorted list l, returning bounding indices if it is not present. |
Find the position of 7 in a sorted list:
| In[1]:= |
| Out[1]= |
Find the bounding indices of 6 in a sorted list without 6:
| In[2]:= |
| Out[2]= |
Compare this to the behavior of BinarySearch:
| In[3]:= |
| Out[3]= |
Subsets returns ordered results; use this to find the position of a subset:
| In[4]:= |
| Out[5]= |
Check it:
| In[6]:= |
| Out[6]= |
From a sorted wordlist, find the position of "position":
| In[7]:= |
| Out[8]= |
Check it:
| In[9]:= |
| Out[9]= |
Find the bounding indices of a non-word not in the list:
| In[10]:= |
| Out[10]= |
Show the words above and below:
| In[11]:= |
| Out[11]= |
A sorted list of 10! or 3628800 items:
| In[12]:= |
Choose a random permutation:
| In[13]:= |
| Out[13]= |
Find it using SortedPosition:
| In[14]:= |
| Out[14]= |
Use Position to find a single permutation and time it:
| In[15]:= |
| Out[15]= |
Use FirstPosition to find a single permutation and time it:
| In[16]:= |
| Out[16]= |
For a sorted list with repeats, only one position is returned:
| In[17]:= |
| Out[17]= |
An item above the list is framed by an index one larger than the length of the list:
| In[18]:= |
| Out[18]= |
An item below the list is framed by zero and one:
| In[19]:= |
| Out[19]= |
An unsorted list:
| In[20]:= |
| Out[20]= |
Finding items in an unsorted list produces spurious results:
| In[21]:= |
| Out[21]= |
Finding an item in an empty list returns an index frame outside of the list:
| In[22]:= |
| Out[22]= |
Wolfram Language 14.0 (January 2024) or above
This work is licensed under a Creative Commons Attribution 4.0 International License