Sorting Algorithm in JavaScript - Quicksort and Heap Sort
Last time we have finished the four different sorting algorithms including : insertion sort and selection sort, bubble sort and merge sort, check on Series section for these posts.
Now let’s continue playing with another one or two.
Median Sort and Quicksort
Median Sort
Similiar with merge sort, we still use divide and conquer, the basic approach for many problems, but what if we use some statistical information about the array that need to be sorted? Like the median of the array.
If we know the median, we can sort the array into 2 distinct subarrays of about half the size: left with all elements smaller than the median, and right with all elements bigger or equal to median. And we keep doing this for all subarrays, finally we will get a sorted array.
That gives us the MEDIAN SORT.
Quicksort
Median sort is a nice start, but it still has one problem: how to find the the median of an array? Before we atually put our efforts on solving this problem, we should consider about another problem: how about we use some other attributes instead of median? Our purpose is divide the array into two parts, we don’t need them to be the same size. By thinking this way, we may consider choose any value in the array and use this value as a separator and divide the array into subarrays.
This is quicksort, and the value we choose as a separator is called pivot.
Now let’s show the code:
1 | var quickSort = (list,left,right) => { |
With all comments, the code should be easy to understand.
Quicksort is famous and popular for its speed especially after linux start using it as the default sorting algorithm. Normally, if we know nothing about the distribution of our array and speed is the most important reason you consider about, then use quicksort.
In above example, we choose the pivot randomly. Normally, its good enough for using. But actually there are a lot of strategies and researches on how to choose a good pivot. Like always choose the first or last or middle, or use median, median-of-k…etc But normally, using randomly pivot will give you an average O(nlogn). If you want to learn more about these strategies, just google it :)
Heap Sort
Before we go to the concept and code, we should know what is a heap:
a heap is a specialized tree-based data structure that satisfies the heap property: If A is a parent node of B then the key of node A is ordered with respect to the key of node B with the same ordering applying across the heap.
In a max heap, the keys of parent nodes are always greater than or equal to those of the children and the highest key is in the root node. In a min heap, the keys of parent nodes are less than or equal to those of the children and the lowest key is in the root node.
Got any inspirations ? Think this way: Max heap => the head of the heap is the max of the array. This is true for any max heap, so we remove the max and rebuild a heap with rest elements, we get the second largest… Yeah, you got it ?! :)
Now what we need to do is using array represent the heap which is pretty much a array tree:
for any element with index - idx:
- left child : idx*2 + 1
- right child: idx*2 + 2
- Show me the code !!!
1 | // heapSort - here we use max heap |
Heap Sort is really fast, sometimes it is even faster than quicksort since it will guarantee the O(nlogn) even in the worst case. But normally in average case, the quicksort is a little faster.
BTW
I believe I still have 2 posts for this serie, one will talk about the radix, and the other will be the counting and bucket sort. See ya.