• 数据结构--排序_数据结构--排序

学习资源推荐

function ArrayList() {
    let arr = [];
    this.insert = function (item) {
        arr.push(item);
    };
    this.toString = function () {
        return arr.join();
    };
    //冒泡排序
    this.bubbleSort = function () {
        for (let i = 0; i < arr.length; i++) {
            for (let j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]
                }
            }
        }
        return arr;

    };
    //选择排序

    this.selectionSort = function () {
        let min;
        for (let i = 0; i < arr.length - 1; i++) {
            min = i;
            for (let j = i; j < arr.length; j++) {
                if (arr[min] > arr[j]) {
                    min = j;
                }
            }
            if (i !== min) {
                [arr[min], arr[i]] = [arr[i], arr[min]]
            }
        }
        return arr;


    };
    //插入排序
    this.insertionSort = function () {

        let j;
        let temp;
        for (let i = 1; i < arr.length; i++) {
            j = i;
            temp = arr[i];
            while (j > 0 && arr[j - 1] > temp) {
                arr[j] = arr[j - 1];
                j--;
            }
            arr[j] = temp;
        }

        return arr
    }

    // 二分查找
    this.binarySearch = function (item) {
        this.insertionSort();
        var low = 0,
            high = arr.length - 1,
            mid, element;
        while (low <= high) {
            mid = Math.floor((low + high) / 2);
            element = arr[mid];
            if (element < item) {
                low = mid + 1;
            } else if (element > item) {
                high = mid - 1;
            } else {
                return mid;
            }
        }
        return -1;
    }

    //归并排序
    this.mergeSort = function () {
        return _mergeSortRec(arr);
    };
    function _mergeSortRec(arr) {
        if (arr.length === 1) {
            return arr;
        }
        let mid = Math.floor(arr.length / 2);
        let left = arr.slice(0, mid);
        let right = arr.slice(mid, arr.length);
        return _merge(_mergeSortRec(left), _mergeSortRec(right));
    };

    function _merge(left, right) {
        let result = [], il = 0, 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;
    };


}

let arrayList = new ArrayList();

arrayList.insert(1)
arrayList.insert(2)
arrayList.insert(3)
arrayList.insert(8)
arrayList.insert(7)
arrayList.insert(4)

//console.log(arrayList.toString())
//console.log(arrayList.bubbleSort())
//console.log(arrayList.selectionSort())
//console.log(arrayList.insertionSort())
//console.log(arrayList.mergeSort())
//console.log(arrayList.binarySearch(2))