COP3502_28_QuickSort

# COP3502_28_QuickSort - Sorting: Quick Sort Computer Science...

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

Computer Science Department University of Central Florida Sorting: Quick Sort COP 3502 – Computer Science I

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

View Full Document
Sorting: Quick Sort page 2 Sorting: Quick Sort Quick Sort Most common sort used in practice Why? cuz it is usually the quickest in practice! Quick Sort uses two main ideas to achieve this efficiency: 1) The idea of making partitions 2) Recursion Let’s look at the partition concept…
Sorting: Quick Sort page 3 Sorting: Quick Sort Quick Sort – Partition A partition works as follows : Given an array of n elements You must manually select an element in the array to partition by You must then compare ALL the remaining elements against this element If they are greater, Put them to the “right” of the partition element If they are less, Put them to the “left” of the partition element

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

View Full Document
Sorting: Quick Sort page 4 Sorting: Quick Sort Quick Sort – Partition A partition works as follows : Once the partition is complete, what can we say about the position of the partition element? We can say (we KNOW) that the partition element is in its CORRECTLY sorted location In fact, after you partition the array, you are left with: all the elements to the left of the partition element, in the array, that still need to be sorted all the elements to the right of the partition element, in the array, that still need to be sorted And if you sort those two sides, the entire array will be sorted!
Sorting: Quick Sort page 5 Sorting: Quick Sort Quick Sort Partition: Essentially breaks down the sorting problem into two smaller sorting problems …what does that sound like? Code for Quick Sort (at a real general level): 1) Partition the array with respect to a random element 2) Sort the left part of the array using Quick Sort 3) Sort the right part of the array using Quick Sort Notice there is no “merge” step like in Merge Sort at the end, all elements are already in their proper order

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

View Full Document
Sorting: Quick Sort page 6 Sorting: Quick Sort Quick Sort Code for Quick Sort (at a real general level): 1) Partition the array with respect to a random element 2) Sort the left part of the array using Quick Sort 3) Sort the right part of the array using Quick Sort Quick Sort is a recursive algorithm: We need a base case A case that does NOT make recursive calls Our base case, or terminating condition, will be when we sort an array with only one element We know the array is already sorted!
Sorting: Quick Sort page 7 Let S be the input set. 1. If |S| = 0 or |S| = 1 , then return . 2. Pick an element v in S . Call v the partition element . 3.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 09/21/2011 for the course COP 3330 taught by Professor Staff during the Spring '08 term at University of Central Florida.

### Page1 / 37

COP3502_28_QuickSort - Sorting: Quick Sort Computer Science...

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

View Full Document
Ask a homework question - tutors are online