Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*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 2006

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

Search the Archive

Re: Insertion into sorted list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg71114] Re: Insertion into sorted list
  • From: "Jens-Peer Kuska" <kuska at informatik.uni-leipzig.de>
  • Date: Thu, 9 Nov 2006 03:37:08 -0500 (EST)
  • Organization: Uni Leipzig
  • References: <eimq10$9b7$1@smc.vnet.net>

Hi,

SortedInsert[lst_, elem_] :=
  Block[{less},
  less = Select[lst, # < elem &];
  Join[Select[lst, # < elem &], {elem}, 
Complement[lst, less]]
 ]

lst = {1, 2, 4, 8, 16};

SortedInsert[lst,5]

Regards

  Jens

"Andrew Moylan" <andrew.j.moylan at gmail.com> 
schrieb im Newsbeitrag 
news:eimq10$9b7$1 at smc.vnet.net...
| 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
| 



  • Prev by Date: Re: Subscript assignment incompatibility??
  • Next by Date: Re: Points sampled by FindMinimum
  • Previous by thread: Re: Insertion into sorted list
  • Next by thread: Possible simple bug in NMaximize