Sorting algorithms/Selection sort: Difference between revisions

Content added Content deleted
(→‎{{header|Ruby}}: use idiomatic Ruby)
m (→‎{{header|Ruby}}: correct a couple of typos)
Line 2,684: Line 2,684:


puts "sequential_sort([9, 6, 8, 7, 5]): #{sequential_sort([9, 6, 8, 7, 5])}"
puts "sequential_sort([9, 6, 8, 7, 5]): #{sequential_sort([9, 6, 8, 7, 5])}"
# prints: sequential_sort([9, 6, 8, 7, 5]): [5, 6, 7, 8, 9]




# more efficient version - swaps the array's elements in place
# more efficient version - swaps the array's elements in place

def sequential_sort_with_swapping(array)
def sequential_sort_with_swapping(array)
array.each_with_index do |element, index|
array.each_with_index do |element, index|
Line 2,705: Line 2,709:
end
end


puts "sequential_sort_with_swapping([9, 6, 8, 7, 5]): #{sequential_sort_with_swapping([9, 6, 8, 7, 5])}"
puts "sequential_sort_with_swapping([7,6,5,9,8,4,3,1,2,0]): #{sequential_sort_with_swapping([7,6,5,9,8,4,3,1,2,0])}"
# prints: sequential_sort_with_swapping([7,6,5,9,8,4,3,1,2,0]): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

ary = [7,6,5,9,8,4,3,1,2,0]
p ary.selectionsort!
# => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]</lang>


=={{header|Run BASIC}}==
=={{header|Run BASIC}}==