Skip to main content

Posts

Showing posts with the label Hackerrank solve
  following version of Bubble Sort: for ( int i = 0 ; i < n ; i ++) { for ( int j = 0 ; j < n - 1 ; j ++) { // Swap adjacent elements if they are in decreasing order if ( a [ j ] > a [ j + 1 ]) { swap ( a [ j ], a [ j + 1 ]); } } } Given an array of integers, sort the array in ascending order using the  Bubble Sort  algorithm above. Once sorted, print the following three lines: Array is sorted in numSwaps swaps. , where   is the number of swaps that took place. First Element: firstElement , where   is the  first  element in the sorted array. Last Element: lastElement , where   is the  last  element in the sorted array. Hint:  To complete this challenge, you must add a variable that keeps a running tally of  all  swaps that occur during execution. Example swap a 0 [6,4,1] 1 [4,6,1] 2 ...