Class: Sorting::SelectionSort

Inherits:
Object
  • Object
show all
Extended by:
Helper
Defined in:
lib/sorting/selection_sort.rb

Constant Summary collapse

TEST_DATA_SIZE =
1000

Class Method Summary collapse

Methods included from Helper

sort

Class Method Details

.sort!(data) ⇒ Object

Selection sort Comparison sort Selection Unstable Time complexity: Ω(n2), Ө(n2), O(n2) Space complexity: O(n) total, O(1) auxiliary Suitable for small arrays (10-20) or write heavy situation Family: Heap sort, Smooth sort



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sorting/selection_sort.rb', line 15

def self.sort!(data)
  max_i = data.size - 1
  (0...max_i).each do |i|
    min_index = i
    ((i+1)..max_i).each do |j|
      min_index = j if data[j] < data[min_index]
    end
    data[i], data[min_index] = data[min_index], data[i]
  end
  nil
end