Selection Sort

Sort an array using the selection sort algorithm by repeatedly finding the minimum element.

Code

Algorithms
a = arr.dup
(0...a.length).each do |i|
  min_idx = i
  ((i + 1)...a.length).each do |j|
    min_idx = j if a[j] < a[min_idx]
  end
  a[i], a[min_idx] = a[min_idx], a[i]
end
a

Parameters

Array to sort

Server

More Ruby Snippets