Class: InsertionSort

Inherits:
Object
  • Object
show all
Defined in:
lib/compare-sort.rb

Class Method Summary collapse

Class Method Details

.run(data) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/compare-sort.rb', line 103

def self.run(data)

	# iterate through each element
	data.each_with_index do |unsorted_num, i|
		data[0..i].each_with_index do |sorted_num, j|

			if sorted_num > unsorted_num
				# insert to its new spot
				data.insert(j, unsorted_num)
				# delete it from old spot
				data.delete_at(i+1)
				break
			end

		end
	end		
	return data
end