Knaspast
Knaspast

Reputation: 11

What's the name of this sorting algorithm?

I searched around a bit and could not found any sorting algorithm that described this way to sort. I do understand that it has no real use as it's terribly inefficient. Here it is in Ruby:

def swap(array, i)
  array[i], array[i+1] = array[i+1], array[i]
end

def compare(array, i)
  array[i] > array[i+1]
end

def sort(array)
  i = 0
  until i + 1 == array.length
    if compare(array, i)
      swap(array, i)
      i = 0
    else
      i += 1
    end
  end
  return array
end

Upvotes: 1

Views: 92

Answers (1)

MBo
MBo

Reputation: 80197

This is Gnome sort kind - walking from the beginning until order violation occurs, then swapping, but here position resets to array begin.

Upvotes: 1

Related Questions