Data Sorting: Insertion Sort: Georgy Gimel'farb
Data Sorting: Insertion Sort: Georgy Gimel'farb
Georgy Gimel’farb
1 / 16
Outline Ordering Sorting Efficiency Insertion sort
1 Ordering
2 Data sorting
4 Insertion sort
2 / 16
Outline Ordering Sorting Efficiency Insertion sort
3 / 16
Outline Ordering Sorting Efficiency Insertion sort
S = {q, w, e, r, t, y, u, i, o, p, a, s, d, f, g, h, j, k, l, z, x, c, v, b, n, m}
and R – the alphabetic relation for all pairs of letters:
(a, a) (a, b) (a, c) (a, d) (a, e) (a, f ) ... (a, y) (a, z)
(b, b) (b, c) (b, d) (b, e) (b, f ) ... (b, y) (b, z)
(c, c) (c, d) (c, e) (c, f ) ... (c, y) (c, z)
R=
...
(y, y) (y, z)
(z, z)
4 / 16
Outline Ordering Sorting Efficiency Insertion sort
The characters are compared in line with their numerical or alphabetical order:
look into any dictionary or thesaurus. . .
6 / 16
Outline Ordering Sorting Efficiency Insertion sort
The key is often a data field in a larger object: rather than move such objects,
a pointer from the key to the object is to be kept.
• Sorting is stable if any two objects, having the same key in the input,
appear in the same order in the output.
• Sorting is in-place if only a fixed additional memory space is required
independently of the input size.
7 / 16
Outline Ordering Sorting Efficiency Insertion sort
Insertion sort (the same scheme also in Selection Sort and Bubble Sort)
• Split an array into a unordered and ordered parts:
Head (ordered) Tail (unordered)
a0 , a1 , . . . , ai−1 ai , ai+1 , . . . , an−1
• Sequentially contract the unordered part, one element per stage:
http://interactivepython.org/runestone/static/pythonds/SortSearch/TheInsertionSort.html
def insertionSort( a )
for i in range (1, len( a ) ) :
tmp = a[ i ] # pick a[i]
k = i
while k > 0 and tmp < a[ k - 1 ] : # compare to a[k]
a[ k ] = a[ k - 1] # shift a[k] right
k = k - 1
10 / 16
Outline Ordering Sorting Efficiency Insertion sort
0 1 2 3 4 5 6
44 13 35 18 15 10 20
i↓
12 / 16
Outline Ordering Sorting Efficiency Insertion sort
0 1 2 3 4 5 6
44 13 35 18 15 10 20
i↓
1 13
12 / 16
Outline Ordering Sorting Efficiency Insertion sort
0 1 2 3 4 5 6
44 13 35 18 15 10 20
i↓
1 13
12 / 16
Outline Ordering Sorting Efficiency Insertion sort
0 1 2 3 4 5 6
44 13
44 35 18 15 10 20
i↓
1 13
12 / 16
Outline Ordering Sorting Efficiency Insertion sort
0 1 2 3 4 5 6
44
13 13
44 35 18 15 10 20
i↓
1 13
12 / 16
Outline Ordering Sorting Efficiency Insertion sort
0 1 2 3 4 5 6
44
13 13
44 35 18 15 10 20
i↓
21 13 35
12 / 16
Outline Ordering Sorting Efficiency Insertion sort
0 1 2 3 4 5 6
44
13 13
44 35 18 15 10 20
i↓
21 13 35
12 / 16
Outline Ordering Sorting Efficiency Insertion sort
0 1 2 3 4 5 6
44
13 13
44 35
44 18 15 10 20
i↓
21 13 35
12 / 16
Outline Ordering Sorting Efficiency Insertion sort
0 1 2 3 4 5 6
44
13 13
44 35
44 18 15 10 20
i↓
21 13 35
12 / 16
Outline Ordering Sorting Efficiency Insertion sort
0 1 2 3 4 5 6
44
13 13
44
35 35
44 18 15 10 20
i↓
21 13 35
12 / 16
Outline Ordering Sorting Efficiency Insertion sort
0 1 2 3 4 5 6
44
13 13
44
35 35
44 18 15 10 20
i↓
321 13 35 18
12 / 16
Outline Ordering Sorting Efficiency Insertion sort
0 1 2 3 4 5 6
44
13 13
44
35 35
44 18 15 10 20
i↓
321 13 35 18
12 / 16
Outline Ordering Sorting Efficiency Insertion sort
0 1 2 3 4 5 6
44
13 13
44
35 35
44 18
44 15 10 20
i↓
321 13 35 18
12 / 16
Outline Ordering Sorting Efficiency Insertion sort
0 1 2 3 4 5 6
44
13 13
44
35 35
44 18
44 15 10 20
i↓
321 13 35 18
12 / 16
Outline Ordering Sorting Efficiency Insertion sort
0 1 2 3 4 5 6
44
13 13
44
35 44
35 18
44 15 10 20
i↓
321 13 35 18
12 / 16
Outline Ordering Sorting Efficiency Insertion sort
0 1 2 3 4 5 6
44
13 13
44
35 44
35 18
44 15 10 20
i↓
321 13 35 18
12 / 16
Outline Ordering Sorting Efficiency Insertion sort
0 1 2 3 4 5 6
44
13 13
44
35
18 44
35 18
44 15 10 20
i↓
i C i Mi
321 1 1 1 13 35 18
2 2 1
3 3 2
12 / 16
Outline Ordering Sorting Efficiency Insertion sort
3 13 18 35 44 15 10 20 3 2
4 15 44 < →
15 35 < →
15 18 < →
15 ≥
4 13 15 18 35 44 10 20 4 3
i Ci Mi
13 / 16
Outline Ordering Sorting Efficiency Insertion sort
3 13 18 35 44 15 10 20 3 2
4 15 44 < →
15 35 < →
15 18 < →
15 ≥
4 13 15 18 35 44 10 20 4 3
i Ci Mi
13 / 16
Outline Ordering Sorting Efficiency Insertion sort
3 13 18 35 44 15 10 20 3 2
4 15 44 < →
15 35 < →
15 18 < →
15 ≥
4 13 15 18 35 44 10 20 4 3
i Ci Mi
13 / 16
Outline Ordering Sorting Efficiency Insertion sort
3 13 18 35 44 15 10 20 3 2
4 15 44 < →
15 35 < →
15 18 < →
15 ≥
4 13 15 18 35 44 10 20 4 3
i Ci Mi
13 / 16
Outline Ordering Sorting Efficiency Insertion sort
3 13 18 35 44 15 10 20 3 2
4 15 44 < →
15 35 < →
15 18 < →
15 ≥
4 13 15 18 35 44 10 20 4 3
i Ci Mi
13 / 16
Outline Ordering Sorting Efficiency Insertion sort
4 13 15 18 35 44 10 20 4 3
5 10 44 < →
10 35 < →
10 18 < →
10 15 < →
10 13 < →
5 10 13 15 18 35 44 20 5 5
i Ci Mi
14 / 16
Outline Ordering Sorting Efficiency Insertion sort
4 13 15 18 35 44 10 20 4 3
5 10 44 < →
10 35 < →
10 18 < →
10 15 < →
10 13 < →
5 10 13 15 18 35 44 20 5 5
i Ci Mi
14 / 16
Outline Ordering Sorting Efficiency Insertion sort
4 13 15 18 35 44 10 20 4 3
5 10 44 < →
10 35 < →
10 18 < →
10 15 < →
10 13 < →
5 10 13 15 18 35 44 20 5 5
i Ci Mi
14 / 16
Outline Ordering Sorting Efficiency Insertion sort
4 13 15 18 35 44 10 20 4 3
5 10 44 < →
10 35 < →
10 18 < →
10 15 < →
10 13 < →
5 10 13 15 18 35 44 20 5 5
i Ci Mi
14 / 16
Outline Ordering Sorting Efficiency Insertion sort
4 13 15 18 35 44 10 20 4 3
5 10 44 < →
10 35 < →
10 18 < →
10 15 < →
10 13 < →
5 10 13 15 18 35 44 20 5 5
i Ci Mi
14 / 16
Outline Ordering Sorting Efficiency Insertion sort
4 13 15 18 35 44 10 20 4 3
5 10 44 < →
10 35 < →
10 18 < →
10 15 < →
10 13 < →
5 10 13 15 18 35 44 20 5 5
i Ci Mi
14 / 16
Outline Ordering Sorting Efficiency Insertion sort
5 10 13 15 18 35 44 20 5 5
5 20 44 < →
20 35 < →
20 ≥
6 10 13 15 18 20 35 44 3 2
i C i Mi
15 / 16
Outline Ordering Sorting Efficiency Insertion sort
5 10 13 15 18 35 44 20 5 5
5 20 44 < →
20 35 < →
20 ≥
6 10 13 15 18 20 35 44 3 2
i C i Mi
15 / 16
Outline Ordering Sorting Efficiency Insertion sort
5 10 13 15 18 35 44 20 5 5
5 20 44 < →
20 35 < →
20 ≥
6 10 13 15 18 20 35 44 3 2
i C i Mi
15 / 16
Outline Ordering Sorting Efficiency Insertion sort
5 10 13 15 18 35 44 20 5 5
5 20 44 < →
20 35 < →
20 ≥
6 10 13 15 18 20 35 44 3 2
i C i Mi
15 / 16
Outline Ordering Sorting Efficiency Insertion sort
Stage i 1 2 3 4 5 6 Total
Comparisons Ci 1 2 3 4 5 3 18
Moves Mi 1 1 2 3 5 2 14
• The best case – an already sorted array, e.g. {10, 13, 15, 18, 20, 35, 44}:
• 1 comparison and 0 moves per each stage i = 1, . . . , n − 1.
• In total, 0 moves and n − 1 comparisons for the already sorted array
of size n.
• The worst case – a reversely sorted array. e.g. {44, 35, 20, 18, 15, 13, 10}:
• i comparisons and i moves per each stage i = 1, . . . , n − 1.
• In total, 1 + . . . + (n − 1) = (n−1)n
2
moves and (n−1)n
2
comparisons
for the reversely sorted array of size n.
16 / 16