Skip to content

Instantly share code, notes, and snippets.

@DanielAmah
Last active May 27, 2019 12:34
Show Gist options
  • Select an option

  • Save DanielAmah/f8b2156c8997a222bf055510d568fe60 to your computer and use it in GitHub Desktop.

Select an option

Save DanielAmah/f8b2156c8997a222bf055510d568fe60 to your computer and use it in GitHub Desktop.
def selection_sort(arr)
n = arr.length - 1
n.times do |i|
min_index = i
((i + 1)..n).each do |j|
min_index = j if arr[j] < arr[min_index]
end
arr[i], arr[min_index] = arr[min_index], arr[i] if min_index != i
end
arr
end
arr = [2, 5, 6, 2, 5425, 54, 5, 4, 12, 7, 3, 5, 5]
print selection_sort(arr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment