Common Sort Algorithms - JavaScript

Following lists 5 common sort algorithms JavaScript implementation.


Bubble Sort

function bubble_sort(arr) {
    if (arr.length <= 1) return arr;
    var alen = arr.length;
    for (var i = 0; i < alen; i++) {
        for (var j = 0; j < alen - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                var temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
    return arr;
}

var arr = [3, 8, 9, 1, 2, 6, 4, 7, 5];
bubble_sort(arr);

Quick Sort

function quick_sort(arr) {
    if (arr.length <= 1) return arr;
    var pivot = Math.floor((arr.length - 1) / 2),
        pivotValue = arr[pivot],
        left = [],
        right = [];
    arr = arr.slice(0, pivot).concat(arr.slice(pivot + 1));
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] < pivotValue) {
            left.push(arr[i]);
        } else {
            right.push(arr[i]);
        }
    }
    return [].concat(quick_sort(left), [pivotValue], quick_sort(right));
}

var arr = [3, 8, 9, 1, 2, 6, 4, 7, 5];
quick_sort(arr);

Merge Sort

function merge(left, right) {
    var result = [];
    while (left.length && right.length) {
        if (left[0] < right[0]) {
            result.push(left.shift());
        } else {
            result.push(right.shift());
        }
    }
    return result.concat(left).concat(right);
}

function merge_sort(arr) {
    if (arr.length <= 1) return arr;
    var mid = Math.floor(arr.length / 2),
        left = arr.slice(0, mid),
        right = arr.slice(mid);
    return merge(merge_sort(left), merge_sort(right));
}

var arr = [3, 8, 9, 1, 2, 6, 4, 7, 5];
merge_sort(arr);

Selection Sort

function selection_sort(arr) {
    for (var i = 0; i < arr.length; i++) {
        var min = i;
        for (var j = i + 1; j < arr.length; j++) {
            if (arr[j] < arr[min]) {
                min = j;
            }
        }
        if (min != i) {
            var temp = arr[min];
            arr[min] = arr[i];
            arr[i] = temp;
        }
    }
    return arr;
}

var arr = [3, 8, 9, 1, 2, 6, 4, 7, 5];
selection_sort(arr);

Insertion Sort

function insertion_sort(arr) {
    for (var i = 0; i < arr.length; i++) {
        var j = i - 1,
            temp = arr[i];
        while (j >= 0 && arr[j] > temp) {
            arr[j + 1] = arr[j];
            arr[j] = temp;
            j--;
        }
    }
    return arr;
}

var arr = [3, 8, 9, 1, 2, 6, 4, 7, 5];
insertion_sort(arr);

Demo

Yang Zhao

Read more posts by this author.


Comment