You are on page 1of 13

Insertion Sort

• Idea: like sorting a handful of playing cards


– Start with an empty left hand and the cards facing
down on the table.
– Remove one card at a time from the table, and insert
it into the correct position in the left hand
• compare it with each of the cards already in the hand, from
right to left
– The cards held in the left hand are sorted
• these cards were originally the top cards of the pile on the
table

1
Insertion Sort

To insert 12, we need to


make room for it by moving
first 36 and then 24.
10 24 3
6 6

12

2
Insertion Sort

6 10 24 36

12

3
Insertion Sort

6 10 24 3
6

12

4
Insertion Sort

6 10 12 24 36 Sorted

5
Insertion Sort
The array is virtually split into a sorted and an unsorted part. Values from the unsorted
part are picked and placed at the correct position in the sorted part.

input array

5 2 4 6 1
3
at each iteration, the array is divided in two sub-arrays:

left sub-array right sub-array

sorted unsorted

To sort an array of size N in ascending order, iterate over the array and compare the current
element (key) to its predecessor. If the key element is smaller than its predecessor, compare it to
the elements before. Move the greater elements one position up to make space for the swapped
element.
Insertion Sort
Initially, the first two elements of the array
are compared in insertion sort. Swap them if
unsorted.

Now, move to the next two elements


and compare them

7
AlgorithmINSERTION-SORT
INSERTION-SORT(A) 1 2 3 4 5 6 7 8

for j ← 2 to n a1 a2 a3 a4 a5 a6 a7 a8

do key ← A[ j ] key
Insert A[ j ] into the sorted sequence A[1 . . j -1]

i←j-1
while i > 0 and A[i] > key
do A[i + 1] ← A[i]
i←i–1
A[i + 1] ← key
• Insertion sort – sorts the elements in place
8
Analysis of Insertion Sort
INSERTION-SORT(A) cost
for j ← 2 to n times
do key ← A[ j ] c1 n
c2
Insert A[ j ] into the sorted sequence A[1 . . j -1] n-1
i←j-1 0 n-1
 jn-1
n
while i > 0 and A[i] > key c4 t
2 j


n
(t j  1)
do A[i + 1] ← A[i] c5 j 2


n
i←i–1 (t j  1)
c6 j 2

A[i + 1] ← key c7
tj: # of times the while statement is executed at iterationcj n-
n 8
T (n)  c1n  c2 (n  1)  c4 (n  1)  c5  t j  c6  t j  1 c7 1t j  1 c8 (n  1)
n n

j 2 j 2 j 2
9
Best Case Analysis
• The array is already sorted “while i > 0 and A[i] > key”
– A[i] ≤ key upon the first time the while loop test is run
(when i = j -1)

– tj = 1

• T(n) = c1n + c2(n -1) + c4(n -1) + c5(n -1) + c8(n-1) =


(c1 + c2 + c4 + c5 + c8)n + (c2 + c4 + c5 + c8)

= an + b = (n)
T (n)  c1n  c2 (n  1)  c4 (n  1)  c5  t j  c6  t j  1 c7  t j  1 c8 (n  1)
n n n

j 2 j 2 j 2
10
Worst Case Analysis
• The array is in reverse sorted order “while i > 0 and A[i] > key”
– Always A[i] > key in while loop test
– Have to compare key with all elements to the left of the j-th
position  compare with j-1 elements  tj = j
n
n(n  1) n
n(n  1) n
n(n  1)
using 
j 1
j
2
  j 
j 2 2
 1   ( j  1) 
j 2 2
we have:

 n ( n  1)  n ( n  1) n( n  1)
T ( n )  c1n  c2 ( n  1)  c4 ( n  1)  c5   1  c6  c7  c8 ( n  1)
 2  2 2

 an 2  bn  c a quadratic function of n

• T(n) = (n2) order nof growth in n 2

T (n)  c1n  c2 (n  1)  c4 (n  1)  c5  t j  c6  t j  1 c7  t j  1 c8 (n  1)


n n

j 2 j 2 j 2 11
Comparisons and Exchanges in
Insertion Sort
INSERTION-SORT(A) cost times
c1 n
for j ← 2 to n
c2 n-1
do key ← A[ j ]
0 n-1
Insert A[ j ] into the sorted sequence A[1 . . j
-1] c4 n-1
n 2
/2 comparisons
c5
 t
n
i←j-1 j 2 j
c6
 (t
n
while i > 0 and A[i] > key j 2 j  1)
c7
do A[i + 1] ← A[i]  (t
n
2
n /2 exchanges j 2 j  1)
c8 n-1
i←i–1
A[i + 1] ← key 12
Insertion Sort - Summary
• Advantages
– Good running time for “almost sorted” arrays (n)
• Disadvantages
 (n2) running time in worst and average case
  n2/2 comparisons and exchanges

13

You might also like