0% found this document useful (0 votes)
29 views

Data Sorting: Insertion Sort: Georgy Gimel'farb

The document discusses insertion sort, a sorting algorithm. It begins with an outline covering ordering, sorting, efficiency, and insertion sort. It then defines key concepts like relations, partial orders, and linear orders. It discusses different types of ordering like numerical, alphabetical, and lexicographic ordering. It introduces the problem of sorting and criteria for sorting algorithms. It explains that comparison-based sorting algorithms can only compare elements, not access their values directly. Finally, it introduces insertion sort, describing its basic approach of splitting the array into ordered and unordered parts and contracting the unordered part through comparisons and element swaps.

Uploaded by

Pavan Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Data Sorting: Insertion Sort: Georgy Gimel'farb

The document discusses insertion sort, a sorting algorithm. It begins with an outline covering ordering, sorting, efficiency, and insertion sort. It then defines key concepts like relations, partial orders, and linear orders. It discusses different types of ordering like numerical, alphabetical, and lexicographic ordering. It introduces the problem of sorting and criteria for sorting algorithms. It explains that comparison-based sorting algorithms can only compare elements, not access their values directly. Finally, it introduces insertion sort, describing its basic approach of splitting the array into ordered and unordered parts and contracting the unordered part through comparisons and element swaps.

Uploaded by

Pavan Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Outline Ordering Sorting Efficiency Insertion sort

Data Sorting: Insertion sort

Georgy Gimel’farb

COMPSCI 220 Algorithms and Data Structures

1 / 16
Outline Ordering Sorting Efficiency Insertion sort

1 Ordering

2 Data sorting

3 Efficiency of comparison-based sorting

4 Insertion sort

2 / 16
Outline Ordering Sorting Efficiency Insertion sort

Relations, Partial Order, and Linear Order

A relation on a set S is a set R of ordered pairs of elements of S,


i.e. a subset, R ⊆ S × S of the set, S × S, of all pairs of these elements.

• An ordered pair (x, y) ∈ R means the element y relates to x.


• The relation is denoted sometimes as yRx.
• An important type of relation: a partial order, which is
reflexive, antisymmetric, and transitive.
Main features of the partial order:
Reflexivity: xRx for every x ∈ S.
Antisymmetry: If xRy and yRx then x = y for every x, y ∈ S.
Transitivity: If xRy and yRz then xRz for every x, y, z ∈ S.
• A linear order (or a total order) is a partial order, such that
every pair of elements is related (i.e. R = S × S).

3 / 16
Outline Ordering Sorting Efficiency Insertion sort

Examples of Linear Order Relations


1 S – the set of real numbers; R – the usual ”less than or equal
to” relation, x ≤ y, for all pairs of numbers.
• For every x ∈ S, x ≤ x.
• For every x, y ∈ S, if x ≤ y and y ≤ x then x = y.
• For every x, y, z ∈ S, if x ≤ y and y ≤ z then x ≤ z.
2 S – the set of Latin letters:

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

Data Ordering: Numerical Order

Ordering relation places each pair α, β of countable data items in


a fixed order denoted as (α, β) or hα, βi.

• Order notation: α ≤ β (less than or equal to).


• Countable item: labelled by a specific integer key.

Comparable objects in Java and Python: if an object can be


less than, equal to, or greater than other object:
Java: object.compareTo( other object ) < 0, = 0, > 0
Python: object. cmp (self,other) < 0, = 0, > 0

Numerical order - on any set of numbers by values of elements:

5 ≤ 5 ≤ 6.45 ≤ 22.79 ≤ . . . ≤ 1056.32


5 / 16
Outline Ordering Sorting Efficiency Insertion sort

Alphabetical and Lexicographic Orders

Alphabetical order - on a set of letters by their position in an


alphabet:
a ≤ b ≤ c ≤ d ≤ e ≤ f ≤ g ≤ h ≤ i ≤ ... ≤ y ≤ z

Such ordering depends on the alphabet used: look into any


bilingual dictionary. . .

Lexicographic order - on a set of strings (e.g. multi-digit


numbers or words) by the first differing character in the strings:

5456 ≤ 5457 ≤ 5500 ≤ 6100 ≤ . . .


pork ≤ ward ≤ word ≤ work ≤ . . .

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 Problem of Sorting

Rearrange an input list of keys, which can be compared using a


total order ≤, into an output list such that key i precedes key j in
the output list if i ≤ j.

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 algorithm is comparison-based if the total order can be


checked only by comparing the order ≤ of a pair of elements at a
time.

• 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

Efficiency of Comparison-Based Sorting


No other information about the keys, except of only their order
relation, can be used.
The running time of sorting is usually dominated by two elementary
operations: a comparison of two items and a move of an item.

Every sorting algorithm in this course makes at most constant


number of moves for each comparison.
• Asymptotic running time in terms of elementary operations is
determined by the number of comparisons.
• Time for a data move depends on the list implementation.
• Sorting makes sense only for linear data structures.

The efficiency of a particular sorting algorithm depends on the number of items


to be sorted; place of sorting (fast internal or slow external memory); to what
extent data items are presorted, etc.
8 / 16
Outline Ordering Sorting Efficiency Insertion sort

Sorting with 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:

At the beginning of each stage i = 1, . . . , n − 1:


i ordered and n − i unordered elements.
i The array to be sorted Ci Mi
44 13 35 18 15 10 20
1 13 44 35 18 15 10 20 1 1
2 13 35 44 18 15 10 20 2 1
3 13 18 35 44 15 10 20 3 2
Ci and Mi – numbers of comparisons and moves at stage i, respectively.
9 / 16
Outline Ordering Sorting Efficiency Insertion sort

Python Code of Insertion Sort

http://interactivepython.org/runestone/static/pythonds/SortSearch/TheInsertionSort.html

