Last active
April 29, 2017 16:20
-
-
Save niquepa/258ea0da78e76ef51da8d03148a323a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/ruby | |
n = gets.strip.to_i | |
a = gets.strip | |
a = a.split(' ').map(&:to_i) | |
#puts a | |
endPos = a.count-1 | |
swapCont = 0 | |
#puts "ENDPOS=#{endPos}" | |
while endPos > 0 | |
swapPos = 0 | |
for i in 0..endPos.to_i-1 do | |
#puts "i=#{i}" | |
if a[i] > a[i.to_i+1] | |
tmp = a[i] | |
a[i] = a[i.to_i+1] | |
a[i.to_i+1] = tmp | |
swapPos = i | |
swapCont += 1 | |
end | |
end | |
endPos = swapPos | |
end | |
puts "Array is sorted in #{swapCont} swaps." | |
puts "First Element: #{a[0]}" | |
puts "Last Element: #{a[a.count-1]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment