Sorting Algorithms in JS:js

Bubble sort

Algorithmic complexity: O (n^2)

var array = [5,9,4,1,10];
this.getArray = function() {
    return array;
}
var swap = function(i, j) {
    temp = array[i];
    array[i] = array[j];
    array[j] = temp;
};
this.bubbleSort = function() {
    for(var i = 1; i < array.length; i++) {
        for (var j = 0; j < i; j++) {
            if(array[i] < array[j]){
                swap(i, j);
            }
        }
    }
};

Bubble sorting is to traverse the entire array, traverse each element before it, and swap if there is a larger one.

Selection sort

Algorithmic complexity: O (n^2)

this.selectionSort = function() {
    minIndex = 0;
    for(var i = 0; i< array.length; i++) {
        minIndex = i;
        for(var j = i+1; j < array.length; j++) {
            if(array[minIndex] > array[j]) {
                minIndex = j;
            }
        }
        if(minIndex != i) {
            this.swap(i, minIndex);
        }
    }
};

The idea of sorting is to traverse the array once, record the lowest subscript for each element, and then traverse the elements after that element. If there are smaller elements than them, the lowest subscript will be changed to its lowest subscript. Exchange after traversal.

Insertion sort

Algorithmic complexity: O (n^2)

this.insertSort = function() {
    for(var i = 1; i < array.length; i++) {
        temp = array[i];
        j = i;
        while(j>0 && array[j-1]>temp) {
            array[j] = array[j-1];
            j--;
        }
        array[j] = temp;
    }
};

Let's assume that the first one of the arrays is already arranged. Starting from the second part of the array, record its value and compare the following elements one by one. If there is a larger one, give the position to the larger one and continue to compare until a smaller one is over.

Merge sort

Algorithmic complexity: O (nlogn)

this.mergeSort = function() {
    array = mergeSortRecursive(array); 
};
//Recursive execution until split into arrays of length 1
var mergeSortRecursive = function(array) {
    if(array.length == 1) {
        return array;
    }
    var middle = Math.round(array.length / 2);
    var arrayLeft = array.slice(0, middle);
    var arrayRight = array.slice(middle, array.length);

    return merge(mergeSortRecursive(arrayLeft), mergeSortRecursive(arrayRight));
};
//Merge two arrays
var merge = function(arrayLeft, arrayRight) {
    var result = [];
    console.log("left",arrayLeft);
    console.log("right",arrayRight);
    var leftLen = 0;
    var rightLen = 0;
    //Compare iteratively until one of the arrays is all put into result
    while(leftLen < arrayLeft.length && rightLen < arrayRight.length){
        if(arrayLeft[leftLen] < arrayRight[rightLen]) {
            result.push(arrayLeft[leftLen]);
            leftLen++;
        } else {
            result.push(arrayRight[rightLen]);
            rightLen++;
        }
    }
    //The remaining arrays are placed directly because they are ordered.
    while(leftLen < arrayLeft.length) {
        result.push(arrayLeft[leftLen]);
        leftLen++;
    }
    while(rightLen < arrayRight.length) {
        result.push(arrayRight[rightLen]);
        rightLen++;
    }
    return result;
};

The principle of merge sorting is to divide an array continuously, and then merge until it can't be divided (an array of length 1).

Quick sort

Algorithmic complexity: O (nlogn)

this.quickSort = function() {
    quick(array, 0, array.length-1);
};
var quick = function (array, left, right) {
    if(array.length > 1) {
        index = partition(array, left, right);
        if(left < index - 1) {
            quick(array, left, index-1);
        }
        if(right > index) {
            quick(array, index, right);
        }
    }
    return array;
};
var partition = function(array, left, right) {
    var i = left;
    var j = right;
    var pivot = array[Math.floor((left+right)/2)];
    while(i <= j) {
        while(array[i] < pivot) {
            i++;
        }
        while(array[j] > pivot) {
            j--;
        }
        if(i <= j) {
            swap(i, j);
            i++;
            j--;
        }
    }
    console.log(array, i, j);
    return i;
};

Quick sorting principle and merging almost always divide arrays, but fast sorting starts from big and merges from small.
Firstly, find an intermediate value, the element with subscript i represents the left element, the element with subscript j represents the right element, I constantly finds subscripts larger than the intermediate value, and j constantly finds subscripts smaller than the intermediate value, once I <= J exchanges.
In this way, we can ensure that the elements before and after the i subscript are larger than the median value, and the elements before the i subscript are smaller than the median value. So until there's only one element, it's sorted out.

Posted by alluoshi on Thu, 18 Apr 2019 02:42:32 -0700