{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Wednesday, Feb 23 rd Sorting Algorithms, part II: Quicksort Mergesort Introduction to Trees
Image of page 1

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

View Full Document Right Arrow Icon
2 But first… STL Challenge Giveme a data structurethat I can useto maintain a bunch of people’s names and for each person, allows meto easily get all of the streets they lived on. Assuming I haveP total peopleand each person has lived on an average of E former streets… What is theBig-Oh cost of: A. Finding thenames of all people who have lived on “Levering street”? B. Determining if “Sony” ever lived on “Westwood blvd”? C. Printing out every namealong with each person’s street addresses, in alphabetical order. D. Printing out all of thestreets that “Tala” has lived on.
Image of page 2
3 Divide and Conquer Sorting Any time you see “ divideand conquer ,” you should think recursion ... EEK! Thelast two sorts we’ll learn (for now) are Quicksort and Mergesort . 1. Divide the elements to besorted into two groups of roughly equal size. 2. Sort each of thesesmaller groups of elements (conquer). 3. Combine the two sorted groups into onelarge sorted list. These sorts generally work as follows:
Image of page 3

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

View Full Document Right Arrow Icon
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 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
Image of page 4
5 QuickSort USC Grad History Major Bio Major EE Major CS Major MBA Drop-out Select an arbitrary item P from thearray. Moveitems 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
Image of page 5

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

View Full Document Right Arrow Icon
6 QuickSort Select an arbitrary item P from thearray. Moveitems 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 thearray. Moveitems 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 Everything left of EE Major (our first P) is now sorted !
Image of page 6
7 QuickSort Select an arbitrary item P from thearray. Moveitems 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 thearray.
Image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern