Js used to prove safety brush offer (to adjust the order of the array so that in front of odd-even)

Title Description

Enter an array of integers, to realize a function to adjust the order of the numbers in the array, such that all the odd part of the front half of the array, is located in the second half of all the even array, and between odd and ensure a relatively even, odd and even the same position.

Cattle off network link

js code

// 空间复杂度1 时间复杂度n*n
function reOrderArray(array)
{
    // write code here
    let temp
    for (let i = 0; i < array.length; i++){
        for (let j = array.length - 1; j > i; j--){
            if (array[j] % 2 === 1 && array[j-1] % 2 === 0){
                temp = array[j]
                array[j] = array[j-1]
                array[j-1] = temp
            }
        }
    }
    return array
}
// 空间复杂度n,时间复杂度n
function reOrderArray(array)
{
    // write code here
    const res = []
    for (let i of array){
        if (i % 2 === 1) res.push(i)
    }
    for (let i of array){
        if (i % 2 === 0) res.push(i)
    }
    return res
    
}

Guess you like

Origin www.cnblogs.com/dpnlp/p/yongjs-shua-jian-zhioffor-diao-zheng-shu-zu-shun-x.html