Class: CompareSort

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

Class Method Summary collapse

Class Method Details

.compare_all(info) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/compare-sort.rb', line 26

def self.compare_all(info)
	data = info[:data]
	view = info[:view]

	sorting_methods = %w(HeapSort QuickSort SelectionSort BubbleSort ModifiedBubbleSort InsertionSort MergeSort)
	sorting_times = {}
	info_hash = { data: data.dup, timer: true }

	sorting_methods.each do |method|
		info_hash = { data: data.dup, sorting_method: method, timer: true }
		sorting_times[method] = self.run(info_hash)
	end 

	if view
		View.compare_all(sorting_times.sort_by{|method, time| time})
	end

	return sorting_times

end

.run(info) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/compare-sort.rb', line 2

def self.run(info)
	data = info[:data]
	sorting_method = info[:sorting_method]
	timer = info[:timer]

	ValidateData.run(data)

	if timer 
		sort = lambda { eval(sorting_method).run(data) }
		return self.timer(sort)
	else 
		return eval(sorting_method).run(data)
	end

end

.timer(sorting_method) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/compare-sort.rb', line 18

def self.timer(sorting_method)
	start_time = Time.now
	sorted_list = sorting_method.call 
	end_time = Time.now

	return end_time - start_time
end