Introduction to Algorithms Insertion Sort by hcj

VIEWS: 5 PAGES: 43

• pg 1
```									Introduction to Algorithms
Insertion Sort
CSE 680
Prof. Roger Crawfis
Insertion Sort Overview

l How do we know
where to place the
next card?
l What assumptions
do we make at each
step?
l What is the
algorithm?
Insertion Sort Algorithm

lGiven: a set U of unsorted elements
lAlgorithm:
1.   Foreach element e in U
2.     remove e from U
3.     place e in the sorted sequence S at the correct location.

l Step 3 needs some refinement.
l What is it’s Problem Statement?
l What are the Data Structures?
Inserting an Element

lAny ideas on how to insert element e
into our sorted sequence S?
Insertion Sort Algorithm

backwards?

lWhat are the differences?
Insertion Sort Example

lThe following slides are from David
Luebke when he taught the course at the
University of Virginia:
http://www.cs.virginia.edu/~luebke/cs332/

lThanks David!

lUnfortunately, he flipped the indices i
and j from the book.
An Example: Insertion Sort
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke               7                4/1/2011
An Example: Insertion Sort
30         10    40   20      i =  j =  key = 
A[j] =     A[j+1] = 
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        8                        4/1/2011
An Example: Insertion Sort
30         10    40   20      i=2 j=1       key = 10
A[j] = 30     A[j+1] = 10
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        9                         4/1/2011
An Example: Insertion Sort
30         30    40   20      i=2 j=1       key = 10
A[j] = 30     A[j+1] = 30
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        10                        4/1/2011
An Example: Insertion Sort
30         30    40   20      i=2 j=1       key = 10
A[j] = 30     A[j+1] = 30
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        11                        4/1/2011
An Example: Insertion Sort
30         30    40   20      i=2 j=0       key = 10
A[j] =       A[j+1] = 30
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        12                        4/1/2011
An Example: Insertion Sort
30         30    40   20      i=2 j=0       key = 10
A[j] =       A[j+1] = 30
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        13                        4/1/2011
An Example: Insertion Sort
10         30    40   20      i=2 j=0       key = 10
A[j] =       A[j+1] = 10
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        14                        4/1/2011
An Example: Insertion Sort
10         30    40   20      i=3 j=0       key = 10
A[j] =       A[j+1] = 10
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        15                        4/1/2011
An Example: Insertion Sort
10         30    40   20      i=3 j=0       key = 40
A[j] =       A[j+1] = 10
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        16                        4/1/2011
An Example: Insertion Sort
10         30    40   20      i=3 j=0       key = 40
A[j] =       A[j+1] = 10
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        17                        4/1/2011
An Example: Insertion Sort
10         30    40   20      i=3 j=2       key = 40
A[j] = 30     A[j+1] = 40
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        18                        4/1/2011
An Example: Insertion Sort
10         30    40   20      i=3 j=2       key = 40
A[j] = 30     A[j+1] = 40
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        19                        4/1/2011
An Example: Insertion Sort
10         30    40   20      i=3 j=2       key = 40
A[j] = 30     A[j+1] = 40
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        20                        4/1/2011
An Example: Insertion Sort
10         30    40   20      i=4 j=2       key = 40
A[j] = 30     A[j+1] = 40
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        21                        4/1/2011
An Example: Insertion Sort
10         30    40   20      i=4 j=2       key = 20
A[j] = 30     A[j+1] = 40
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        22                        4/1/2011
An Example: Insertion Sort
10         30    40   20      i=4 j=2       key = 20
A[j] = 30     A[j+1] = 40
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        23                        4/1/2011
An Example: Insertion Sort
10         30    40   20      i=4 j=3       key = 20
A[j] = 40     A[j+1] = 20
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        24                        4/1/2011
An Example: Insertion Sort
10         30    40   20      i=4 j=3       key = 20
A[j] = 40     A[j+1] = 20
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        25                        4/1/2011
An Example: Insertion Sort
10         30    40   40      i=4 j=3       key = 20
A[j] = 40     A[j+1] = 40
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        26                        4/1/2011
An Example: Insertion Sort
10         30    40   40      i=4 j=3       key = 20
A[j] = 40     A[j+1] = 40
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        27                        4/1/2011
An Example: Insertion Sort
10         30    40   40      i=4 j=3       key = 20
A[j] = 40     A[j+1] = 40
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        28                        4/1/2011
An Example: Insertion Sort
10         30    40   40      i=4 j=2       key = 20
A[j] = 30     A[j+1] = 40
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        29                        4/1/2011
An Example: Insertion Sort
10         30    40   40      i=4 j=2       key = 20
A[j] = 30     A[j+1] = 40
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        30                        4/1/2011
An Example: Insertion Sort
10         30    30   40      i=4 j=2       key = 20
A[j] = 30     A[j+1] = 30
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        31                        4/1/2011
An Example: Insertion Sort
10         30    30   40      i=4 j=2       key = 20
A[j] = 30     A[j+1] = 30
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        32                        4/1/2011
An Example: Insertion Sort
10         30    30   40      i=4 j=1       key = 20
A[j] = 10     A[j+1] = 30
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        33                        4/1/2011
An Example: Insertion Sort
10         30    30   40      i=4 j=1       key = 20
A[j] = 10     A[j+1] = 30
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        34                        4/1/2011
An Example: Insertion Sort
10         20    30   40      i=4 j=1       key = 20
A[j] = 10     A[j+1] = 20
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke                        35                        4/1/2011
An Example: Insertion Sort
10         20    30   40      i=4 j=1       key = 20
A[j] = 10     A[j+1] = 20
1         2     3    4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}           Done!
David Luebke                        36                        4/1/2011
Memory Analysis

lThe algorithm is said to be in-place if it
uses only a constant amount of memory
in accomplishing its solution.
lClearly, the book’s implementation is
better than mine.
lClearly, the book’s algorithm is better
than mine.
That is, do we consider my implementation to be a
different algorithm or the same?
Correctness
l We can use a property known as loop-invariance to
reason about the correctness of the algorithm.
l For Insertion-Sort, we can say that:
l The subarray A[1..i-1] elements are in sorted order
l A[1..i-1] is the set of numbers from the original A[1..i-1]
Insertion Sort Example
Loop Invariant

lInitialization
lTrue at the beginning of the loop.
lTermination
lThe loop terminates.
lTrue, when the loop exists.
lMaintenance
lTrue at the end of each iteration of the loop.
lThis allows us to prove the invariant
similarly to proof-by-induction.
Insertion Sort
InsertionSort(A, n) {      What is the precondition
for i = 2 to n {         for this loop?
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

David Luebke            41                     4/1/2011
Bubble-Sort

lCan you come up with loop invariants?
lAka, can you prove it is correct?
lHow expensive is it?
lWhen is it most expensive?
lWhen is it least expensive?
Selection Sort

void selectionSort(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
int min = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[min]) {
min = j;
}
}
if (i != min) {
int swap = a[i];
a[i] = a[min];
a[min] = swap;
}
}
}

```
To top