Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

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

Search the Archive

Re: Insertion into sorted list

  • To: mathgroup at
  • Subject: [mg71078] Re: [mg71027] Insertion into sorted list
  • From: János <janos.lobb at>
  • Date: Wed, 8 Nov 2006 06:12:53 -0500 (EST)
  • References: <>

On Nov 6, 2006, at 2:52 AM, Andrew Moylan wrote:

> Hi all,
> I have a sorted List. I need to occasionally insert new elements,  
> while
> always keeping the list sorted. Is there a more efficient way to do
> this than the most obvious way (calling Append then calling Sort)?
> Thanks for any help,
> Andrew

With MemberQ you can check if the value is already there or not.
If it is there, then you find the first such value with Position and  
use Insert to insert the vale.

If it is not there then you can "walk" in the list - binary walking  
is fast - to find the values that are "surround" your vale and use  
Insert again.


  • Prev by Date: Efficiency of repeatedly appending to a List
  • Next by Date: CleanSlate
  • Previous by thread: Re: Insertion into sorted list
  • Next by thread: Re: Insertion into sorted list