Ruby implements insertion sorting algorithm and advanced two-way insertion sorting code example

Time:2021-11-24

Basics
Insert a record into a sorted table to get an ordered table with one record added. And the most critical point is that it moves all records larger than the current element back to make room for “itself” to be inserted. When the N-1 insertion is completed, the record is an ordered sequence.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def insertSort(tarray)
  i=1
  while(i < tarray.size) do
   if tarray[i] < tarray[i-1]
     j=i-1
     x=tarray[i]
   #puts x.class
   #puts tarray[i].class
     tarray[i]=tarray[i-1]#First swap positions with the first one on the left that is larger than yourself
     while(x< tarray[j].to_i) do#Find one smaller than yourself and put it behind
      tarray[j+1]=tarray[j]
      #puts tarray[j].class
      j=j-1
     end
     tarray[j+1]=x
   end
   i=i+1
  end
 end
 
a=[5,2,6,4,7,9,8]
insertSort(a)
print a
?
1
[2, 4, 5, 6, 7, 8, 9]>Exit code: 0

When I first started writing code, I didn’t add to at x < tarray [J]_ I method, the following error occurred:

?
1
final.rb:10:in `<': comparison of Fixnum with nil failed (ArgumentError)

At first, I was very confused, so I output X.class and tarray [J]. Class, but the output of both of them is fixnum. Later, I found that Ruby’s array class is different from others. Ruby allows different types of elements to be stored in an array object. When an element of a is assigned to x, I can’t determine whether the a [i] compared with X is of fixnum type, so an error is reported, which is only my own understanding.

Advanced
2-way insertion sorting is based on half insertion sorting:

?
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
def two_way_sort data
 first,final = 0,0
 temp = []
 temp[0] = data[0]
 result = []
 len = data.length
 
 for i in 1..(len-1)
  if data[i]>=temp[final]
   final +=1
   temp[final] = data[i]
  elsif data[i]<= temp[first]
   first = (first -1 + len)%len
   temp[first] = data[i]
  else
   if data[i]<temp[0]
    low = first
    high = len -1
   
    while low <=high do
     m = (low + high)>>1
     if data[i]>temp[m]
      low = m + 1
     else
      high = m -1
     end
    end
    
    j = first - 1
    first -=1
    while j < high do
     temp[j] = temp[j+1]
     j +=1
    end
 
    temp[high] = data[i]
   else
    low =0
    high = final
 
    while low <=high do
     m =(low + high)>>1
 
     if data[i]>=temp[m]
      low = m + 1
     else
      high = m - 1
     end
    end
 
    j = final + 1
    final +=1
 
    while j > low do
     temp[j] = temp[j-1]
     j -=1
    end
 
    temp[low] = data[i]
   end
  end
  p temp
 end
 
 i = 0
 for j in first..(len - 1)
  result[i] = temp[j]
  i +=1
 end
 
 for j in 0..final
  result[i] = temp[j]
  i +=1
 end
 
 return result
end
 
 
data = [4,1,5,6,7,2,9,3,8].shuffle
 
p data
 
result = two_way_sort data
 
p result