Answers for "day 20 sorting hackerrank solution in javascript"

0

day 20 sorting hackerrank solution in javascript

// 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<n; i++){
        for(let j=0; j<n-1; j++){
            if(a[j]> 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]}`)
}
Posted by: Guest on January-03-2022

Code answers related to "day 20 sorting hackerrank solution in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language