Re: Insertion into sorted list
- To: mathgroup at smc.vnet.net
- Subject: [mg71078] Re: [mg71027] Insertion into sorted list
- From: János <janos.lobb at yale.edu>
- Date: Wed, 8 Nov 2006 06:04:24 -0500 (EST)
- References: <200611060752.CAA08537@smc.vnet.net>
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. János
- References:
- Insertion into sorted list
- From: "Andrew Moylan" <andrew.j.moylan@gmail.com>
- Insertion into sorted list