Efficient way to insert a number into a sorted array of numbers?
Solution 1
Just as a single data point, for kicks I tested this out inserting 1000 random elements into an array of 100,000 pre-sorted numbers using the two methods using Chrome on Windows 7:
First Method:
~54 milliseconds
Second Method:
~57 seconds
So, at least on this setup, the native method doesn't make up for it. This is true even for small data sets, inserting 100 elements into an array of 1000:
First Method:
1 milliseconds
Second Method:
34 milliseconds
Solution 2
Simple (Demo):
function sortedIndex(array, value) {
var low = 0,
high = array.length;
while (low < high) {
var mid = (low + high) >>> 1;
if (array[mid] < value) low = mid + 1;
else high = mid;
}
return low;
}
Solution 3
Very good and remarkable question with a very interesting discussion! I also was using the Array.sort()
function after pushing a single element in an array with some thousands of objects.
I had to extend your locationOf
function for my purpose because of having complex objects and therefore the need for a compare function like in Array.sort()
:
function locationOf(element, array, comparer, start, end) {
if (array.length === 0)
return -1;
start = start || 0;
end = end || array.length;
var pivot = (start + end) >> 1; // should be faster than dividing by 2
var c = comparer(element, array[pivot]);
if (end - start <= 1) return c == -1 ? pivot - 1 : pivot;
switch (c) {
case -1: return locationOf(element, array, comparer, start, pivot);
case 0: return pivot;
case 1: return locationOf(element, array, comparer, pivot, end);
};
};
// sample for objects like {lastName: 'Miller', ...}
var patientCompare = function (a, b) {
if (a.lastName < b.lastName) return -1;
if (a.lastName > b.lastName) return 1;
return 0;
};
Solution 4
There's a bug in your code. It should read:
function locationOf(element, array, start, end) {
start = start || 0;
end = end || array.length;
var pivot = parseInt(start + (end - start) / 2, 10);
if (array[pivot] === element) return pivot;
if (end - start <= 1)
return array[pivot] > element ? pivot - 1 : pivot;
if (array[pivot] < element) {
return locationOf(element, array, pivot, end);
} else {
return locationOf(element, array, start, pivot);
}
}
Without this fix the code will never be able to insert an element at the beginning of the array.
Solution 5
I know this is an old question that has an answer already, and there are a number of other decent answers. I see some answers that propose that you can solve this problem by looking up the correct insertion index in O(log n) - you can, but you can't insert in that time, because the array needs to be partially copied out to make space.
Bottom line: If you really need O(log n) inserts and deletes into a sorted array, you need a different data structure - not an array. You should use a B-Tree. The performance gains you will get from using a B-Tree for a large data set, will dwarf any of the improvements offered here.
If you must use an array. I offer the following code, based on insertion sort, which works, if and only if the array is already sorted. This is useful for the case when you need to resort after every insert:
function addAndSort(arr, val) {
arr.push(val);
for (i = arr.length - 1; i > 0 && arr[i] < arr[i-1]; i--) {
var tmp = arr[i];
arr[i] = arr[i-1];
arr[i-1] = tmp;
}
return arr;
}
It should operate in O(n), which I think is the best you can do. Would be nicer if js supported multiple assignment. here's an example to play with:
Update:
this might be faster:
function addAndSort2(arr, val) {
arr.push(val);
i = arr.length - 1;
item = arr[i];
while (i > 0 && item < arr[i-1]) {
arr[i] = arr[i-1];
i -= 1;
}
arr[i] = item;
return arr;
}
Update 2
@terrymorse pointed out in the comments that javascripts Array.splice method is crazy fast, and it's more than just constant improvement in the time complexity. It seems some linked list magic is being used. It means you still do need a different data structure than a plain array - just that javascript arrays might provide that different data structure natively.
Related videos on Youtube
Comments
-
Rotartsi almost 2 years
I have a sorted JavaScript array, and want to insert one more item into the array such the resulting array remains sorted. I could certainly implement a simple quicksort-style insertion function:
var array = [1,2,3,4,5,6,7,8,9]; var element = 3.5; function insert(element, array) { array.splice(locationOf(element, array) + 1, 0, element); return array; } function locationOf(element, array, start, end) { start = start || 0; end = end || array.length; var pivot = parseInt(start + (end - start) / 2, 10); if (end-start <= 1 || array[pivot] === element) return pivot; if (array[pivot] < element) { return locationOf(element, array, pivot, end); } else { return locationOf(element, array, start, pivot); } } console.log(insert(element, array));
[WARNING] this code has a bug when trying to insert to the beginning of the array, e.g.
insert(2, [3, 7 ,9]
) produces incorrect [ 3, 2, 7, 9 ].However, I noticed that implementations of the Array.sort function might potentially do this for me, and natively:
var array = [1,2,3,4,5,6,7,8,9]; var element = 3.5; function insert(element, array) { array.push(element); array.sort(function(a, b) { return a - b; }); return array; } console.log(insert(element, array));
Is there a good reason to choose the first implementation over the second?
Edit: Note that for the general case, an O(log(n)) insertion (as implemented in the first example) will be faster than a generic sorting algorithm; however this is not necessarily the case for JavaScript in particular. Note that:
- Best case for several insertion algorithms is O(n), which is still significantly different from O(log(n)), but not quite as bad as O(n log(n)) as mentioned below. It would come down to the particular sorting algorithm used (see Javascript Array.sort implementation?)
- The sort method in JavaScript is a native function, so potentially realizing huge benefits -- O(log(n)) with a huge coefficient can still be much worse than O(n) for reasonably sized data sets.
-
Breton over 14 yearsusing splice in the second implementation is a bit wasteful. Why not use push?
-
Rotartsi over 14 yearsGood point, I just copied it from the first.
-
Ankit Roy over 13 yearsAnything containing
splice()
(e.g. your 1st example) is already O(n). Even if it doesn't internally create a new copy of the entire array, it potentially has to shunt all n items back 1 position if the element is to be inserted in position 0. Maybe it's fast because it's a native function and the constant is low, but it's O(n) nonetheless. -
Charlie Parker about 10 yearswhat does start||0 suppose to do?
-
Charlie Parker about 10 yearsalso, for future reference for people using this code, the code has a bug when trying to insert to the beginning of the array. Look further down for the corrected code.
-
Hubert Schölnast over 6 yearsDon't use
parseInt
useMath.floor
instead.Math.floor
is much faster thanparseInt
: jsperf.com/test-parseint-and-math-floor -
Khamaseen almost 3 years@j_random_hacker , array.push() would also reserve a space, copy, and place the pushed value. Not much of a difference to splice() then?
-
Ankit Roy almost 3 years@Khamaseen:
array.push()
always adds the new element at the end, so it doesn't need to reposition any existing elements. If the internal allocation size increases geometrically (e.g., doubling each time we run out of space), then insertions will be amortised O(1) time. -
Khamaseen almost 3 years@j_random_hacker , interesting I thought it would needed to copy. However it are more like ArrayList then? In that case a splice would also not bother that much?
-
Ankit Roy almost 3 years@Khamaseen: I don't know what ArrayList you're referring to, or how it's implemented.
array.splice()
necessarily has to move ( = copy) all elements to the right of the inserted element, and this necessarily takes time linear in the number of those elements.array.push()
does not, because it needs to move ( = copy) zero such elements.array.push()
might be implemented to always reallocate and copy, in which case it would indeed take linear time (in the length of the entire array) each time, but that would be a bad choice of implementation. -
Khamaseen almost 3 years@j_random_hacker, I apparently didn't know much about arrays in Javascript. Bottomline to this questioning about splice() is, yes most likely O(n). And push(), depending, most likely there is no copying indeed. Javascripts arrays are implemented differently, though often it seems to be Hashmap kind of structure, or even sometimes a binary tree kind of structure. Okay, didn't know. Cool.
-
Miguel Angelo over 2 yearsIf Array is implemented as some balanced tree (AVL, RB, B, ...) then most of these operations are O(log(n)). Specifically, B-trees block sizes can be optimized for the CPU cache line size.
-
Miguel Angelo over 2 yearsIf Array further optimizes given the functions you use on it, then it could be O(1) most often, e.g. if you use push/shift, then it uses a queue internally, if push/pop then it uses a stack, this all works by pre-allocating memory. If you use a lot of splicing, then a b-tree is perfect, if you only use indexing, then a vector is used. More optimizations are possible with "views", which works great with static content, like strings, data-arrays, in which you never really operate on the original object, but create views into that object.
-
Miguel Angelo over 2 yearsThe language engine can also help by tracking where these arrays are created, and using the optimal implementation right from the creation moment. JavaScript engines these days have 3+ levels of optimization, it could be done! In the 1st level, where things are still interpreted, it could detect these patterns. And when level 3 optimization arrives, it could hard-code the specific structure there.
-
Miguel Angelo over 2 years@Khamaseen as you said, a hashmap is also a good idea, for sparse arrays, or arrays which have attributes added. If the same set of attributes are always added, it could be turned into a C-like struct in the implementation level, at one of these optimization levels.
-
Ankit Roy over 13 years+1 because anything containing
splice()
is already O(n). Even if it doesn't internally create a new copy of the entire array, it potentially has to shunt all n items back 1 position if the element is to be inserted in position 0. -
njzk2 over 11 yearsyes, but no, it depends on your sort algorithm. Using a bubble sort in the reverse order, your sort if the last element is not sorted is always in o(n)
-
njzk2 over 11 yearsarrays.sort sounds quite terrible
-
Charlie Parker about 10 yearswhy are you or-ing a int with 0? i.e. what does start || 0 do?
-
SuperNova about 10 years@Pinocchio: start || 0 is a short equivalent of: if(!start) start = 0; - However, the "longer" version is more efficent, because it does not assign a variable to itself.
-
garyrob about 10 yearsIt seems worth noting, for the record, that this version DOES work correctly when trying to insert to the beginning of the array. (It's worth mentioning it because the version in the original question has a bug and doesn't work correctly for that case.)
-
Niel almost 9 yearsI'm not sure if my implementation was different, but I needed to change the ternary to
return c == -1 ? pivot : pivot + 1;
in order to return the correct index. Otherwise for an array with length 1 the function would return -1 or 0. -
gnasher729 almost 9 yearsSeems that the array.splice must be doing something really clever, to insert a single element within 54 microseconds.
-
kwrl almost 9 yearsIt depends on the logic you want. Mine was "return the index of the element which is lower or equal to the element".
-
Jackson over 8 yearsNice touch. I never heard of using bitwise operators to find the mid value of two numbers. Normally I would just multiply by 0.5. Is there a significant performance boost doing it this way?
-
Qwerty over 8 years@Jackson
x >>> 1
is binary right shift by 1 position, which is effectively just a division by 2. e.g. for 11:1011
->101
results to 5. -
yckart about 8 years
-
asherkin about 8 years
>>>
is an unsigned right shift, whereas>>
is sign-extending - it all boils down to in-memory representation of negative numbers, where the high bit is set if negative. So if you shift0b1000
right 1 place with>>
you'll get0b1100
, if you instead use>>>
you'll get0b0100
. While in the case given in the answer it doesn't really matter (the number being shifted with neither be larger than a signed 32-bit positive integer's max value nor negative), it's important to use the right one in those two cases (you need to pick which case you need to handle). -
James about 8 yearsYour code will not work for fractions, negative numbers or large integers because the >> bitwise operators casts to unsigned 32 bit. You should use '/ 2' instead of '>>> 1', I'd be surprised if it were slower.
-
kwrl about 8 years@James: The parameters start and end are only used on recursive call and will not be used on inital call. Because these are index values for the array they must be of type integer and on recursive call this is implicitly given.
-
James about 8 years@kwehrle Yes, you are right, my mistake, >> is good in this case.
-
gilly3 almost 8 years@asherkin - This is not right: "if you shift
0b1000
right 1 place with>>
you'll get0b1100
". No, you get0b0100
. The result of the different right shift operators will be the same for all values except negative numbers and numbers greater than 2^31 (ie, numbers with a 1 in the first bit). -
asherkin almost 8 years@gilly3 That example was using 4-bit numbers for the sake of brevity, not 32-bit ones - the explanation where it matters with 32-bit numbers (the same one you just gave me) is in my comment if you read to the end rather than jumping to respond.
-
gilly3 almost 8 yearsOk, I see how that helps to illustrate. I think it's important to clarify that your example is illustrative only, and that in practice all numbers are 32 bits when doing binary operations in JavaScript. The JavaScript bitwise operators first convert all input values to 32-bit integers.
-
The Red Pea about 7 yearsIs the difference between those operators in Java, the same as the difference between those operators in Javascript? I think so. stackoverflow.com/questions/2811319/difference-between-and Also, @yckart links off SO, but there's an example of
>>
right here in this thread, which it expects is faster than>>>
-
The Red Pea about 7 yearskwl says "var pivot = (start + end) >> 1; // should be faster than the above calculation" -- does he mean that
>>
should be faster than>>>
? -
kwrl about 7 years@TheRedPea: no, I meant
>> 1
should be faster (or not slower) than/ 2
-
The Red Pea about 7 yearsThanks. I guess I've read elsewhere people suggest ("just use
/ 2
, it results in a shift equivalent in the bytecode anyway = same performance; write your code for humans, not for computers' optimizations"). I guess this is why you said "(or not slower)". Agree? Thanks. -
domoarigato about 7 yearsthe call to
arr.splice()
is surely O(n) time complexity. -
domoarigato about 7 yearsI believe insertion sort is also O(n) best case, and O(n^2) worst case (though the OP's use case is probably best case).
-
Pasha Skender over 6 years@TheRedPea unless humans start compiling and executing code, you're never really writing code for humans.
-
Ian over 6 years@gnasher729 - I don't think Javascript arrays are really the same as physically continuous arrays like we have in C. I think the JS engines can implement them as a hash map/dictionary enabling the quick insert.
-
eXavier about 6 yearsI can see a potential issue with the result of
comparer
function. In this algorithm it is compared to+-1
but it could be arbitrary value<0
/>0
. See compare function. The problematic part is not only theswitch
statement but also the line:if (end - start <= 1) return c == -1 ? pivot - 1 : pivot;
wherec
is compared to-1
as well. -
kwrl about 6 years@eXavier: Your absolutely right that in some cases - if there are numerical values to compare where I can simply subtract them to get an sort order - this could be a problem. I always assumed a "clean" comparer function returning just values of
[-1, 0, +1]
. For your case the call to compare could be extended tovar c = Math.sign(comparer(element, array[pivot]));
. But this - and also my - version would fail, if someone provides an exotic comparer returning non numerical values. :( So I stay with my assumption, that the comparer should return only the three mentioned values. -
eXavier about 6 years@kwrl OK, I understand but I still think you should mention such assumption explicitly in your answer (it's a bit lost here in comments). Real scenario: I used your algorithm in a function and it worked well as I was using it with array of numbers (BTW thanks for sharing). Later on I imported that function when I needed to sort array of objects by one of its numeric property and used the subtraction comparer which accidentally worked for 2 or 3 items. So I realized the sorting issue later in dev cycle. My fault, of course, but my comment was meant to save others some time..
-
aleclarson almost 6 yearswhen you use a comparator function with
Array.prototype.sort
, you lose the benefits of C++ because the JS function is called so much. -
trincot about 5 yearsIn JavaScript the insertion sort you propose will be slower than the binary search & splice method, because splice has a fast implementation.
-
domoarigato about 5 yearsunless javascript somehow can break the laws of time complexity, i'm skeptical . Do you have a runnable example of how the binary search and splice method is faster?
-
trincot about 5 yearsI take back my second comment ;-) Indeed, there will be an array size beyond which a B-tree solution will outperform the splice solution.
-
Matt Zera over 4 yearsMinus one for talking down to the OP. The first paragraph felt like an unnessessary admonishment of for not knowing how splice works under the hood
-
qwr over 4 yearsWhy not test data structures designed to implement fast insertion and searching? ex. skip lists and BSTs. stackoverflow.com/a/59870937/3163618
-
greybeard over 4 yearsHow does this answer
Is there a good reason to choose [splice into location found] over [push & sort]?
-
qwr over 4 years@greybeard It answers the title. cynically neither choice is efficient.
-
qwr over 4 yearsNeither option could be efficient if they involve copying many elements of an array over.
-
poshest over 4 yearsHow does Native compare now that Chrome uses TimSort? From TimSort Wikipedia: "In the best case, which occurs when the input is already sorted, it runs in linear time".
-
poshest over 4 yearsHow does the First Method compare now that Chrome uses TimSort? From TimSort Wikipedia: "In the best case, which occurs when the input is already sorted, [TimSort] runs in linear time".
-
NemPlayer about 4 yearsIt's worth noting that inserting an element into an array has a complexity of O(n), so the end result should be about the same.
-
Redu over 3 yearsThis is good but probably it could have been better if you had defined
mid
in the function context rather than inside thewhile
loop. It gets defined many times over and over and this might form a culprit for the performance in large arrays. -
terrymorse about 3 yearsThis "array.push, bubble-swap" method is O(n). It is much slower than the "binary search, Array#splice" method, which is O(log n) (splice takes almost no time). On a large array, it is over 100 times faster. See speed benchmark test.
-
domoarigato about 3 years@terrymorse unfortunately, Array.splice is not O(log n), it is also O(n) which was the main point of my answer.
-
domoarigato about 3 yearsThe code I offered is indeed slower than an implementation using splice, because there are some optimizations in the native code, however, the difference is a constant difference, ie. O(n/k), not a difference in the fundamental complexity order.
-
terrymorse about 3 years@domoarigato Performance test shows dramatically that insertion with Array.splice is much less than O(N). Time/N decreases for every increase in
N
between 100 and 100,000. -
domoarigato about 3 yearshmm, it seems that javascript might have implemented some linked list type magic under the hood: stackoverflow.com/questions/5175925/…. Thats really cool - but it also just means that a javascript array is not "just an array" and so reinforces the fundamental point of my answer, which that you need another data structure. Thanks for pointing this out @terrymorse
-
Flavio over 2 yearsVery nice. Well done, sir! :)
-
Armen Michaeli about 2 yearsI struggle to see an answer here. What is meant by "the two methods"? The answer reads like a comment on another answer, I must be missing something very obvious since it has received dozens of votes.
-
Felix B. about 2 yearsThe way this optimization might be related to memory blocks (cf. youtu.be/443UNeGrFoM?t=4975 (at time 1:22:55)) I.e. memory is not contiguous so if you want to insert something. Anyway that video is always great to watch if you are confused about datastructure magic
-
AnorZaken almost 2 yearsIf you use
/ 2
to get mid, don't forget to floor it:Math.floor((low + high) / 2);
-
l3l_aze almost 2 years@amn There are 2 examples in the post, so they must've tested those.