25 - CMPSCI 187 Programming With Data Structures Lecture#25...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
CMPSCI 187: Programming With Data Structures Lecture #25: Sorting: QuickSort and MergeSort 9 November 2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sorting: QuickSort and MergeSort • Review of O(n^2) Sorting Algorithms • Divide-And-Conquer Sorting • QuickSort: Thoughtful Divide, Easy Recombine • MergeSort: Easy Divide, Thoughtful Recombine • Why MergeSort is O(n log n) • Why QuickSort is Kind of O(n log n) • A Brief Word on Radix Sort
Background image of page 2
Review of O(n^2) Sorting Algorithms • We can insert an element into its proper place in an ordered list, taking time O(k) if the list has k elements. Insertion Sort creates a sorted list from an arbitrary list by inserting the n elements in turn, taking O(n^2) time in all. • Finding the smallest element in an unordered list of size k also takes O(k) time. Selection Sort takes the smallest, then the next smallest, then the next smallest, and so on, also taking total time O(n^2). • Bubble Sort makes repeated passes over the input, switching adjacent elements if they are out of order -- n - 1 passes of O(n) each for O(n^2) time. • Counting Sort works if the elements are all distinct -- simply compare every element to every other, record how many wins each has, then place the element with i wins in location i of the ±nal array.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
• In Binary Search we profted by dividing our search space in halF and, with one comparison, reducing our search problem to a similar problem oF halF the size. • Sorting can be addressed by dividing our input set into two pieces, recursively solving the two smaller sorting problems, and combining the two resulting sorted lists into a single sorted output list. • The base case is a list oF size 1, which is already sorted. We can hope to get From size n to size 1 with log n levels oF recursion. IF we spend O(n) time at each level, we get O(n log n) total time, signifcantly better than O(n^2). • How we divide and how we recombine will determine two important sorting
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/22/2011 for the course COMPSCI 187 taught by Professor Barrington during the Fall '11 term at UMass (Amherst).

Page1 / 11

25 - CMPSCI 187 Programming With Data Structures Lecture#25...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online