# Insertion sort of an input array a of size n


# Each leftmost unordered a[i] is compared right-to-left to the already
# ordered elements a[i-1],...,a[0], being right-shifted to free place
# between them for insertion of the element a[i]

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

a[ k ] = tmp # insert a[i]

10 / 16
Outline Ordering Sorting Efficiency Insertion sort

Java Code of Insertion Sort


// Insertion sort of an input array a of size n
//
// Each leftmost unordered a[i] is compared right-to-left to the already
// ordered elements a[i-1],...,a[0], being right-shifted to free place
// between them for insertion of the element a[i]

public static void insertionSort( int [ ] a ) {


for ( int i = 1; i < a.length; i++ ) {
int tmp = a[ i ]; // pick a[i]
int k = i - 1;
while ( k >= 0 && tmp < a[ k ] ) { // compare to a[k]
a[ k + 1 ] = a[ k ]; // shift a[k] right
k--;
}
a[ k + 1 ] = tmp; // insert a[i]
}
}
11 / 16
Outline Ordering Sorting Efficiency Insertion sort

Insertion Sort: Stages i = 1, 2, 3

0 1 2 3 4 5 6

44 13 35 18 15 10 20
i↓

12 / 16
Outline Ordering Sorting Efficiency Insertion sort

Insertion Sort: Stages i = 1, 2, 3

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

Insertion Sort: Stages i = 1, 2, 3

0 1 2 3 4 5 6

44 13 35 18 15 10 20
i↓

1 13

13 < 44? → Comparison 1 for i = 1

12 / 16
Outline Ordering Sorting Efficiency Insertion sort

Insertion Sort: Stages i = 1, 2, 3

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

Insertion Sort: Stages i = 1, 2, 3

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

Insertion Sort: Stages i = 1, 2, 3

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

Insertion Sort: Stages i = 1, 2, 3

0 1 2 3 4 5 6

44
13 13
44 35 18 15 10 20
i↓

21 13 35

35 < 44? → Comparison 1 for i = 2

12 / 16
Outline Ordering Sorting Efficiency Insertion sort

Insertion Sort: Stages i = 1, 2, 3

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

Insertion Sort: Stages i = 1, 2, 3

0 1 2 3 4 5 6

44
13 13
44 35
44 18 15 10 20
i↓

21 13 35

35 < 13? → Comparison 2 for i = 2

12 / 16
Outline Ordering Sorting Efficiency Insertion sort

Insertion Sort: Stages i = 1, 2, 3

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

Insertion Sort: Stages i = 1, 2, 3

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

Insertion Sort: Stages i = 1, 2, 3

0 1 2 3 4 5 6

44
13 13
44
35 35
44 18 15 10 20
i↓

321 13 35 18

18 < 44? → Comparison 1 for i = 3

12 / 16
Outline Ordering Sorting Efficiency Insertion sort

Insertion Sort: Stages i = 1, 2, 3

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

Insertion Sort: Stages i = 1, 2, 3

0 1 2 3 4 5 6

44
13 13
44
35 35
44 18
44 15 10 20
i↓

321 13 35 18

18 < 35? → Comparison 2 for i = 3

12 / 16
Outline Ordering Sorting Efficiency Insertion sort

Insertion Sort: Stages i = 1, 2, 3

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

Insertion Sort: Stages i = 1, 2, 3

0 1 2 3 4 5 6

44
13 13
44
35 44
35 18
44 15 10 20
i↓

321 13 35 18

18 < 13? → Comparison 3 for i = 3

12 / 16
Outline Ordering Sorting Efficiency Insertion sort

Insertion Sort: Stages i = 1, 2, 3

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

Insertion Sort: Stage i = 4

• Ci – the number of comparisons at stage i.


• Mi – the number of moves at stage i.

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

Insertion Sort: Stage i = 4

• Ci – the number of comparisons at stage i.


• Mi – the number of moves at stage i.

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

Insertion Sort: Stage i = 4

• Ci – the number of comparisons at stage i.


• Mi – the number of moves at stage i.

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

Insertion Sort: Stage i = 4

• Ci – the number of comparisons at stage i.


• Mi – the number of moves at stage i.

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

Insertion Sort: Stage i = 4

• Ci – the number of comparisons at stage i.


• Mi – the number of moves at stage i.

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

Insertion Sort : Stage i = 5

• Ci – the number of comparisons at stage i.


• Mi – the number of moves at stage i.

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

Insertion Sort : Stage i = 5

• Ci – the number of comparisons at stage i.


• Mi – the number of moves at stage i.

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

Insertion Sort : Stage i = 5

• Ci – the number of comparisons at stage i.


• Mi – the number of moves at stage i.

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

Insertion Sort : Stage i = 5

• Ci – the number of comparisons at stage i.


• Mi – the number of moves at stage i.

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

Insertion Sort : Stage i = 5

• Ci – the number of comparisons at stage i.


• Mi – the number of moves at stage i.

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

Insertion Sort : Stage i = 5

• Ci – the number of comparisons at stage i.


• Mi – the number of moves at stage i.

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

Insertion Sort : Stage i = 6

• Ci – the number of comparisons per insertion


• Mi – the number of moves per insertion

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

Insertion Sort : Stage i = 6

• Ci – the number of comparisons per insertion


• Mi – the number of moves per insertion

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

Insertion Sort : Stage i = 6

• Ci – the number of comparisons per insertion


• Mi – the number of moves per insertion

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

Insertion Sort : Stage i = 6

• Ci – the number of comparisons per insertion


• Mi – the number of moves per insertion

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

Total Number of Moves and Comparisons


Insertion sort:
{44, 13, 35, 18, 15, 10, 20} −→ {10, 13, 15, 18, 20, 35, 44}}

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

You might also like