• + 0 comments

    In JavaScript

    function main() {
      
    	...
    	
      // Write your code here
    
      let numberOfSwaps = 0
    
      for (let i = 0; i < n; i++) {
    	
        // Track number of elements swapped during a single array traversal
        for (let j = 0; j < n - 1; j++) {
    		
          // Swap adjacent elements if they are in decreasing order
          if (a[j] > a[j + 1]) {
            ;[a[j], a[j + 1]] = [a[j + 1], a[j]]
            numberOfSwaps++
          }
        }
    
        // If no elements were swapped during a traversal, array is sorted
        if (numberOfSwaps == 0) {
          break
        }
      }
    
      console.log(`Array is sorted in ${numberOfSwaps} swaps.`)
      console.log(`First Element: ${a[0]}`)
      console.log(`Last Element: ${a[a.length - 1]}`)
    }