lecture12

# lecture12 - 1 We sday, Fe 23 dne b orting Algorithm part I...

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

1 Wednesday, Feb 23 rd 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
2 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 “Sony” 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.
3 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
4 The Quicksort Algorithm 1. 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 . 1. Move all elements that are less than or equal to P to the left of the array and all elements greater than P right (this is called partitioning ). Divide 4. Recursively repeat this process on the left sub-array and then the right sub-array. Conquer
5 QuickSort USC Grad History Major Bio EE CS 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 right ; P goes in-between. Recursively repeat this process on the left items P Everything on this side is smaller than item P! larger And item P is exactly in the right spot in between! right items

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

View Full Document
6 QuickSort 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 right ; P goes in-between. Recursively repeat this process on the left items right items P P P Everything left of EE Major (our first P) is now sorted !
7 QuickSort USC Grad History Major Bio EE MBA 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 right ; P goes in-between. Recursively repeat this process on the left items right items P P P P 3 Everything right of EE Major (our first P) is now sorted ! Finally, all items are sorted!

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

View Full Document
8 0 7 D&C Sorts: Quicksort And here’s an actual Quicksort C++ function: void QuickSort(int Array[],int First,int Last) { if (Last – First >= 1 ) { int PivotIndex; PivotIndex = Partition (Array,First,Last); QuickSort (Array,First,PivotIndex-1); // left QuickSort (Array,PivotIndex+1,Last); // right } } First specifies the starting element of the array to sort. Last specifies the last Only bother sorting arrays of at least two elements! DIVIDE Pick an element. Move <= items left Move > items right CONQUER Apply our QS algorithm to the left half of the array.
This is the end of the preview. Sign up to access the rest of the document.

## lecture12 - 1 We sday, Fe 23 dne b orting Algorithm part I...

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

View Full Document
Ask a homework question - tutors are online