Bubble sort, Insertion sort, Selection sort, Merge sort, Quick sort, - Node.js Algorithm

Node.js examples for Algorithm:Sort

Description

Bubble sort, Insertion sort, Selection sort, Merge sort, Quick sort,

Demo Code

Array.prototype.bubbleSort = function() {
    var keepGoing = true;
    var temp = null;
    while (keepGoing) {
        keepGoing = false;/*from w  w w  . j a va2  s .  c  om*/
        for (var i = 0; i < this.length - 1; i++) {
            if (this[i] > this[i+1]) {
                keepGoing = true;
                temp = this[i];
                this[i] = this[i+1];
                this[i+1] = temp;
            }
        }
    }
};

Array.prototype.insertionSort = function() {
    var position;
    var currentValue;
    for (var i = 1; i < this.length; i++) {
        position = i;
        currentValue = this[i];
        while (position > 0 && this[position-1] > currentValue) {
            this[position] = this[position-1];
            position -= 1;
        }
        this[position] = currentValue;
    }
};

Array.prototype.selectionSort = function() {
    var minimumIndex;
    var temp;
    for (var i = 0; i < this.length; i++) {
        minimumIndex = i;
        for (var j = i; j < this.length; j++) {
            if (this[j] < this[minimumIndex]) {
                minimumIndex = j;
            }
        }
        // Swap the values
        temp = this[i];
        this[i] = this[minimumIndex];
        this[minimumIndex] = temp;
    }
};

Array.prototype.mergeSort = function() {
    function merge(a1, a2) {
        var a1Pointer = 0;
        var a2Pointer = 0;
        var mergedArray = [];
        while(a1Pointer < a1.length || a2Pointer < a2.length) {
            if (a1Pointer < a1.length && a2Pointer < a2.length) {
                if (a1[a1Pointer] <= a2[a2Pointer]) {
                    mergedArray.push(a1[a1Pointer]);
                    a1Pointer += 1;
                }
                else {
                    mergedArray.push(a2[a2Pointer]);
                    a2Pointer += 1;
                }
            }
            else if (a1Pointer < a1.length) {
                Array.prototype.push.apply(mergedArray, a1.slice(a1Pointer));
                break;
            }
            else {
                Array.prototype.push.apply(mergedArray, a2.slice(a2Pointer));
                break;
            }
        }
        return mergedArray;
    }
    if (this.length > 1) {
        var sublist1 = this.slice(0, this.length/2);
        var sublist2 = this.slice(this.length/2);
        sublist1.mergeSort();
        sublist2.mergeSort();
        var mergedList = merge(sublist1, sublist2);
        this.length = 0;
        Array.prototype.push.apply(this, mergedList);
    }
};

function quickSort(arr) {
    if (arr.length < 2) {
        return arr;
    }
    var pivotIndex = Math.floor(arr.length/2);
    var lessThan = arr.filter(function(elem, index, a) {
        return elem < arr[pivotIndex] && index != pivotIndex;
    });
    var greaterThan = arr.filter(function(elem, index, a) {
        return elem >= arr[pivotIndex] && index != pivotIndex;
    });
    return quickSort(lessThan).concat([arr[pivotIndex]], quickSort(greaterThan));
}

Array.prototype.quickSort = function() {
    if (this.length < 2) {
        return;
    }
    var pivotIndex = Math.floor(this.length/2);
    var lessThan = this.filter(function(elem, index, a) {
        return elem < this[pivotIndex] && index != pivotIndex;
    }, this);
    var greaterThan = this.filter(function(elem, index, a) {
        return elem >= this[pivotIndex] && index != pivotIndex;
    }, this);
    lessThan.quickSort();
    greaterThan.quickSort();
    var sorted = lessThan.concat([this[pivotIndex]], greaterThan);
    this.length = 0;
    Array.prototype.push.apply(this, sorted);
};

Array.prototype.quickSort2 = function() {
    if (this.length < 2) {
        return;
    }
    var pivotIndex = Math.floor(this.length/2);
    var lessThan = this.filter(function(elem, index) {
        return elem < this[pivotIndex] && index != pivotIndex;
    }, this);
    var greaterThan = this.filter(function(elem, index) {
        return elem >= this[pivotIndex] && index != pivotIndex;
    }, this);
    lessThan.quickSort();
    greaterThan.quickSort();
    var sorted = lessThan.concat([this[pivotIndex]], greaterThan);
    this.length = 0;
    Array.prototype.push.apply(this, sorted);
};

Related Tutorials