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.<int>; = Vector.<int>([ 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
Insert Number into a sequence
Monday, June 25, 2012
[Action Script] Insert Number into a sequence June,2012
Posted by Bimo Hery Prabowo at 7:13 AM
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment