lecture12

lecture12 - Wednesday, Feb 24 th • Sorting Algorithms,...

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: Wednesday, Feb 24 th • Sorting Algorithms, part II: – Quicksort – Mergesort • Introduction to Trees 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: 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. 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 Everything left of EE Major (our first P) is now sorted ! QuickSort USC Grad History Major Bio Major EE Major 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 to the right ; P goes in-between....
View Full Document

This note was uploaded on 04/10/2012 for the course CS 32 taught by Professor Davidsmallberg during the Winter '08 term at UCLA.

Page1 / 68

lecture12 - Wednesday, Feb 24 th • Sorting Algorithms,...

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