Monday, 29 April 2019

Array Sorting Algorithm in Ruby

SORTING ALGORITHM
def sort_list(array)
  n = array.length - 1

  n.times.each do |l|
    min_index = l
    for j in (l+1)..n
      min_index = j if array[j] < array[min_index]
    end

    array[l], array[min_index] = array[min_index], array[l] if min_index != l
  end
array
end

sort_list([65,565656,43,3,2,1,0,9,6]) # to call the method

No comments:

Post a Comment

Interactor in Rails

What is interactor? Interactor provides a common interface for performing complex user interactions An interactor is a simple, sin...