This document discusses the quick sort algorithm for sorting arrays. Quick sort works by selecting a pivot element in the array and partitioning the other elements into two sublists - those less than the pivot and those greater than the pivot. The steps involve determining a pivot element, typically the middle element, partitioning the array by moving all less-than-pivot elements before the pivot and all greater-than elements after it, and then recursively applying the same process to the two sublists until the entire array is sorted. The choice of pivot can affect the performance and several pivot selection methods are discussed.
This document discusses the quick sort algorithm for sorting arrays. Quick sort works by selecting a pivot element in the array and partitioning the other elements into two sublists - those less than the pivot and those greater than the pivot. The steps involve determining a pivot element, typically the middle element, partitioning the array by moving all less-than-pivot elements before the pivot and all greater-than elements after it, and then recursively applying the same process to the two sublists until the entire array is sorted. The choice of pivot can affect the performance and several pivot selection methods are discussed.
This document discusses the quick sort algorithm for sorting arrays. Quick sort works by selecting a pivot element in the array and partitioning the other elements into two sublists - those less than the pivot and those greater than the pivot. The steps involve determining a pivot element, typically the middle element, partitioning the array by moving all less-than-pivot elements before the pivot and all greater-than elements after it, and then recursively applying the same process to the two sublists until the entire array is sorted. The choice of pivot can affect the performance and several pivot selection methods are discussed.
Chapter 14 Quick Sort Objectives • Explore how to sort an array using quick sort
Java Programming: From Problem Analysis to Program Design, 5e 2
Sorting a List
• Quick sort
Java Programming: From Problem Analysis to Program Design, 5e 3
Quick Sort
Java Programming: From Problem Analysis to Program Design, 5e 4
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
Java Programming: From Problem Analysis to Program Design, 5e 5
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
Java Programming: From Problem Analysis to Program Design, 5e 6
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.
Java Programming: From Problem Analysis to Program Design, 5e 7
Quick Sort (continued)
Java Programming: From Problem Analysis to Program Design, 5e 8
Quick Sort (continued)
Java Programming: From Problem Analysis to Program Design, 5e 9
Quick Sort (continued)
Java Programming: From Problem Analysis to Program Design, 5e 10
Quick Sort (continued)
Java Programming: From Problem Analysis to Program Design, 5e 11
Quick Sort (continued)
Java Programming: From Problem Analysis to Program Design, 5e 12
Quick Sort (continued)
Java Programming: From Problem Analysis to Program Design, 5e 13
Quick Sort (continued)
Java Programming: From Problem Analysis to Program Design, 5e 14
Quick Sort (continued)
Java Programming: From Problem Analysis to Program Design, 5e 15