1. 程式人生 > >資料結構之排序演算法

資料結構之排序演算法

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);