Successfully reported this slideshow.
Upcoming SlideShare
×

# Chapter 14 quick_sort

1,295 views

Published on

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### Chapter 14 quick_sort

1. 1. Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Quick Sort
2. 2. Objectives <ul><li>Explore how to sort an array using quick sort </li></ul>
3. 3. Sorting a List <ul><li>Quick sort </li></ul>
4. 4. Quick Sort
5. 5. Quick Sort (continued) <ul><li>Several ways to determine pivot </li></ul><ul><li>Pivot is chosen so that, it is hoped, lowerSublist and upperSublist are of nearly equal size </li></ul><ul><li>The choice of pivot affects the performance of the algorithm </li></ul><ul><li>We choose the middle element of the list as pivot </li></ul>
6. 6. Quick Sort (continued) <ul><li>The partition procedure that we describe partitions this list using pivot as the middle element, in our case 50, as shown in Figure Q-2 </li></ul>
7. 7. Quick Sort (continued) <ul><li>Partition algorithm: </li></ul><ul><li>Determine pivot , and swap pivot with the first element </li></ul><ul><li>of the list. </li></ul><ul><li>Suppose that the index smallIndex points to the last element less than pivot . The index smallIndex is initialized to the first element of the list. </li></ul><ul><li>2. For the remaining elements in the list (starting at the </li></ul><ul><li>second element): If the current element is less than pivot </li></ul><ul><ul><li>a. Increment smallIndex . </li></ul></ul><ul><ul><li>b. Swap the current element with the array element </li></ul></ul><ul><ul><li>pointed to by smallIndex . </li></ul></ul><ul><li>3. Swap the first element, that is, pivot , with the array </li></ul><ul><li>element pointed to by smallIndex . </li></ul>
8. 8. Quick Sort (continued)
9. 9. Quick Sort (continued)
10. 10. Quick Sort (continued)
11. 11. Quick Sort (continued)
12. 12. Quick Sort (continued)
13. 13. Quick Sort (continued)
14. 14. Quick Sort (continued)
15. 15. Quick Sort (continued)