JS实现冒泡排序

var bubble = (list) => {
    var a,
        b,
        flag,
        isNumber = true;
    if (list.length > 0) {
        do {
            flag = false;
            for (let i = 0; i < list.length - 1; i++) {
                a = Number(list[i]);
                b = Number(list[i + 1]);
                if (a && b) {
                    if (a > b) {
                        [list[i], list[i + 1]] = [list[i + 1], list[i]];
                        flag = true;
                    }
                } else {
                    isNumber = false;
                    break;
                }
            }
        } while (flag && isNumber);

        if (!isNumber) {
            return '这个不是纯数字数组!';
        } else {
            return list;
        }
    }
}

你可能感兴趣的:(JS实现冒泡排序)