Sorting Algorithms in JavaScript
Sorting in general is arranging 'things' in a particular order. This particular order can either be ascending or descending. Sorting algorithms are instructions to define the particular order in which you want the 'things' sorting.
There are many different sorting algorithms, but in this article I will be discussing Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, Quick Sort and efficiency of each.
Bubble Sort // O(n^2) - nested loops
Code snippet:
const numbers = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0]
function bubbleSort(array) {
for (let i=0; i<array.length; i++) {
for (let j=0; j<array.length; j++) {
if (array[j]>array[j+1]) {
let temp = array[j];
array[j] = array[j+1];
array[j+1] = temp
}
}
}
} // O(n^2) - here we have nested loops to compare each element and bubble up through
bubbleSort(numbers);
console.log(numbers);
Selection Sort - O(n^2) nested loops
Code Snippet
const numbers = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0]
function selectionSort(array) {
for (let i=0; i<array.length; i++) {
let min = i;
let temp = array[i];
for (let j=i+1; j<array.length; j++) {
if (array[j]<array[min]) {
min = j;
}
}
array[i] = array[min];
array[min] = temp;
}
} //O(n^2) - nested loops again.
selectionSort(numbers);
console.log(numbers);
Insertion Sort - O(n^2) nested loops
Code Snippet
const numbers = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0];
function insertionSort(arr){
//Start from the second element.
for (let i = 1; i < arr.length; i++) {
//Go through the elements behind it.
for (let j = i - 1; j > -1; j--) {
if (arr[j + 1] < arr[j]) {
let temp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = temp
}
}
}
return arr;
}
insertionSort(numbers);
console.log(numbers);
Recommended by LinkedIn
Merge Sort - O(n*log(n)) DIVIDE & CONQUER Technique
Code Snippet
const numbers = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0]
function mergeSort(array) {
//base case
if (array.length === 1) {
return array
}
//split array into right and left
const middle = Math.floor(array.length / 2);
const left = array.slice(0,middle);
const right = array.slice(middle);
//use recursion here to keep splitting array until length 1.
return merge(
mergeSort(left),
mergeSort(right)
)
}
function merge(left, right){
const result = [];
let leftIndex = 0;
let rightIndex = 0;
while (leftIndex < left.length && rightIndex < right.length) {
if (left[leftIndex] < right[rightIndex]) {
result.push(left[leftIndex]);
leftIndex++
} else {
result.push(right[rightIndex]);
rightIndex++
}
}
return result.concat(left.slice(leftIndex)).concat(right.slice(rightIndex));
}
const answer = mergeSort(numbers);
console.log(answer);
Quick Sort - O(n*log(n)) DIVIDE & CONQUER Technique
Quick sort is another divide & conquer algorithm which picks an element as pivot and partitions the given array around the picked pivot.
There are many different versions of quick sort that pick the pivot in different ways e.g.
This method can be quite complex and cumbersome and so I've omitted the implementation of this. More information on this can be found here: https://www.geeksforgeeks.org/quick-sort/
Some Rules to follow and think about:
Overview & additional information
With an understanding of sorting algorithms, software developers can figure out the appropriate sorting algorithm to use depending on the data set, the time required, and the space available.
Although merge sort and quick sort are harder to implement, they are the most often used sorting algorithms in real life. They use divide and conquer to get O(n*log(n)) time complexity.
In JavaScript, the sort() method uses insertion sort and so is not suitable for large data sets. That is why when we deal with large data sets, we should consider other sorting algorithms.
Mathematically it is impossible to be quicker than O(n*log(n)) due to making comparison sorts, but there are a small selection of inputs which are non-comparison sorts.
This is an entirely different way to think about sorting and these methods have better time complexities. It can get very complex and not worth myself knowing for the time being but it is useful to know they are options and this exists.