Class: SelectionSort
- Inherits:
-
Object
- Object
- SelectionSort
- Defined in:
- lib/compare-sort.rb
Class Method Summary collapse
Class Method Details
.run(data) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/compare-sort.rb', line 125 def self.run(data) len = data.length # iterate through each element in the array for i in 0..len-2 min_index = i # iterate through the rest of the array for j in i+1..len-1 # if min, save index min_index = j if data[j] < data[min_index] end # put the min in it's correct spot data[i], data[min_index] = data[min_index], data[i] end return data end |