DOC PREVIEW
MSU CSE 830 - Sorting

This preview shows page 1-2-3-27-28-29 out of 29 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

SortingWhy don't CS profs ever stop talking about sorting?!Example ProblemsHeaps and HeapsortDefinitionWhich of these are heaps?Partial Order PropertyQuestionsArray ImplementationInsertion OperationHeap Construction By InsertionHeapify OperationHeap Construction By HeapifyAnalysis: Heap Construction By HeapifyExtract Max OperationHeap SortSorting Algorithm Review ISorting Algorithm Review IIQuicksort OptimizationsPossible reasons for not choosing quicksortLower BoundsExample Decision TreeAnalysis of Decision TreeLinear Time SortingCounting SortRadix SortBucket SortBucket Sort AnalysisBucketsort Gone WrongSorting•Heapsort•Quick review of basic sorting methods•Lower bounds for comparison-based methods•Non-comparison based sortingWhy don't CS profs ever stop talking about sorting?!•Computers spend more time sorting than anything else, historically 25% on mainframes. •Sorting is the best studied problem in computer science, with a variety of different algorithms known. •Most of the interesting ideas we encounter in the course are taught in the context of sorting, such as divide-and-conquer, randomized algorithms, and lower bounds. You should have seen most of the algorithms - we willconcentrate on the analysisExample Problemsa. You are given a pile of thousands of telephone bills and thousands of checks sent in to pay the bills. Find out who did not pay. b. You are given all the book checkout cards used in the campus library during the past year, each of which contains the name of the person who took out the book. Determine how many distinct people checked out at least one book.Heaps and Heapsort•Definition•Operations and uses in heap construction–Insertion–Heapify–Extract max•HeapsortDefinitionA binary heap is defined to be a binary tree with a key in each node such that: 1: All leaves are on, at most, two adjacent levels. 2: All leaves on the lowest level occur to the left, and all levels except the lowest one are completely filled. 3: The key in root is greater than all its children, and the left and right subtrees are again binary heaps. Conditions 1 and 2 specify shape of the tree, and condition 3 the labeling of the tree.Which of these are heaps?Partial Order PropertyThe ancestor relation in a heap defines a partial order on its elements, which means it is reflexive, anti-symmetric, and transitive.Reflexive: x is an ancestor of itself. Anti-symmetric: if x is an ancestor of y and y is an ancestor of x, then x=y.Transitive: if x is an ancestor of y and y is an ancestor of z, x is an ancestor of z.Partial orders can be used to model hierarchies with incomplete information or equal-valued elements.Questions•What are the minimum and maximum number of elements in a heap of height h?–1 node heap has height 0 •What is the height of a heap with n elements?•Where in a heap might the smallest node reside?Array Implementation•Root stored in index 1•Children(x) in locations 2x and 2x+1•Parent(x) in floor(x/2) 1 2 3 4 5 6 7 8 9 10 11 1243 41 29 23 37 17 19 3 7 31 1 2Insertion Operation•Place item to be inserted into leftmost open array slot•If item is greater than parent, swap and recurse•Number of comparisons in the worst case? 1 2 3 4 5 6 7 8 9 10 11 12 1343 41 29 23 37 17 19 3 7 31 1 233Heap Construction By Insertion•Suppose we did heap construction of an n element heap by sequentially inserting n items•Let T(n) denote the number of comparisons needed in the worst-case to build a heap of n items•Define a recurrence relation for T(n)–T(n) =–T(1) =•Solve your recurrence relation to derive the worst-case time to build a heap in this manner.Heapify Operation•Suppose you have a heap EXCEPT a specific value may violate the heap condition•Fix by 3-way comparison working DOWN the heap•WC # of comparisons? 1 2 3 4 5 6 724 41 29 23 37 17 192441232937 17 19Heap Construction By Heapify•How can we construct a heap from n numbers by using the heapify operation?•Example:–5, 3, 17, 10, 84, 19, 6, 22, 9Analysis: Heap Construction By Heapify•There is a direct analysis in the textbook. Here I present a recurrence relation analysis.•Let T(n) denote the number of comparisons needed in the worst-case to build a heap of n items•Define a recurrence relation for T(n)–T(n) =–T(1) =•Solve your recurrence relation to derive the worst-case time to build a heap in this manner.Extract Max Operation•Copy root value to be returned•Move rightmost entry to root•Perform heapify to fix up heap•WC running time? 1 2 3 4 5 6 7 8 9 10 11 12 2 41 29 23 37 17 19 3 7 31 1 -Heap Sort•How can we use a heap and heap operations to solve the sorting problem?•Do we need all three operations studied?–Insertion, Heapify, Extract Max•What is the running time?Sorting Algorithm Review I•Θ(n2) worst-case methods–Insertion Sort–Selection Sort–Bubble Sort•What is the idea behind each method?•What are advantages/disadvantages of each method?Sorting Algorithm Review II•Faster methods–Merge Sort–Quicksort–Heapsort•What is the idea behind merge sort?•What are advantages/disadvantages of each method?Quicksort Optimizations•Quicksort is regarded as the fastest sort algorithm in most cases•Some optimization possibilities–Randomized pivot selection: guarantees never to never have worst-case time due to bad data.–Median of three pivot selection: Can be slightly faster than randomization for somewhat sorted data.–Leave small sub-arrays for insertion sort: Insertion sort can be faster, in practice, for small values of n.–Do the smaller partition first: minimize runtime memory.Possible reasons for not choosing quicksort• Is the data already partially sorted?• Do we know the distribution of the keys?• Is the range of possible keys very small?Lower Bounds•Any comparison-based sorting program can be thought of as defining a decision tree of possible executions.Example Decision TreeAnalysis of Decision Tree•Consider the decision tree T for any comparison-based algorithm. T must have at least n! leaves. Why?•Given that there are n! leaves, what must the height of the decision tree be?•What does this imply about the running time of any comparison-based algorithm?Linear Time Sorting•Algorithms exist for sorting n items in Θ(n) time IF we can make some assumptions about the input data•These algorithms do not sort solely by comparisons, thus avoiding the Ω (n log


View Full Document

MSU CSE 830 - Sorting

Download Sorting
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Sorting and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Sorting 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?