mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-11-16 19:50:00 +01:00
Create Selection_Sort.rb
Selection_Sort in Ruby
This commit is contained in:
parent
3a420212b5
commit
40c21942ef
1 changed files with 18 additions and 0 deletions
18
Selection_Sort.rb
Normal file
18
Selection_Sort.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
def selection_sort(array)
|
||||
n = array.length - 1
|
||||
i = 0
|
||||
while i <= n - 1
|
||||
smallest = i
|
||||
j = i + 1
|
||||
while j <= n
|
||||
smallest = j if array[j] < array[smallest]
|
||||
j += 1
|
||||
end
|
||||
array[i], array[smallest] = array[smallest], array[i] if i != smallest
|
||||
i += 1
|
||||
end
|
||||
end
|
||||
|
||||
arr = ([9,8,3,1,2,55,68,48].shuffle) #We have taken a rondom example and also shuffling it
|
||||
selection_sort(arr)
|
||||
puts "Sorted array is: #{arr.inspect}"
|
Loading…
Reference in a new issue