2018-06-22

2018-06-22  本文已影响0人  ZombieBrandg
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())
上一篇下一篇

猜你喜欢

热点阅读