Home Tutorials Forums Articles Blogs Movies Library Employment Press

 ActionScript.org Forums Insert Number into a sequence
 Register FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

 06-24-2012, 12:42 AM #1 midnightgreen Member   Join Date: Jun 2009 Location: UK Posts: 94 Insert Number into a sequence I am looking for ideas to write a function that can insert an arbitary number into an arbitary sequence of numbers in an array, so that the result is in an ascending order. ActionScript Code: ```var _objects:Vector.; = Vector.([ 1, 2, 4, 6, 7 ]); insertNum(5); // i want this: 1, 2, 4, 5, 6, 7 ``` - I first thought of looping through the array and inserting the number after the first number that it is larger, but that would result in: 1, 2, 5, 4, 6, 7, where the 5 precedes 4. Similarly if I loop backwards, and I have the number 3 for example, then the result could be 1, 2, 5, 3, 6, 7 NOTE: Using sort, or sortOn() is NOT a possibility (as it is a simplified example, in the real case these numbers would be properties on a vector array of Objects) PS: the array can only receive new elements using this method, so it will always be sorted, although it may have gaps Last edited by midnightgreen; 06-24-2012 at 10:05 AM.
 06-24-2012, 12:58 AM #2 [afz]snickelfitz Senior Member     Join Date: Dec 2011 Location: Tucson, AZ Posts: 1,888 Try this: ActionScript Code: ```var arr:Vector. = Vector.([ 1, 2, 4, 6, 7 ]); insertNum(5); // i want this: 1, 2, 4, 5, 6, 7 function insertNum(value:int):void {     var n:int = arr.length;         for (var i:int = 0; i < n; i++)     {         if(value > arr[i] && value < arr[i+1])         {             arr.splice(i+1,0,value);             trace(arr);         }     } }```
 06-24-2012, 04:09 AM #3 newblack dondeEstanMisPantalones?     Join Date: Nov 2005 Location: New York Proper Posts: 1,355 Your examples don't make sense. If you're asking if you can insert in less than linear time, you have to have an already sorted list and do a binary search for the insertion point, which is O(log n). __________________ i am gibreel farishta general relativity jellytanks alpha redux
 06-24-2012, 09:36 AM #4 midnightgreen Member   Join Date: Jun 2009 Location: UK Posts: 94 newblack: Yes I forgot to mention, the array can only receive new elements using this method, so it will always be sorted. however any element can be removed, so while the list will remain sorted, there migth be gaps Last edited by midnightgreen; 06-24-2012 at 09:49 AM.
 06-24-2012, 09:49 AM #5 midnightgreen Member   Join Date: Jun 2009 Location: UK Posts: 94 [afz]snickelfitz Yeah, actually this seems promising! will only need to take care of the situation for arrays shorter than 2, as well as when you get to the end of the array (so i+1 would be out of range )

 Thread Tools Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home ActionScript Forums Group     ActionScript 3.0     ActionScript 2.0     ActionScript 1.0 (and below)     Simple Stuff (Newbies) Flash General Questions     Flash 10 General Questions     Flash 9 General Questions     Flash 8 General Questions     Other Flash General Questions Flex     Flex 2, 3 & 4     Flex 1 Extensions and Plugins     Components     JSFL - Extending Flash Desktop, Mobile and non-browser Environments     AIR (Apollo)     FlashLite / Portable Devices Development     Projectors and CDs Supporting Technologies     HTML and JavaScript     haXe     Server-Side Scripting     Flash Remoting     Flash Media Server General     Best Practices     Gaming and Game Development     Animation and Effects     Flashants Support Forum Community Boards     General Chat     Just for Kicks Challenges     Detention Flash In Action     Site Check     Cool Sites     Widgets Decommissioned     Projects and Positions CMS Forums     Announcements Board     Content Postings / Updates     Product Review Requests     CMS Technical Questions     Process Questions     Collaboration & Suggested Articles

All times are GMT. The time now is 10:29 PM.

///