Insertion Sort Overview Introduction to Algorithms Introduction to Algorithms � How do we know � How do we know where to place the Insertion Sort Insertion Sort next card? � What assumptions do we make at each CSE 680 step? Prof. Roger Crawfis � What is the algorithm? l i h ? Insertion Sort Algorithm g Inserting an Element g � Given: a set U of unsorted elements � Given: a set U of unsorted elements � Any ideas on how to insert element e � Any ideas on how to insert element e into our sorted sequence S ? � Algorithm: Foreach element e in U Foreach element e in U 1 1. remove e from U 2. place e in the sorted sequence S at the correct location. 3. � Step 3 needs some refinement. What is it’s Problem Statement? � What are the Data Structures ? �
Insertion Sort Algorithm g Insertion Sort Example p � The following slides are from David � The following slides are from David � Your book s pseudo code: � Your book’s pseudo-code: Why go Why go Luebke when he taught the course at the backwards? University of Virginia: y g http://www.cs.virginia.edu/~luebke/cs332/ � Thanks David! � Unfortunately, he flipped the indices i � What are the differences? and j from the book. An Example: Insertion Sort p An Example: Insertion Sort p i = ∅ j = ∅ key = ∅ 30 10 40 20 InsertionSort(A, n) { A[j] = ∅ A[j] = ∅ A[j+1] = ∅ A[j+1] = ∅ for i = 2 to n { 1 2 3 4 key = A[i] InsertionSort(A, n) { j j = i - 1; ; for i = 2 to n { f i 2 t { while (j > 0) and (A[j] > key) { key = A[i] A[j+1] = A[j] j = i - 1; j = j j = j - 1 1 while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { A[j+1] = A[j] } j = j - 1 A[j+1] = key } } A[j+1] = key } } } } David Luebke 7 9/29/2009 David Luebke 8 9/29/2009
An Example: Insertion Sort p An Example: Insertion Sort p i = 2 j = 1 key = 10 i = 2 j = 1 key = 10 30 10 40 20 30 30 40 20 A[j] = 30 A[j] = 30 A[j+1] = 10 A[j+1] = 10 A[j] = 30 A[j] = 30 A[j+1] = 30 A[j+1] = 30 1 2 3 4 1 2 3 4 InsertionSort(A, n) { InsertionSort(A, n) { f for i = 2 to n { i 2 t { f for i = 2 to n { i 2 t { key = A[i] key = A[i] j = i - 1; j = i - 1; while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { A[j+1] = A[j] A[j+1] = A[j] j = j - 1 j = j - 1 } } A[j+1] = key A[j+1] = key } } } } } } David Luebke 9 9/29/2009 David Luebke 10 9/29/2009 An Example: Insertion Sort p An Example: Insertion Sort p i = 2 j = 1 key = 10 i = 2 j = 0 key = 10 30 30 40 20 30 30 40 20 A[j] = ∅ A[j] = ∅ A[j] = 30 A[j] = 30 A[j+1] = 30 A[j+1] = 30 A[j+1] = 30 A[j+1] = 30 1 2 3 4 1 2 3 4 InsertionSort(A, n) { InsertionSort(A, n) { f for i = 2 to n { i 2 t { f for i = 2 to n { i 2 t { key = A[i] key = A[i] j = i - 1; j = i - 1; while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { A[j+1] = A[j] A[j+1] = A[j] j = j - 1 j = j - 1 } } A[j+1] = key A[j+1] = key } } } } } } David Luebke 11 9/29/2009 David Luebke 12 9/29/2009
An Example: Insertion Sort p An Example: Insertion Sort p i = 2 j = 0 key = 10 i = 2 j = 0 key = 10 30 30 40 20 10 30 40 20 A[j] = ∅ A[j] = ∅ A[j] = ∅ A[j] = ∅ A[j+1] = 30 A[j+1] = 30 A[j+1] = 10 A[j+1] = 10 1 2 3 4 1 2 3 4 InsertionSort(A, n) { InsertionSort(A, n) { f for i = 2 to n { i 2 t { f for i = 2 to n { i 2 t { key = A[i] key = A[i] j = i - 1; j = i - 1; while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { A[j+1] = A[j] A[j+1] = A[j] j = j - 1 j = j - 1 } } A[j+1] = key A[j+1] = key } } } } } } David Luebke 13 9/29/2009 David Luebke 14 9/29/2009 An Example: Insertion Sort p An Example: Insertion Sort p i = 3 j = 0 key = 10 i = 3 j = 0 key = 40 10 30 40 20 10 30 40 20 A[j] = ∅ A[j] = ∅ A[j] = ∅ A[j] = ∅ A[j+1] = 10 A[j+1] = 10 A[j+1] = 10 A[j+1] = 10 1 2 3 4 1 2 3 4 InsertionSort(A, n) { InsertionSort(A, n) { f for i = 2 to n { i 2 t { f for i = 2 to n { i 2 t { key = A[i] key = A[i] j = i - 1; j = i - 1; while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { A[j+1] = A[j] A[j+1] = A[j] j = j - 1 j = j - 1 } } A[j+1] = key A[j+1] = key } } } } } } David Luebke 15 9/29/2009 David Luebke 16 9/29/2009
An Example: Insertion Sort p An Example: Insertion Sort p i = 3 j = 0 key = 40 i = 3 j = 2 key = 40 10 30 40 20 10 30 40 20 A[j] = ∅ A[j] = ∅ A[j+1] = 10 A[j+1] = 10 A[j] = 30 A[j] = 30 A[j+1] = 40 A[j+1] = 40 1 2 3 4 1 2 3 4 InsertionSort(A, n) { InsertionSort(A, n) { f for i = 2 to n { i 2 t { f for i = 2 to n { i 2 t { key = A[i] key = A[i] j = i - 1; j = i - 1; while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { A[j+1] = A[j] A[j+1] = A[j] j = j - 1 j = j - 1 } } A[j+1] = key A[j+1] = key } } } } } } David Luebke 17 9/29/2009 David Luebke 18 9/29/2009 An Example: Insertion Sort p An Example: Insertion Sort p i = 3 j = 2 key = 40 i = 3 j = 2 key = 40 10 30 40 20 10 30 40 20 A[j] = 30 A[j] = 30 A[j+1] = 40 A[j+1] = 40 A[j] = 30 A[j] = 30 A[j+1] = 40 A[j+1] = 40 1 2 3 4 1 2 3 4 InsertionSort(A, n) { InsertionSort(A, n) { f for i = 2 to n { i 2 t { f for i = 2 to n { i 2 t { key = A[i] key = A[i] j = i - 1; j = i - 1; while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { A[j+1] = A[j] A[j+1] = A[j] j = j - 1 j = j - 1 } } A[j+1] = key A[j+1] = key } } } } } } David Luebke 19 9/29/2009 David Luebke 20 9/29/2009
An Example: Insertion Sort p An Example: Insertion Sort p i = 4 j = 2 key = 40 i = 4 j = 2 key = 20 10 30 40 20 10 30 40 20 A[j] = 30 A[j] = 30 A[j+1] = 40 A[j+1] = 40 A[j] = 30 A[j] = 30 A[j+1] = 40 A[j+1] = 40 1 2 3 4 1 2 3 4 InsertionSort(A, n) { InsertionSort(A, n) { f for i = 2 to n { i 2 t { f for i = 2 to n { i 2 t { key = A[i] key = A[i] j = i - 1; j = i - 1; while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { A[j+1] = A[j] A[j+1] = A[j] j = j - 1 j = j - 1 } } A[j+1] = key A[j+1] = key } } } } } } David Luebke 21 9/29/2009 David Luebke 22 9/29/2009 An Example: Insertion Sort p An Example: Insertion Sort p i = 4 j = 2 key = 20 i = 4 j = 3 key = 20 10 30 40 20 10 30 40 20 A[j] = 30 A[j] = 30 A[j+1] = 40 A[j+1] = 40 A[j] = 40 A[j] = 40 A[j+1] = 20 A[j+1] = 20 1 2 3 4 1 2 3 4 InsertionSort(A, n) { InsertionSort(A, n) { f for i = 2 to n { i 2 t { f for i = 2 to n { i 2 t { key = A[i] key = A[i] j = i - 1; j = i - 1; while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { A[j+1] = A[j] A[j+1] = A[j] j = j - 1 j = j - 1 } } A[j+1] = key A[j+1] = key } } } } } } David Luebke 23 9/29/2009 David Luebke 24 9/29/2009
An Example: Insertion Sort p An Example: Insertion Sort p i = 4 j = 3 key = 20 i = 4 j = 3 key = 20 10 30 40 20 10 30 40 40 A[j] = 40 A[j] = 40 A[j+1] = 20 A[j+1] = 20 A[j] = 40 A[j] = 40 A[j+1] = 40 A[j+1] = 40 1 2 3 4 1 2 3 4 InsertionSort(A, n) { InsertionSort(A, n) { f for i = 2 to n { i 2 t { f for i = 2 to n { i 2 t { key = A[i] key = A[i] j = i - 1; j = i - 1; while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { A[j+1] = A[j] A[j+1] = A[j] j = j - 1 j = j - 1 } } A[j+1] = key A[j+1] = key } } } } } } David Luebke 25 9/29/2009 David Luebke 26 9/29/2009 An Example: Insertion Sort p An Example: Insertion Sort p i = 4 j = 3 key = 20 i = 4 j = 3 key = 20 10 30 40 40 10 30 40 40 A[j] = 40 A[j] = 40 A[j+1] = 40 A[j+1] = 40 A[j] = 40 A[j] = 40 A[j+1] = 40 A[j+1] = 40 1 2 3 4 1 2 3 4 InsertionSort(A, n) { InsertionSort(A, n) { f for i = 2 to n { i 2 t { f for i = 2 to n { i 2 t { key = A[i] key = A[i] j = i - 1; j = i - 1; while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { while (j > 0) and (A[j] > key) { A[j+1] = A[j] A[j+1] = A[j] j = j - 1 j = j - 1 } } A[j+1] = key A[j+1] = key } } } } } } David Luebke 27 9/29/2009 David Luebke 28 9/29/2009
Recommend
More recommend