Basic Sorting Algorithms
Bubble Sort
Two-pointer loop, swap when out of order, until the desired element floats to the boundary.
function bubbleSort(arr) {
const n = arr.length;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
}
Quick Sort
Key is how to implement partition. Common approach: choose the leftmost as pivot. First method: scan left to right, swap elements smaller than pivot to the left. Second method: two-pointer approach from both ends.
function swap(arr, i, j) {
[arr[i], arr[j]] = [arr[j], arr[i]];
}
function partitionMethod1(arr, left, right) {
let pivot = arr[left];
let storeIndex = left + 1;
for (let i = left + 1; i <= right; i++) {
if (arr[i] < pivot) {
swap(arr, i, storeIndex++);
}
}
swap(arr, left, storeIndex - 1);
return storeIndex - 1;
}
function partitionMethod2(arr, left, right) {
let pivot = arr[left];
while (left < right) {
while (left < right && arr[right] > pivot) right--;
arr[left] = arr[right];
while (left < right && arr[left] <= pivot) left++;
arr[right] = arr[left];
}
arr[left] = pivot;
return left;
}
function quickSort(arr, left = 0, right = arr.length - 1) {
if (left < right) {
const pivotIndex = partitionMethod2(arr, left, right);
quickSort(arr, left, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, right);
}
}
Selection Sort
function selectionSort(arr) {
for (let i = 0; i < arr.length; i++) {
let minIndex = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
[arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
}
}
Insertion Sort
function insertionSort(arr) {
for (let i = 1; i < arr.length; i++) {
let key = arr[i];
let j = i - 1;
while (j >= 0 && key < arr[j]) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
Shell Sort
function shellSort(arr) {
let gap = 1;
while (gap < arr.length / 3) {
gap = gap * 3 + 1;
}
while (gap >= 1) {
for (let i = gap; i < arr.length; i++) {
let temp = arr[i];
let j;
for (j = i - gap; j >= 0 && arr[j] > temp; j -= gap) {
arr[j + gap] = arr[j];
}
arr[j + gap] = temp;
}
gap = Math.trunc(gap / 3);
}
}
Merge Sort
function merge(left, right) {
let result = [];
while (left.length && right.length) {
if (left[0] < right[0]) {
result.push(left.shift());
} else {
result.push(right.shift());
}
}
return [...result, ...left, ...right];
}
function mergeSort(arr) {
if (arr.length < 2) return arr;
const mid = Math.trunc(arr.length / 2);
const left = arr.slice(0, mid);
const right = arr.slice(mid);
return merge(mergeSort(left), mergeSort(right));
}
Heap Sort
// Complete binary tree: first non-leaf index = n/2 - 1, parent index = (n-1)/2, children indices = n2+1, n2+2
function heapify(arr, i, len) {
let left = i * 2 + 1;
let right = i * 2 + 2;
let largest = i;
if (left < len && arr[left] > arr[largest]) largest = left;
if (right < len && arr[right] > arr[largest]) largest = right;
if (largest !== i) {
[arr[i], arr[largest]] = [arr[largest], arr[i]];
heapify(arr, largest, len);
}
}
function heapSort(arr) {
const len = arr.length;
// Build max heap
for (let i = Math.trunc((len - 1) / 2); i >= 0; i--) {
heapify(arr, i, len);
}
// Extract elements
for (let i = len - 1; i > 0; i--) {
[arr[0], arr[i]] = [arr[i], arr[0]];
heapify(arr, 0, i);
}
return arr;
}
Comlpexity

References
- Sorting algorithms and their time complexities
- Top ten classic sorting algorithms