Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms CSCI 235, Fall 2017 Lecture 13 Elementary Sorts II

Similar presentations


Presentation on theme: "Algorithms CSCI 235, Fall 2017 Lecture 13 Elementary Sorts II"— Presentation transcript:

1 Algorithms CSCI 235, Fall 2017 Lecture 13 Elementary Sorts II

2 Insertion Sort Idea: Sort items one at a time into previously sorted group. Invariant: After the ith step of the algorithm, A[1..i] is sorted.

3 Insertion sort algorithm
for i <- 2 to length[A] do Insert(A, i) Insert(A, i) key <- A[i] j <- i while j > 1 and less(key, A[j-1]) do A[j] <- A[j-1] j <- j-1 {Insert key} A[j] <- key

4 Is it Stable and in place?
Stepping through the insertion sort algorithm: A a b c d Example: Array already sorted alphabetically. Sort by numbers as key. If the algorithm is stable, the letters associated with the same number will stay in order relative to one another. Index: We will step through the algorithm in class.

5 Running time Already showed that worst case running time is Q(n2)
This is true for both the recursive version and for the iterative version (as derived earlier in class). Insertion-Sort(A) for i <- 2 to length[A] do Insert(A, i) Array size to be solved decreases by 1 each time through loop. T(n) = T(n-1) + cost of Insert Insert(A, i) key <- A[i] j <- i while j > 1 and less(key, A[j-1]) do A[j] <- A[j-1] j <- j-1 {Insert key} A[j] <- key Array size to be solved decreases by 1 each time through loop. T2(n) = T2(n-1) + cost of assignments = T2(n-1) + 1 T2(n) = Q(n) T(n) = T(n-1) + n T(n) = Q(n2)

6 Selection Sort Idea: On the ith step of the algorithm, store into A[i] the smallest element of A[i..n]. Invariant: After step i, the elements A[1..i] are in their final sorted positions.

7 Selection sort algorithm
for i <- 1 to length[A] - 1 do swap(A, i, Min-Index(A, i, length[A])) Min-Index(A, lo, hi) {Assume lo and hi are legal subscripts, and hi >= lo.} min_index <- lo for i <- lo + 1 to hi do if less(A[i], A[min_index]) then min-index <- i return min_index Is Selection sort stable? Is it in place? We will work through an example in class.

8 Stability of an algorithm
To show that an algorithm is not stable, it is sufficient to find an example for which the relative order of elements is not preserved during the sort. To show that an algorithm is stable may take more reasoning, because an unstable algorithm may preserve the relative order of elements for some initial orderings of an array but not others.

9 Running time of Selection Sort
Selection-Sort(A) for i <- 1 to length[A] - 1 do swap(A, i, Min-Index(A, i, length[A])) Min-Index(A, lo, hi) {Assume lo and hi are legal subscripts, and hi >= lo.} min_index <- lo for i <- lo + 1 to hi do if less(A[i], A[min_index]) then min-index <- i return min_index T(n) = T(n-1) + cost of Min-Index T2(n) = T2(n-1) + cost of assignments and less( ) = T2(n-1) + 1 T2(n) = Q(n) T(n) = T(n-1) + n T(n) = Q(n2)

10 Bubble Sort Idea: On the every step of the algorithm, scan A from left to right and exchange adjacent elements that are out of order. Repeat until a scan finds no elements out of order. Invariant: After step i, (at least) the elements A[(n i)..n] are in their final sorted positions.

11 Bubble sort pseudo-code
Bubble-Sort(A) hi <- length[A] changed <- false repeat changed <- Bubble-Up(A, 1, hi) hi <- hi - 1 until not changed; Bubble-Up(A, lo, hi) for i <- lo to hi - 1 do if less(A[i + 1], A[i]) then swap(A, i, i+1); changed <- true return changed

12 Behavior of Bubble sort
Is Bubble Sort stable? Try sorting: a b c d Is Bubble Sort in place?

13 Running time of Bubble sort
Bubble-Sort(A) hi <- length[A] changed <- false repeat changed <- Bubble-Up(A, 1, hi) hi <- hi - 1 until not changed; Bubble-Up(A, lo, hi) for i <- lo to hi - 1 do if less(A[i + 1], A[i]) then swap(A, i, i+1); changed <- true return changed T(n) = ? T2(n) = ? T(n) = ?

14 Merge Sort Idea: Recursively sort subarrays and then merge them into a single sorted array.

15 Pseudo code for Merge Sort
Merge-Sort(A) MSort(A, 1, length[A]) MSort(A, lo, hi) if lo < hi then mid <- (lo + hi) div 2 MSort(A, lo, mid) MSort(A, mid + 1, hi) Merge(A, lo, mid, hi)

16 Merge pseudo-code Merge(A, lo, mid, hi) n <- (hi - lo) + 1
{Merge elements into temporary array B.} B <-newArray(n) left <- lo right <- mid + 1 for i = 1 to n do if left ≤ mid and (right > hi or less(A[left], A[right])) then B[i] <- A[left] left <- left + 1 else B[i] <- A[right] right <- right + 1 {Copy elements from B back to A} A[left] <- B[i]

17 Behavior of Merge Sort Is Merge Sort stable? Try sorting: a b c d
Is Merge Sort in place? Running Time of Merge Sort: Derived in previous lectures: T(n) = 2T(n/2) + Cost of Merge Cost of Merge: (2 loops, each with maximum size of n) T2(n) = 2n = Q(n) Merge sort: T(n) = 2T(n/2) + n T(n) = Q(nlgn)

18 Summary Algorithm Stable In place Running time Insertion Selection
Bubble Merge


Download ppt "Algorithms CSCI 235, Fall 2017 Lecture 13 Elementary Sorts II"

Similar presentations


Ads by Google