9781111530532 ppt ch14_quick_sort

15
Java Programming: From Problem Analysis to Program Design, 5e Chapter 14 Quick Sort

Upload: terry-yoast

Post on 20-May-2015

475 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: 9781111530532 ppt ch14_quick_sort

Java Programming: From Problem Analysis to Program Design, 5e

Chapter 14Quick Sort

Page 2: 9781111530532 ppt ch14_quick_sort

Java Programming: From Problem Analysis to Program Design, 5e 2

Objectives• Explore how to sort an array using quick

sort

Page 3: 9781111530532 ppt ch14_quick_sort

Java Programming: From Problem Analysis to Program Design, 5e 3

Sorting a List

• Quick sort

Page 4: 9781111530532 ppt ch14_quick_sort

Java Programming: From Problem Analysis to Program Design, 5e 4

Quick Sort

Page 5: 9781111530532 ppt ch14_quick_sort

Java Programming: From Problem Analysis to Program Design, 5e 5

Quick Sort (continued)

• Several ways to determine pivot

• Pivot is chosen so that, it is hoped, lowerSublist and upperSublist are of nearly equal size

• The choice of pivot affects the performance of the algorithm

• We choose the middle element of the list as pivot

Page 6: 9781111530532 ppt ch14_quick_sort

Java Programming: From Problem Analysis to Program Design, 5e 6

Quick Sort (continued)• 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

Page 7: 9781111530532 ppt ch14_quick_sort

Java Programming: From Problem Analysis to Program Design, 5e 7

Quick Sort (continued)Partition algorithm: 1. Determine pivot, and swap pivot with the first element of the list. 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.

2. For the remaining elements in the list (starting at the second element): If the current element is less than pivot

a. Increment smallIndex.b. Swap the current element with the array element pointed to by smallIndex.

3. Swap the first element, that is, pivot, with the array element pointed to by smallIndex.

Page 8: 9781111530532 ppt ch14_quick_sort

Java Programming: From Problem Analysis to Program Design, 5e 8

Quick Sort (continued)

Page 9: 9781111530532 ppt ch14_quick_sort

Java Programming: From Problem Analysis to Program Design, 5e 9

Quick Sort (continued)

Page 10: 9781111530532 ppt ch14_quick_sort

Java Programming: From Problem Analysis to Program Design, 5e 10

Quick Sort (continued)

Page 11: 9781111530532 ppt ch14_quick_sort

Java Programming: From Problem Analysis to Program Design, 5e 11

Quick Sort (continued)

Page 12: 9781111530532 ppt ch14_quick_sort

Java Programming: From Problem Analysis to Program Design, 5e 12

Quick Sort (continued)

Page 13: 9781111530532 ppt ch14_quick_sort

Java Programming: From Problem Analysis to Program Design, 5e 13

Quick Sort (continued)

Page 14: 9781111530532 ppt ch14_quick_sort

Java Programming: From Problem Analysis to Program Design, 5e 14

Quick Sort (continued)

Page 15: 9781111530532 ppt ch14_quick_sort

Java Programming: From Problem Analysis to Program Design, 5e 15

Quick Sort (continued)