Class: QuickSort

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

Class Method Summary collapse

Class Method Details

.run(data) ⇒ Object



227
228
229
230
231
# File 'lib/compare-sort.rb', line 227

def self.run(data)
	return data if data.length <= 1

	return self.sort_section(data, 0, data.length-1)
end

.sort_section(data, start_loc, end_loc) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/compare-sort.rb', line 233

def self.sort_section(data, start_loc, end_loc)
	return data if end_loc - start_loc <1
	wall = start_loc
	pivot = data[end_loc]


	for i in start_loc..end_loc #valid indicies
		if data[i]<pivot
			smaller_datum = data[i]
			data.delete_at(i)
			data.insert(wall, smaller_datum)
			wall += 1
		end 
	end 
		data.insert(wall, pivot)
		data.delete_at(end_loc + 1)

	self.sort_section(data, start_loc, wall-1)
	self.sort_section(data, wall+1, end_loc)
end