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

Week 3 Slide 2 Insertion Sort

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Week 3 Slide 2 Insertion Sort

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Insertion Sort

 while some elements unsorted:


 Using linear search, find the location in the sorted
portion where the 1st element of the unsorted
portion should be inserted
 Move all the elements after the insertion location
up one position to make space for the new element
45

38 45
60 60
66 45
66 79 47 13 74 36 21 94 22 57 16 29 81

the fourth iteration of this loop is shown here


An insertion sort partitions the array into two regions
An insertion sort of an array of five integers
Insertion Sort Algorithm
public void insertionSort(Comparable[] arr) {
for (int i = 1; i < arr.length; ++i) {
Comparable temp = arr[i];
int pos = i;
// Shuffle up all sorted items > arr[i]
while (pos > 0 &&
arr[pos-1].compareTo(temp) > 0) {
arr[pos] = arr[pos–1];
pos--;
} // end while
// Insert the current item
arr[pos] = temp;
}
}
Insertion Sort Analysis
public void insertionSort(Comparable[] arr) {
for (int i = 1; i < arr.length; ++i) { outer loop
Comparable temp = arr[i]; outer times
int pos = i;
// Shuffle up all sorted items > arr[i]
while (pos > 0 &&
inner loop arr[pos-1].compareTo(temp) > 0) {
arr[pos] = arr[pos–1]; inner times
pos--;
} // end while
// Insert the current item
arr[pos] = temp;
}
}
Insertion Sort: Cost
Function
 1 operation to initialize the outer loop
 The outer loop is evaluated n-1 times
 5 instructions (including outer loop comparison and
increment)
 Total cost of the outer loop: 5(n-1)
 How many times the inner loop is evaluated is affected
by the state of the array to be sorted
 Best case: the array is already completely sorted so no
“shifting” of array elements is required.
 We only test the condition of the inner loop once (2
operations = 1 comparison + 1 element comparison), and
the body is never executed
 Requires 2(n-1) operations.
Insertion Sort: Cost
Function
 Worst case: the array is sorted in reverse order (so each
item has to be moved to the front of the array)
 In the i-th iteration of the outer loop, the inner loop will
perform 4i+1 operations
 Therefore, the total cost of the inner loop will be 2n(n-1)+n-
1
 Time cost:
 Best case: 7(n-1)
 Worst case: 5(n-1)+2n(n-1)+n-1

 What about the number of moves?


 Best case: 2(n-1) moves
 Worst case: 2(n-1)+n(n-1)/2
Insertion Sort: Average
Case
 Is it closer to the best case (n comparisons)?
 The worst case (n * (n-1) / 2) comparisons?
 It turns out that when random data is sorted, insertion sort is
usually closer to the worst case
 Around n * (n-1) / 4 comparisons
 Calculating the average number of comparisons more exactly
would require us to state assumptions about what the “average”
input data set looked like
 This would, for example, necessitate discussion of how items
were distributed over the array
 Exact calculation of the number of operations required to
perform even simple algorithms can be challenging
(for instance, assume that each initial order of elements has
the same probability to occur)
9 Insertion Sort

 Idea: like sorting a hand 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
Insertion Sort

10

To insert 12, we need to


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

12
Insertion Sort
11

6 10 24 36

12
Insertion Sort
12

6 10 24 3
6

12
13 Insertion Sort
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
14 Insertion Sort
15
Alg.: 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
16

Alg.: INSERTION-SORT(A)
for j ← 2 to n
do key ← A[ j ]
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
Invariant: at the start of the for loop the elements in A[1 . . j-1]
are in sorted order
Proving Loop Invariants
17

 Proving loop invariants works like induction


 Initialization (base case):
 It is true prior to the first iteration of the loop

 Maintenance (inductive step):


 If it is true before an iteration of the loop, it remains true
before the next iteration
 Termination:
 When the loop terminates, the invariant gives us a
useful property that helps show that the algorithm is
correct
 Stop the induction when the loop terminates
Loop Invariant for Insertion Sort
18
 Initialization:
 Just before the first iteration, j = 2:

the subarray A[1 . . j-1] = A[1], (the element


originally in A[1]) – is sorted
Loop Invariant for Insertion Sort
19
 Maintenance:
 the while inner loop moves A[j -1], A[j -2], A[j -3], and so on, by one
position to the right until the proper position for key (which has the value
that started out in A[j]) is found
 At that point, the value of key is placed into this position.
Loop Invariant for Insertion Sort
20
 Termination:
 The outer for loop ends when j = n + 1  j-1 = n
 Replace n with j-1 in the loop invariant:
 the subarray A[1 . . n] consists of the elements originally in A[1 . . n], but in
sorted order

j-1 j

 The entire array is sorted!

Invariant: at the start of the for loop the elements in A[1 . . j-1]
are in sorted order
Analysis of Insertion Sort
21
INSERTION-SORT(A) cost
times
for j ← 2 to n
c1 n
do key ← A[ j ]
Insert A[ j ] into the sorted sequence A[1c.2 . j -1] n-
1
i←j-1
0 
n
n-t
while i > 0 and A[i] > key j 2 j
1

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

n

i←i–1 1
j 2 j

A[i + 1] ← key c5
tj: # of times the while statement is executed at iteration j
n n c6
n
T (n) c1n  c2 (n  1)  c4 (n  1)  c5  t j  c6  t j  1 c7  t j  1 c8 (n  1)
j 2 j 2 c7
j 2
Best Case Analysis
22
 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 = O(n)
n n n
T (n) c1n  c2 (n  1)  c4 (n  1)  c5  t j  c6  t j  1 c7  t j  1 c8 (n  1)
j 2 j 2 j 2
Worst Case Analysis

“while i > 0 and A[i] > key”


 The array is in reverse sorted order
 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

n n n
T (n) T(n) (n  12)) c4 (n  1)  c5 order
c1n =c2 O(n t j  c6 oft jgrowth
 1 c7 int jn 21 c8 (n  1)
j 2 j 2 j 2
23
Comparisons and Exchanges in Insertion Sort
24
INSERTION-SORT(A)
cost
for j ← 2 to n
times
do key ← A[ j ] c1 n
Insert A[ j ] into the sorted sequence
c2 n-
A[1 . . j -1]
 n2/2 comparisons 1
i←j-1
 n-t
n
0 j 2 j

while i > 0 and A[i] > key


 (t
n
1 j 2 j  1)
do A[i + 1] ←2 A[i]  n-(t
n
 n /2 exchanges c4 j 2 j  1)
i←i–1 1
A[i + 1] ← key c5
25 Insertion Sort - Summary

 Advantages
 Good running time for “almost sorted” arrays O(n)
 Disadvantages
 O(n2) running time in worst and average case
  n2/2 comparisons and exchanges

You might also like