{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

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

Monday, Feb 27th Sorting Algorithms, part II: Quicksort Mergesort Introduction to Trees

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

View Full Document
But first… STL Challenge      Give me a data structure that I can use to maintain a bunch of people’s  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 person’s 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 we’ll 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:

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

View Full Document
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 13 21 30 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

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

View Full Document
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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}