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]}`)
}