2018-06-22

var data = [5, 1, 9, 10, 3, 4, 6, 1]

function _sort(data) {
    function bubbleSort() {
        let x
        for (let i = 0; i < data.length; i++) {
            // console.log(i,'i')
            for (x = 0; x < data.length - 1; x++) {
                // console.log(x,'x')
                // console.log(data[x],data[x+1])
                if (data[x] <= data[x + 1]) {

                } else {
                    swap(data, x, x + 1)
                }
            }

        }
        return data
    }

    function quickSort() {
        let x
        let min
        for (let i = 0; i < data.length; i++) {
            min = i
            for (x = i + 1; x < data.length; x++) {
                if (data[x] < data[min]) {
                    min = x
                }
                swap(data, i, min)
            }
        }
        return data
    }

    function swap(data, a, b) {
        var newdata = data[a]
        data[a] = data[b]
        data[b] = newdata
    }

    return {
        bubbleSort: bubbleSort,
        quickSort: quickSort
    }
}
console.log(_sort(data).bubbleSort())
console.log(_sort(data).quickSort())

你可能感兴趣的:(2018-06-22)