hackerrank/interview_preparation_kit_sorting_bubble_sort.rb
# Complete the countSwaps function below.
Complete the countSwaps function below.
Complete the countSwaps function below .
def countSwaps(a)
  valid_a = a.sort
  swap_cnt = 0
  until a == valid_a
    a.reverse_each.with_index do |elem, i|
      i = a.size - i - 1
      if elem > valid_a[i] && elem > a[i+1]
        a = a - [elem]
        a.insert(i+1, elem)
        swap_cnt += 1
      end
    end
  end
  puts "Array is sorted in #{swap_cnt} swaps."
  puts "First Element: #{a[0]}"
  puts "Last Element: #{a[-1]}"
end
def countSwaps ( a ) valid_a = a . sort swap_cnt = 0 until a == valid_a a . reverse_each . with_index do | elem , i | i = a . size - i - 1 if elem > valid_a [ i ] && elem > a [ i + 1 ] a = a - [ elem ] a . insert ( i + 1 , elem ) swap_cnt += 1 end end end puts "Array is sorted in #{swap_cnt} swaps." puts "First Element: #{a[0]}" puts "Last Element: #{a[-1]}" end
Complete the countSwaps function below.
Complete the countSwaps function below .
a
<not_specific>