数据结构之排序算法

JavaScript实现排序算法

代码实现

const ArrayList = function() {
    let array = [],
        length = 0;

    this.insert = function(item) {
        array.push(item);
        length++;
    };

    this.toString = function() {
        return array.join();
    };

    const swap = function(array, key1, key2) {
        let temp = array[key1];
        array[key1] = array[key2];
        array[key2] = temp;
    }

    this.bubbleSort = function() {
        // 冒泡排序
        for (let i = 0; i < length; i++) {
            for (let j = 0; j < length-1; j++) {
                if(array[j] > array[j+1]) {
                    swap(array, j, j+1);
                }
            }
        }
    };

    this.bubbleSort2 = function() {
        // 冒泡算法改进,
        for (let i = 0; i < length; i++) {
            for (let j = 0; j < length-1-i; j++) { // 在内部循环时候减去第一次循环拍好顺序的尾部元素
                if(array[j] > array[j+1]) {
                    swap(array, j, j+1);
                }
            }
        }
    };

    this.insertionSort = function() {
        // 插入排序
        let j, temp;
        for (let i = 1; i < length; i++) {
            j = i;
            temp = array[i]
            while (j > 0 && temp < array[j-1]) {
                // 在已经排好序的数组中,寻找大于当前值(temp)的元素并向后挪一位。
                array[j] = array[j-1];
                j--;
            }
            array[j] = temp;
        }
    };

    this.selectionSort = function() {
        // 选择排序
        let indexMin;
        for (let i = 0; i < length; i++) {
            indexMin = i;
            for (let j = i; j < length; j++) {
                if(array[indexMin] > array[j]) {
                    indexMin = j;
                }
            }
            if (i !== indexMin) {
                swap(array, i, indexMin);
            }
        }
    };

   
    const merge = function(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;
    }

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

        return merge(mergeSortRec(left), mergeSortRec(right));
    };

    this.mergeSort = function() {
        // 归并排序
        array = mergeSortRec(array);
    };

    const quick = function(array, left, right) {
        let index;
        if(length > 1) {
            index = partition(array, left, right);
            if (left < index - 1) {
                quick(array, left, index-1);
            }
            if (index < right) {
                quick(array, index, right);
            }
        }
    }

    const partition = function(array, left, right) {
        let pivot = array[Math.floor((right + left) / 2)],
            i = left,
            j = right;
        while (i <= j) {
            while (array[i] < pivot) {
                i++;
            }
            while (array[j] > pivot) {
                j--;
            }
            if (i <= j) {
                swap(array, i, j);
                i++;
                j--;
            }
            return i;
        }
    }

    this.quickSort = function() {
        // 快速排序
        quick(array, 0, length - 1);
    };
}



function createNonSortedArray(size) {
    let array = new ArrayList();

    for (let i = size; i > 0; i--) {
        array.insert(i);
    };
    return array;
}


// 测试代码
// let array = createNonSortedArray(8);
// console.log(array.toString());
// array.bubbleSort2();
// console.log("bubbleSort:  " + array);

// let array2 = createNonSortedArray(8);
// console.log(array2.toString());
// array2.insertionSort();
// console.log("insertionSort:  " + array2);


// let array3 = createNonSortedArray(8);
// console.log(array3.toString());
// array3.selectionSort();
// console.log("selectionSort:  " + array3);


let array4 = createNonSortedArray(9);
console.log(array4.toString());
array4.mergeSort();
console.log("mergeSort:  " + array4);


// let array5 = createNonSortedArray(9);
// console.log(array5.toString());
// array5.quickSort();
// console.log("quickSort:  " + array5);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值