DEV Community

Discussion on: Common Sorting Algorithms in JavaScript

Collapse
 
wulymammoth profile image
David • Edited

Wow! You just keep firing! Nice work! I honestly don’t know the implementation for the slower sorts off the top of my head. What typically come to mind are O(n log n) sorting algos like quicksort, mergesort, and heapsort. Heap-sort has a lot of utility particularly on non-fixed size inputs (streams).

One note: I noticed that your merge function for mergesort could be improved — by not allocating a new array each time. The canonical implementation passes in the original array and replaces the elements in the original input array utilizing a third pointer within merge — this third pointer moves forward after a replacement and only one of the two pointers pointing at the smaller element of the two arrays being merged moves forward in each iteration. This halves the memory footprint of the current implementation.