Sorting Review
z Insertion Sort Introduction to Algorithms z T(n) = Θ(n2) z In-place Quicksort z Merge Sort z T(n) = Θ(n lg(n)) z Not in-place CSE 680 z Selection Sort (from homework) Prof. Roger Crawfis z T(n) = Θ(n2) z In-place Seems pretty good. z Heap Sort Can we do better? z T(n) = Θ(n lg(n)) z In-place
Sorting Comppgarison Sorting
z Assumptions z Given a set of n values, there can be n! 1. No knowledge of the keys or numbers we permutations of these values. are sorting on. z So if we look at the behavior of the 2. Each key supports a comparison interface sorting algorithm over all possible n! or operator. inputs we can determine the worst -case 3. Sorting entire records, as opposed to complexity of the algorithm. numbers,,p is an implementation detail. 4. Each key is unique (just for convenience). Comparison Sorting Decision Tree Decision Tree Model
z Decision tree model ≤ 1:2 > z Full binary tree 2:3 1:3 z A full binary tree (sometimes proper binary tree or 2- ≤≤> > tree) is a tree in which every node other than the leaves has two child ren <1,2,3> ≤ 1:3 >><2,1,3> ≤ 2:3 z Internal node represents a comparison. z Ignore control, movement, and all other operations, just <1,3,2> <3,1,2> <2,3,1> <3,2,1> see comparison z Each leaf represents one possible result (a permutation of the elements in sorted order). Internal node i:j indicates comparison between ai and aj. z The height of the tree (i . e., longest path) is the suppose three elements < a1, a2, a3> with instance <6, 8, 5> lower bound. Leaf node <π(1), π(2), π(3)> indicates ordering aπ(1)≤ aπ(2)≤ aπ(3). Path of bold lines indicates sorting path for <6,8,5>. There are total 3!=6 possible permutations (paths).
Decision Tree Model QuickSort Design
z The longest path is the worst case number of z Follows the divide-and-conquer paradigm. comparisons. The length of the longest path is the z Divide: Partition (separate) the array A[p..r] into two height of the decision tree. (possibly empty) subarrays A[p..q–1] and A[q+1..r]. z Theorem 8.1: Anyyp comparison sort al gorithm z Each element in A[p..q–1] < A[q]. requires Ω(nlg n) comparisons in the worst case. z A[q] < each element in A[q+1..r]. z Index q is computed as part of the partitioning procedure. z Proof: z Conquer: Sort the two subarrays by recursive calls to z Suppose height of a decision tree is h, and number of quikicksor t. paths (i,e,, permutations) is n!. z Since a binary tree of height h has at most 2h leaves, z Combine: The subarrays are sorted in place – no z n! ≤ 2h , so h ≥ lg (n!) ≥Ω(nlg n))( (B y e quation 3.18 ). workwork isis neededneeded to cocombinembine tthem.hem. z That is to say: any comparison sort in the worst z How do the divide and combine steps of quicksort case needs at least nlg n comparisons. compare with those of merge sort? Pseudocode Example
p r Quicksort(A, p, r) initially: 2 5 8 3 9 4 1 7 10 6 note: pivot (x) = 6 if p