Lecture 11 - Efficient Sorts, QuickSort and MergeSort and Binary Trees

Lecture 11 - Efficient Sorts, QuickSort and MergeSort and Binary Trees

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Monday, Feb 27th Sorting Algorithms, part II: Quicksort Mergesort Introduction to Trees But first STL Challenge Give me a data structure that I can use to maintain a bunch of peoples names and for each person, allows me to easily get all of the streets they lived on. Assuming I have P total people and each person has lived on an average of E former streets What is the Big-Oh cost of: A. Finding the names of all people who have lived on Levering street? B. Determining if Bill ever lived on Westwood blvd? C. Printing out every name along with each persons street addresses, in alphabetical order. D. Printing out all of the streets that Tala has lived on. Divide and Conquer Sorting Any time you see divide and conquer , you should think recursion ... EEK! The last two sorts well learn (for now) are Quicksort and Mergesort . 1. Divide the elements to be sorted into two groups of roughly equal size. 2. Sort each of these smaller groups of elements (conquer). 3. Combine the two sorted groups into one large sorted list. These sorts generally work as follows: The Quicksort Algorithm 2. Select an arbitrary element P from the array (typically the first element in the array). 1. If the array contains only 0 or 1 element, return . 30 1 77 13 69 40 21 3. Move all elements that are less than or equal to P to the left of the array and all elements greater than P to the right (this is called partitioning ). Divide 4. Recursively repeat this process on the left sub-array and then the right sub-array. Conquer 30 1 77 13 69 40 21 13 1 21 30 69 40 77 QuickSort USC Grad History Major Bio Major EE Major CS Major MBA Drop-out Select an arbitrary item P from the array. Move items smaller than or equal to P to the left and larger items to the right ; P goes in-between. Recursively repeat this process on the left items P Everything on this side is smaller than item P! Everything on this side is larger than item P! And item P is exactly in the right spot in between! Recursively repeat this process on the right items QuickSort USC Grad History Major Bio Major EE Major CS Major MBA Drop-out P P 2 Select an arbitrary item P from the array. Move items smaller than or equal to P to the left and larger items to the right ; P goes in-between. Recursively repeat this process on the left items Recursively repeat this process on the right items Select an arbitrary item P from the array....
View Full Document

This note was uploaded on 04/02/2012 for the course COMPUTER S 32 taught by Professor Smallberg during the Winter '12 term at UCLA.

Page1 / 68

Lecture 11 - Efficient Sorts, QuickSort and MergeSort and Binary Trees

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

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