Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
Selection sort in Ruby (#5766)
Browse files Browse the repository at this point in the history
Co-authored-by: Riyazul555 <riyazulislam2003@gmail.com>
  • Loading branch information
Riyazul555 and MdRiyazulIslam authored Jun 28, 2024
1 parent 1fb7d4d commit 800baef
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions program/program/implement-selection-sort/ImplementSelectionSort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
def selection_sort(list)
n = list.length

for i in 0...(n - 1)
# Set current element as minimum
min_index = i

# Check the element to be minimum
for j in (i + 1)...n
if list[j] < list[min_index]
min_index = j
end
end

# Swap the minimum element with the current element
if min_index != i
list[i], list[min_index] = list[min_index], list[i]
end
end

list
end

# Example usage:
unsorted_list = [64, 25, 12, 22, 11]
sorted_list = selection_sort(unsorted_list)
puts "Sorted list: #{sorted_list}"

0 comments on commit 800baef

Please sign in to comment.