9781111530532 ppt ch14_quick_sort

640 views
587 views

Published on

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
640
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

9781111530532 ppt ch14_quick_sort

  1. 1. Java Programming: From Problem Analysis to Program Design, 5e Chapter 14 Quick Sort
  2. 2. Objectives <ul><li>Explore how to sort an array using quick sort </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  3. 3. Sorting a List <ul><li>Quick sort </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  4. 4. Quick Sort Java Programming: From Problem Analysis to Program Design, 5e
  5. 5. Quick Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e <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) Java Programming: From Problem Analysis to Program Design, 5e <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) Java Programming: From Problem Analysis to Program Design, 5e <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) Java Programming: From Problem Analysis to Program Design, 5e
  9. 9. Quick Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e
  10. 10. Quick Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e
  11. 11. Quick Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e
  12. 12. Quick Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e
  13. 13. Quick Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e
  14. 14. Quick Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e
  15. 15. Quick Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e

×