首页 > 代码库 > js家的排序算法

js家的排序算法

function ArrayList() {
    this.array = [];
}
ArrayList.prototype = {
    constructor: ArrayList,
    insert: function(item) {
        this.array.push(item);
    },
    toString: function() {
        return this.array.join();
    },
    swap: function(index1, index2) {
        var aux = this.array[index2];
        this.array[index2] = this.array[index1];
        this.array[index1] = aux;
    },

    //冒泡排序,冒泡排序比较任何两个相邻的项,如果第一个比第二个大,
    // 则交换它们。元素向上移动至正确的顺序。
    bubbleSort: function() {
        var length = this.array.length;
        for (var i = 0; i < length; i++) {
            for (var j = 0; j < length - 1 - i; j++) {
                if (this.array[j] > this.array[j + 1]) {
                    this.swap(j, j + 1);
                }
            }
        }
    },
    // 选择排序,找到数据结构中的最小值并将其放在第一位,接
    // 着找到第二小的值放在第二位
    selectionSort: function() {
        var length = this.array.length;
        var indexMin;
        for (var i = 1; i < length - 1; i++) {
            indexMin = i;
            for (var j = i; j < length; j++) {
                if (this.array[indexMin] > this.array[j]) {
                    indexMin = j;
                }
            }
            if (indexMin !== i) {
                this.swap(indexMin, i);
            }
        }
    },

    //插入排序,在一个已经有序的数据序列中插入一个数

    insertionSort: function() {
        var length = this.array.length;
        var j;
        var temp;
        for (var i = 0; i < length; i++) {
            temp = this.array[i];
            j = i;
            while (j > 0 && this.array[j - 1] > temp) {
                this.array[j] = this.array[j - 1]; //往后移
                j--;
            }
            this.array[j] = temp; //移完了插进去
        }
    },

    //归并排序,将已有序的子序列合并,得到完全有序的序列

    mergeSort: function() {
        function mergeSortRec(array) {
            var length = array.length;
            if (length === 1) {
                return array;
            }
            var mid = Math.floor(length / 2);
            var left = array.slice(0, mid);
            var right = array.slice(mid, length);
            return merge(mergeSortRec(left), mergeSortRec(right));
        }

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

        this.array = mergeSortRec(this.array);
    },

    //快速排序
    // 通过一趟排序将要排序的数据分割成独立的两部分,其中一部分所有数据比另一部分小
    //然后在按此方法分别进行快速排序,递归
    quickSort: function() {
        function sort(array) {
            if (array.length <= 1) {
                return array;
            }
            var pivotIndex = Math.floor(array.length / 2);
            var pivot = array.splice(pivotIndex, 1)[0];
            var left = [];
            var right = [];
            for (var i = 0; i < array.length; i++) {
                if (array[i] < pivot) {
                    left.push(array[i]);
                } else {
                    right.push(array[i]);
                }
            }
            return sort(left).concat([pivot], sort(right));
        }

        this.array = sort(this.array);
    }
};

 

js家的排序算法