-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertion_sort_archive2.rb
86 lines (78 loc) · 1.8 KB
/
insertion_sort_archive2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
class InsertionSort
attr_reader :unsorted, :sorted
def initialize(unsorted,sorted=[])
@unsorted = unsorted
@sorted = sorted
@unsorted_length = unsorted.length
end
def unsorted
@unsorted
end
def sorted
@sorted
end
def unsorted_length
@unsorted_length
end
def kick_it_off
sorted.insert(0,unsorted[0])
end
def sort
if unsorted.length == 0
unsorted
elsif unsorted.length == 1
sorted_active = sorted.insert(0,unsorted[0]) #why does this break if I change variable name from sorted_active to sorted
sorted_active
else
kick_it_off
unsorted.shift
if (sorted[0] <=> unsorted[0]) == -1
sorted.insert(1,unsorted[0])
else
sorted.insert(0,unsorted[0])
end
@sorted
end
end
def sort23
kick_it_off
unsorted.shift
if (sorted[0] <=> unsorted[0]) == 1
sorted.insert(0,unsorted[0])
unsorted.shift
else
sorted.insert(1,unsorted[0])
unsorted.shift
end
if unsorted_length >=3
if (sorted[0] <=> unsorted[0]) == 1
sorted.insert(0,unsorted[0])
unsorted.shift
else
if (sorted[1] <=> unsorted[0]) == 1
sorted.insert(1,unsorted[0])
unsorted.shift
else
sorted.insert(2,unsorted[0])
unsorted.shift
end
end
if unsorted_length >= 4
if (sorted[0] <=> unsorted[0]) == 1
sorted.insert(0,unsorted[0])
unsorted.shift
elsif (sorted[1] <=> unsorted[0]) == 1
sorted.insert(1,unsorted[0])
unsorted.shift
elsif
(sorted[2] <=> unsorted[0]) == 1
sorted.insert(2,unsorted[0])
unsorted.shift
else
sorted.insert(3,unsorted[0])
end
end
end
@sorted
end
end