Search Minimum

tags: pairwise-comparison linear-scan

Search the minimum in an integer sequence.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def min(a)
  n = a.size
  min_index = 0
  
  for j in (1..n-1)
    if a[min_index] > a[j]
      min_index = j
    end
  end
  
  return a[min_index]
end

p min([3,1,9,2,5,7])

The selection sort algorithm sorts an array by repeatedly finding the minimum element from unsorted part and moving it to the front.