Array sorting question for ruby to swap two elements

You could write the following:

arr = [5, 22, 29, 39, 19, 51, 78, 96, 84]
i = (0..arr.size-2).find { |i| arr[i] > arr[i+1] }
  #=> 3
arr[i], arr[i+1] = arr[i+1], arr[i] if i
  #=> [19, 39]
arr
  #=> [5, 22, 29, 19, 39, 51, 78, 96, 84]