📌  相关文章
📜  第 20 天在 javascript 代码示例中排序hackerrank 解决方案

📅  最后修改于: 2022-03-11 15:02:49.682000             🧑  作者: Mango

代码示例1
// day 20 sorting hackerrank solution in javascript
function main() {
    const n = parseInt(readLine().trim(), 10);

    const a = readLine().replace(/\s+$/g, '').split(' ').map(aTemp => parseInt(aTemp, 10));

    // Write your code here
    let swap =[];
    let numberOfSwap = 0;
    for(let i=0; i a[j+1]){
                [a[j + 1], a[j]] = [a[j], a[j + 1]];
                numberOfSwap++;
            }
        }
        if(numberOfSwap === 0){
            break;
        }
    }
    console.log(`Array is sorted in ${numberOfSwap} swaps.\nFirst Element: ${a[0]}\nLast Element: ${a[a.length - 1]}`)
}