-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertion_sort.rb
50 lines (42 loc) · 1.2 KB
/
insertion_sort.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
# Insertion sort work really well for almost sorted list
# Finds next element that needs to be sorted
# and puts it where it belongs among already sorted elements.
# O(n^2)
# destructive
def insertion_sort!(array)
array.each_with_index do |el, i|
j = i
j -= 1 while j > 0 && el < array[j-1]
array[i] = array[i-1] and i -= 1 while i > j
array[j] = el
end
array
end
# non-destructive
def insertion_sort(array)
clone = array.clone
clone.each_with_index do |el, i|
j = i
j -= 1 while j > 0 && el < clone[j-1]
clone[i] = clone[i-1] and i -= 1 while i > j
clone[j] = el
end
clone
end
# tests
array1 = [1,2,3,4]
puts "Array #{array1} should stay sorted."
puts insertion_sort(array1) == array1
puts "---"
array2 = [4,3,2,1]
puts "Array #{array2} should be sorted."
puts insertion_sort(array2) == [1,2,3,4]
puts "---"
array3 = [2352345, 54453532, 45646546, 767889867, 896754, 433, 2342342, 333]
puts "Array #{array3} should be sorted."
puts insertion_sort(array3) == [333, 433, 896754, 2342342, 2352345, 45646546, 54453532, 767889867]
puts "---"
array4 = []
1000.times { array4 << rand(1000) }
puts "Array4 #{array4} should be sorted."
puts insertion_sort(array4) == array4.sort