10.8 Heapsort 10.9 Quicksort Chapter 10 - Sorting.

Slides:



Advertisements
Similar presentations
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Advertisements

Section 8.8 Heapsort.  Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations  Heapsort does not require any additional.
Spring 2010CS 2251 Sorting Chapter 8. Spring 2010CS 2252 Chapter Objectives To learn how to use the standard sorting methods in the Java API To learn.
Sorting Chapter 10.
QuickSort QuickSort is often called Partition Sort. It is a recursive method, in which the unsorted array is first rearranged so that there is some record,
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
Sorting Chapter 10. Chapter 10: Sorting2 Chapter Objectives To learn how to use the standard sorting methods in the Java API To learn how to implement.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Sorting Chapter 10. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
1 Joe Meehean.  Problem arrange comparable items in list into sorted order  Most sorting algorithms involve comparing item values  We assume items.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Chapter 10: Sorting1 Sorting. Chapter 10: Sorting2 Chapter Outline How to use standard sorting functions in How to implement these sorting algorithms:
Heaps and basic data structures David Kauchak cs161 Summer 2009.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Prof. U V THETE Dept. of Computer Science YMA
Sorting Mr. Jacobs.
Sorting Why? Displaying in order Faster Searching Categories Internal
Heap Sort Example Qamar Abbas.
QuickSort QuickSort is often called Partition Sort.
Design and Analysis of Algorithms
Teach A level Computing: Algorithms and Data Structures
Priority Queues (Heaps)
Data Structures and Algorithms
Monday, March 19, 2018 Announcements… For Today… For Next Time…
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Sorting Chapter 10.
Sorting Chapter 8.
Lab 08 - BST.
10.6 Shell Sort: A Better Insertion
Lab 10 - Quicksort.
Description Given a linear collection of items x1, x2, x3,….,xn
How can this be simplified?
10.3 Bubble Sort Chapter 10 - Sorting.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Sorting means The values stored in an array have keys of a type for which the relational operators are defined. (We also assume unique keys.) Sorting.
Lab 04 – Linked List.
Wednesday, April 11, 2018 Announcements… For Today…
Algorithm design and Analysis
Advanced Sorting Methods: Shellsort
Quicksort analysis Bubble sort
Lecture 3 / 4 Algorithm Analysis
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
8/04/2009 Many thanks to David Sun for some of the included slides!
C++ Plus Data Structures
24 Searching and Sorting.
Sorting Chapter 8 CS 225.
Sub-Quadratic Sorting Algorithms
Binary Heaps What if we’re mostly concerned with finding the most relevant data? A binary heap is a binary tree (2 or fewer subtrees for each node) A heap.
Heapsort.
Chapter 4.
Sorting Chapter 10.
Chapter 10 Sorting Algorithms
Priority Queues (Heaps)
Searching/Sorting/Searching
Heapsort.
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
Heapsort.
Sorting Taking an arbitrary permutation of n items and rearranging them into total order Sorting is, without doubt, the most fundamental algorithmic.
Heaps & Multi-way Search Trees
CO 303 Algorithm Analysis and Design
Lab 03 – Linked List.
Advanced Sorting Methods: Shellsort
EE 312 Software Design and Implementation I
10.3 Bubble Sort Chapter 10 - Sorting.
Presentation transcript:

10.8 Heapsort 10.9 Quicksort Chapter 10 - Sorting

Attendance Quiz #36 Sorting

Tip #38: STL Additions Sorting Recently added algorithms provide more intuitive parameter lists and cleaner interfaces. For example, copy_if() is a new convenience algorithm that selectively copies every element that satisfies the predicate p. In the following example, copy_if() copies only the positive numbers from the range [first, last) to result: #include <algorithm> struct ispositive // predicate functor { bool operator()(int val) const return val >= 0; } }; int first[n] = { 0, -1, -2, 3 }; int result[n] = { 0 }; copy_if(first, first + n, result, ispositive());

10.8, pgs. 599-601 10.8 Heapsort Heapsort Algorithm Algorithm to Build a Heap Analysis of Heapsort Algorithm Code for Heapsort 10.8, pgs. 599-601

Heapsort Sorting Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations. Heapsort does not require any additional storage. As its name implies, heapsort uses a heap to store the array. A max heap is a data structure with the largest value at the top. Once we have a heap, we can remove one item at a time from the heap. The item removed is always the top element, and we will place it at the bottom of the heap. When we reheap, the larger of a node’s two children is moved up the heap, so the new heap will have the next largest item as its root. As we continue to remove items from the heap, the heap size shrinks as the number of the removed items increases. If we implement the heap using an array, each element removed will be placed at the end of the array, but in front of the elements that were removed earlier. After we remove the last element, the array will be sorted.

Heapsort Sorting Max Heap? 89 76 74 37 32 39 66 20 26 18 28 29 6 Yes!

Heapsort Sorting 89 76 74 37 32 39 66 20 26 18 28 29 6

Heapsort Sorting 6 76 74 37 32 39 66 20 26 18 28 29 89

Heapsort Sorting 76 6 74 37 32 39 66 20 26 18 28 29 89

Heapsort Sorting 76 37 74 6 32 39 66 20 26 18 28 29 89

Heapsort Sorting 76 37 74 26 32 39 66 20 6 18 28 29 89

Heapsort Sorting 76 37 74 26 32 39 66 20 6 18 28 29 89

Heapsort Sorting 29 37 74 26 32 39 66 20 6 18 28 76 89

Heapsort Sorting 74 37 29 26 32 39 66 20 6 18 28 76 89

Heapsort Sorting 74 37 66 26 32 39 29 20 6 18 28 76 89

Heapsort Sorting 74 37 66 26 32 39 29 20 6 18 28 76 89

Heapsort Sorting 28 37 66 26 32 39 29 20 6 18 74 76 89

Heapsort Sorting 66 37 28 26 32 39 29 20 6 18 74 76 89

Heapsort Sorting 66 37 39 26 32 28 29 20 6 18 74 76 89

Heapsort Sorting 66 37 39 26 32 28 29 20 6 18 74 76 89

Heapsort Sorting 18 37 39 26 32 28 29 20 6 66 74 76 89

Heapsort Sorting 39 37 18 26 32 28 29 20 6 66 74 76 89

Heapsort Sorting 39 37 29 26 32 28 18 20 6 66 74 76 89

Heapsort Sorting 39 37 29 26 32 28 18 20 6 66 74 76 89

Heapsort Sorting 6 37 29 26 32 28 18 20 39 66 74 76 89

Heapsort Sorting 37 6 29 26 32 28 18 20 39 66 74 76 89

Heapsort Sorting 37 32 29 26 6 28 18 20 39 66 74 76 89

Heapsort Sorting 37 32 29 26 6 28 18 20 39 66 74 76 89

Heapsort Sorting 20 32 29 26 6 28 18 37 39 66 74 76 89

Heapsort Sorting 32 20 29 26 6 28 18 37 39 66 74 76 89

Heapsort Sorting 32 26 29 20 6 28 18 37 39 66 74 76 89

Heapsort Sorting 32 26 29 20 6 28 18 37 39 66 74 76 89

Heapsort Sorting 18 26 29 20 6 28 32 37 39 66 74 76 89

Heapsort Sorting 29 26 18 20 6 28 32 37 39 66 74 76 89

Heapsort Sorting 29 26 28 20 6 18 32 37 39 66 74 76 89

Heapsort Sorting 29 26 28 20 6 18 32 37 39 66 74 76 89

Heapsort Sorting 18 26 28 20 6 29 32 37 39 66 74 76 89

Heapsort Sorting 28 26 18 20 6 29 32 37 39 66 74 76 89

Heapsort Sorting 28 26 18 20 6 29 32 37 39 66 74 76 89

Heapsort Sorting 6 26 18 20 28 29 32 37 39 66 74 76 89

Heapsort Sorting 26 6 18 20 28 29 32 37 39 66 74 76 89

Heapsort Sorting 26 20 18 6 28 29 32 37 39 66 74 76 89

Heapsort Sorting 26 20 18 6 28 29 32 37 39 66 74 76 89

Heapsort Sorting 6 20 18 26 28 29 32 37 39 66 74 76 89

Heapsort Sorting 20 6 18 26 28 29 32 37 39 66 74 76 89

Heapsort Sorting 20 6 18 26 28 29 32 37 39 66 74 76 89

Heapsort Sorting 18 6 20 26 28 29 32 37 39 66 74 76 89

Heapsort Sorting 18 6 20 26 28 29 32 37 39 66 74 76 89

Heapsort Sorting 6 18 20 26 28 29 32 37 39 66 74 76 89

Heapsort Sorting 6 18 20 26 28 29 32 37 39 66 74 76 89

Revising the Heapsort Algorithm Sorting If we implement the heap as an array each element removed will be placed at the end of the array, and the heap part of the array decreases by one element

Algorithm for In-Place Heapsort Sorting Build a heap by rearranging the elements in an unsorted array. 1.1 while n is less than table.length 1.2 Increment n by 1. This inserts a new item into the heap 1.3 Restore the heap property while the heap is not empty Remove the first item from the heap by swapping it with the last item in the heap and restoring the heap property

Analysis of Heapsort Sorting If we want to sort the sequence table bounded by the iterators first through last, we can consider the first item to be a heap of one item We now consider the general case where the items in the sequence from table[first] through table[first + n - 1] form a heap; Items from table[first + n] through table[last – 1] are not in the heap. As each item is inserted, we must “reheap” to restore the heap property. Because a heap of size n is a complete binary tree, it has log n levels Building a heap requires finding the correct location for an item in a heap with log n levels Each insert (or remove) is O(log n) With n items, building a heap is O(n log n) No extra storage is needed.

How many exchanges were required? How many comparisons? Sort the following integer array in ascending order using heap sort. Show each step. 1 2 3 4 5 6 8 9 Heap Sort Build(1): 1 Build(2): 5 1 Build(3): 6 1 5 Build(4): 6 4 5 1 Build(5): 8 6 5 1 4 Build(6): 9 6 8 1 4 5 Heapify(1): 5 6 8 1 4 9 Heapify(2): 8 6 5 1 4 9 Heapify(3): 4 6 5 1 8 9 Heapify(4): 6 4 5 1 8 9 Heapify(5): 1 4 5 6 8 9 Heapify(6): 5 4 1 6 8 9 Heapify(7): 1 4 5 6 8 9 Heapify(8): 4 1 5 6 8 9 Heapify(9): 1 4 5 6 8 9 Heapify(10): 1 4 5 6 8 9 How many exchanges were required? How many comparisons?

Code for Heapsort Sorting #ifndef HEAPSORT_H_ #define HEAPSORT_H_ #include <algorithm> /** Sort data in the specified range using heapsort. */ template<typename RI> void heap_sort(RI first, RI last) { build_heap(first, last); shrink_heap(first, last); } /** build_heap transforms the sequence into a heap. */ void build_heap(RI first, RI last) int n = 1; // Invariant: table[first] through table[first + n - 1] is a heap. while (n < (last - first)) ++n; // Add a new item to the heap and reheap. RI child = first + n - 1; RI parent = first + (child - first - 1) / 2; // Find parent. while ((parent >= first) && (*parent < *child)) std::iter_swap(parent, child); // Exchange elements. child = parent; parent = first + (child - first - 1) / 2;

Code for Heapsort Sorting /** shrink_heap transforms a heap into a sorted sequence. */ template<typename RI> void shrink_heap(RI first, RI last) { RI n = last; /* Invariant: [first] - [first + n - 1] is a heap. [first + n] - [last - 1] is sorted. */ while (n != first) --n; std::iter_swap(first, n); // swap top and last of heap RI parent = first; while (true) int temp = 2 * (parent - first) + 1; if (temp >= (n - first)) break; // No more children. RI left_child = first + temp; RI right_child = left_child + 1; // Find the larger of the two children. RI max_child = left_child; if ((right_child < n) && (*left_child < *right_child)) max_child = right_child; if (*parent < *max_child) // If the parent is smaller than the larger child std::iter_swap(parent, max_child); // Swap the parent and child. parent = max_child; // Continue at the child level. } else break; // Heap property is restored. Exit the loop. #endif

10.9, pgs. 604-614 10.9 Quicksort Algorithm for Quicksort Code for Quicksort Algorithm for Partitioning Code for partition A Revised Partition Algorithm Code for Revised partition Function 10.9, pgs. 604-614

Quicksort Developed in 1962. Sorting Developed in 1962. Quicksort selects a specific value called a pivot and rearranges the array into two parts (called partitioning). All the elements in the left subarray are less than or equal to the pivot. All the elements in the right subarray are larger than the pivot. The pivot is placed between the two subarrays. The process is repeated until the array is sorted.

Quicksort Example Sorting 44 75 23 43 55 12 64 77 33

Arbitrarily select the first element as the pivot Quicksort Example Sorting 44 75 23 43 55 12 64 77 33 Arbitrarily select the first element as the pivot

Swap the pivot with the element in the middle Quicksort Example Sorting 55 75 23 43 44 12 64 77 33 Swap the pivot with the element in the middle

Quicksort Example Sorting 55 75 23 43 44 12 64 77 33 Partition the elements so that all values less than or equal to the pivot are to the left, and all values greater than the pivot are to the right

Trace of Quicksort Sorting 12 33 23 43 44 55 64 77 75 Partition the elements so that all values less than or equal to the pivot are to the left, and all values greater than the pivot are to the right

44 is now in its correct position Quicksort Example Sorting 44 is now in its correct position 12 33 23 43 44 55 64 77 75

Now apply quicksort recursively to the two subarrays Quicksort Example Sorting 12 33 23 43 44 55 64 77 75 Now apply quicksort recursively to the two subarrays

Quicksort Example Sorting Pivot value = 12 12 33 23 43 44 55 64 77 75

Quicksort Example Sorting Pivot value = 12 12 33 23 43 44 55 64 77 75

Quicksort Example Sorting Pivot value = 33 12 33 23 43 44 55 64 77 75

Quicksort Example Sorting Pivot value = 33 12 23 33 43 44 55 64 77 75

Quicksort Example Sorting Pivot value = 33 12 23 33 43 44 55 64 77 75

Left and right subarrays have single values; they are sorted Quicksort Example Sorting Pivot value = 33 12 23 33 43 44 55 64 77 75 Left and right subarrays have single values; they are sorted

Left and right subarrays have single values; they are sorted Quicksort Example Sorting Pivot value = 33 12 23 33 43 44 55 64 77 75 Left and right subarrays have single values; they are sorted

Quicksort Example Sorting Pivot value = 55 12 23 33 43 44 55 64 77 75

Quicksort Example Sorting Pivot value = 64 12 23 33 43 44 55 64 77 75

Quicksort Example Sorting Pivot value = 77 12 23 33 43 44 55 64 77 75

Quicksort Example Sorting Pivot value = 77 12 23 33 43 44 55 64 75 77

Quicksort Example Sorting Pivot value = 77 12 23 33 43 44 55 64 75 77

Left subarray has single value; it is sorted Quicksort Example Sorting 12 23 33 43 44 55 64 75 77 Left subarray has single value; it is sorted

Quicksort Example Sorting 12 23 33 43 44 55 64 75 77

Algorithm for Quicksort Sorting We describe how to do the partitioning later… The iterators first and last are the end points of the array being sorted. pivot is a pointer to the pivot value after partitioning. Algorithm for Quicksort if the sequence has more than one element: Partition the elements in the sequence (from first through last) so that the pivot value is in its correct place and is pointed to by pivot. Recursively apply quicksort to the sequence in the iterator range first through pivot. Recursively apply quicksort to the sequence in the iterator range pivot + 1 through last

Analysis of Quicksort Sorting If the pivot value is a random value selected from the current sequence, then statistically, half of the items in the subarray will be less than the pivot and half will be greater. If both subarrays have the same number of elements (best case), there will be log n levels of recursion. At each recursion level, the partitioning process involves moving an element to its correct position—n moves. Quicksort is O(n log n), just like merge sort. A quicksort will give very poor behavior if, each time the array is partitioned, a subarray is empty. In that case, the sort will be O(n2). Further, the overhead of recursive calls and the extra run-time stack storage required by these calls makes this version of quicksort a poor performer relative to the quadratic sorts.

Quicksort /** Sort data in the specified range using quicksort. Sorting /** Sort data in the specified range using quicksort. @param RI A random-access iterator. @param first - references first element in the sequence to be sorted @param last - references 1 past the end of the sequence */ template<typename RI> void quick_sort(RI first, RI last) { if (last - first > 1) // Partition the table. RI pivot = partition(first, last); // Sort the left half. quick_sort(first, pivot); // Sort the right half. quick_sort(pivot + 1, last); }

Quicksort Partitioning Sorting 44 75 23 43 55 12 64 77 33 If the array is randomly ordered, it does not matter which element is the pivot. For simplicity we pick the element with subscript first

Quicksort Partitioning Sorting 44 75 23 43 55 12 64 77 33 If the array is randomly ordered, it does not matter which element is the pivot. For simplicity we pick the element with subscript first

Quicksort Partitioning Sorting 44 75 23 43 55 12 64 77 33 For visualization purposes, items less than or equal to the pivot will be colored blue; items greater than the pivot will be colored light purple

Quicksort Partitioning Sorting 44 75 23 43 55 12 64 77 33 For visualization purposes, items less than or equal to the pivot will be colored blue; items greater than the pivot will be colored light purple

Quicksort Partitioning Sorting 44 75 23 43 55 12 64 77 33 Search for the first value at the left end of the array that is greater than the pivot value

Quicksort Partitioning Sorting up 44 75 23 43 55 12 64 77 33 Search for the first value at the left end of the array that is greater than the pivot value

Quicksort Partitioning Sorting up 44 75 23 43 55 12 64 77 33 Then search for the first value at the right end of the array that is less than or equal to the pivot value

Quicksort Partitioning Sorting up down 44 75 23 43 55 12 64 77 33 Then search for the first value at the right end of the array that is less than or equal to the pivot value

Quicksort Partitioning Sorting up down 44 75 23 43 55 12 64 77 33 Exchange these values

Quicksort Partitioning Sorting 44 33 23 43 55 12 64 77 75 Exchange these values

Quicksort Partitioning Sorting 44 33 23 43 55 12 64 77 75 Repeat

Quicksort Partitioning Sorting up 44 33 23 43 55 12 64 77 75 Find first value at left end greater than pivot

Quicksort Partitioning Sorting up down 44 33 23 43 55 12 64 77 75 Find first value at right end less than or equal to pivot

Quicksort Partitioning Sorting 44 33 23 43 12 55 64 77 75 Exchange

Quicksort Partitioning Sorting 44 33 23 43 12 55 64 77 75 Repeat

Quicksort Partitioning Sorting up 44 33 23 43 12 55 64 77 75 Find first element at left end greater than pivot

Quicksort Partitioning Sorting down up 44 33 23 43 12 55 64 77 75 Find first element at right end less than or equal to pivot

Quicksort Partitioning Sorting down up 44 33 23 43 12 55 64 77 75 Since down has "passed" up, do not exchange

Quicksort Partitioning Sorting down up 44 33 23 43 12 55 64 77 75 Exchange the pivot value with the value at down

Quicksort Partitioning Sorting down 12 33 23 43 44 55 64 77 75 Exchange the pivot value with the value at down

Quicksort Partitioning Sorting down 12 33 23 43 44 55 64 77 75 The pivot value is in the correct position; return the value of down and assign it to the iterator pivot

Algorithm for partition Function Sorting Define the pivot value as the contents of table[first]. Initialize up to first + 1 and down to last - 1. do Increment up until up selects the first element greater than the pivot value or up has reached last - 1. Decrement down until down selects the first element less than or equal to the pivot value or down has reached first. if up < down then Exchange table[up] and table[down]. while up is to the left of down Exchange table[first] and table[down]. Return the value of down to pivot.

Algorithm for partition Function Sorting

Code for partition Sorting /** Partition the table so that values in the iterator range first through pivot are less than or equal to the pivot value, and values in the iterator range pivot + 1 through last are greater than the pivot value. @param RI An iterator that meets the random-access iterator requirements @param first An iterator that references the first element in the sequence to be sorted @param last An iterator that references 1 past the end of the sequence @return The position of the pivot value (originally at first) */ template<typename RI> RI partition(RI first, RI last) { // Start up and down at either end of the sequence. (1st table element is the pivot value.) RI up = first + 1; RI down = last - 1; do /* Invariant: All items in T[first] through T[up - 1] <= T[first] All items in T[down + 1] through T[last - 1] > T[first] */ while ((up != last - 1) && !(*first < *up)) ++up; while (*first < *down) --down; // Assert: up equals last-1 or T[up] > T[first]. if (up < down) // Assert: down equals first or T[down] <= T[first]. std::iter_swap(up, down); // if up is to the left of down, exchange T[up] and T[down]. } } while (up < down); // Repeat while up is left of down. std::iter_swap(first, down); // Put the pivot value where it belongs. return down; // Return position of pivot.

How many exchanges were required? How many comparisons? Sort the following integer array in ascending order using quicksort. Show each step. 1 2 3 4 5 6 7 8 9 10 50 25 80 35 60 90 70 40 12 85 How many exchanges were required? How many comparisons?

10.9, pgs. 604-614 10.9 Quicksort Algorithm for Quicksort Code for Quicksort Algorithm for Partitioning Code for partition A Revised Partition Algorithm Code for Revised partition Function 10.9, pgs. 604-614

A Revised Partition Algorithm Sorting Quicksort is O(n2) when each split yields one empty subarray, which is the case when the array is presorted. The worst possible performance occurs for a sorted array, which is not very desirable. A better solution is to pick the pivot value in a way that is less likely to lead to a bad split. Use three references: first, middle, last. Select the median of the these items as the pivot. Revised Partition Algorithm 1. Sort table[first], table[middle], and table[last-1]. 2. Move median value to first position (pivot value) by exchanging table[first] and table[middle]. 3. Initialize up to first + 1 and down to last - 1. …

Finding a Pivot Sorting 1 2 3 4 5 6 7 8 9 10 50 25 80 35 60 90 70 40 12 85 Up Pivot Up Down Down 50 25 12 35 60 90 70 40 80 85 Pivot Up Down 50 25 12 35 40 90 70 60 80 85 Pivot Down Up 40 25 12 35 50 90 70 60 80 85 Pivot

A Revised Partition Algorithm Sorting

middle = (last – first) / 2 Median of Three Sorting first last middle 44 75 23 43 55 12 64 77 33 middle = (last – first) / 2 = (8 – 0) / 2 = 4

Median of Three first middle last 33 75 23 43 44 12 64 77 55 Sorting first middle last 33 75 23 43 44 12 64 77 55 Sort these values

Exchange middle and first Revised Partitioning Sorting first middle 44 75 23 43 33 12 64 77 55 Exchange middle and first

Run the partition algorithm using the first element as the pivot Revised Partitioning Sorting up down 44 75 23 43 33 12 64 77 55 Run the partition algorithm using the first element as the pivot

Revised Partitioning up down 44 75 23 43 33 12 64 77 55 Sorting up down 44 75 23 43 33 12 64 77 55 up moves right until > 44 down move left until <= 44

continue until down <= up Revised Partitioning Sorting up down 44 12 23 43 33 75 64 77 55 then exchange and continue until down <= up

Revised Partitioning down up 44 12 23 43 33 75 64 77 55 Sorting down up 44 12 23 43 33 75 64 77 55 When down and up cross…

Exchange first with down Revised Partitioning Sorting down up 33 12 23 43 44 75 64 77 55 Exchange first with down down becomes pivot

Code for Revised partition Sorting /** Partition the table so that left values are less than pivot and right are greater then or equal to pivot. @return The position of the pivot value (originally at first) */ template<typename RI> RI partition(RI first, RI last) { /* Put the median of table[first], table[middle], table[last - 1] into table[first], and use this value as the pivot. */ bubble_sort3(first, last); // Swap first element with middle element. std::iter_swap(first, first + (last - first) / 2); // Start up and down at either end of the sequence with first as pivot RI up = first + 1; RI down = last - 1; do /* Invariant: All items in table[first] through table[up - 1] <= table[first] All items in table[down + 1] through table[last - 1] > table[first] */ while ((up != last - 1) && !(*first < *up)) ++up; // Assert: up equals last - 1 or table[up] > table[first]. while (*first < *down) --down; // Assert: down equals first or table[down] <= table[first]. // if up is to the left of down, exchange table[up] and table[down]. if (up < down) std::iter_swap(up, down); } while (up < down); // Repeat while up is left of down. // Exchange table[first] and table[down] thus putting the pivot value where it belongs. std::iter_swap(first, down); return down; // Return position of pivot. }

How many exchanges were required? How many comparisons? Sort the following integer array in ascending order using revised Quicksort. Show each step. 1 2 3 4 5 6 7 8 44 75 23 43 55 12 64 77 33 How many exchanges were required? How many comparisons?

Lab 10 - Quicksort

Lab 10 - Quicksort Sorting "Quicksort is a sorting algorithm developed by Tony Hoare that, on average, makes O(n log n) comparisons to sort n items. It is also known as partition-exchange sort. In the worst case, Quicksort makes O(n2) comparisons, though this behavior is rare. Quicksort is typically faster in practice than other O(n log n) algorithms. Additionally, quicksort's sequential and localized memory references work well with a cache. Quicksort can be implemented as an in-place partitioning algorithm."

Quicksort Commands Recursion COMMAND DESCRIPTION OUTPUT QuickSort <capacity> Dynamically allocate a QuickSort array of size capacity. Set current number of elements (Size) to 0. OK Error AddToArray <data1> <data2>... Add data element(s) to QuickSort array. Duplicates are allowed. Dynamically increase array size as needed (double array capacity for each increase.) OK Error Capacity Return the size of the QuickSort array. size Clear Delete all nodes from the QuickSort array. OK Size Return the number of elements currently in the QuickSort array. elements Sort <left> <right> QuickSort the elements in the QuickSort array from index <left> to index <right> (where <right> is one past last element) using median and partition functions. OK Error SortAll QuickSort all the elements in the QuickSort array using median and partition functions. OK Error MedianOfThree <left> <right> 1) Calculate the middle index (middle = (left + right)/2), then 2) bubble-sort the values at the left, middle, and right indices. (<right> is one past last element.) Pivot Index -1 error Partition <left> <right> <pivot> Using the revised pivot algorithm, partition the QuickSort array (<left>, <right> and <pivot> indexes) around the pivot value. Values smaller than the pivot should be placed to the left of the pivot while values larger than the pivot should be placed to the right of the pivot. (<right> is one past last element.) Pivot Index -1 error PrintArray Print the contents of the QuickSort array as comma separated values (using a friend insertion (<<) operator and toString() function.) Array values. Empty Stats (Bonus) Output the number of comparisons and exchanges used by the SortAll command. Comparisons,Exchanges

Quicksort Example QuickSort 2 AddToArray 1 6 5 4 2 8 PrintArray Sorting QuickSort 2 AddToArray 1 6 5 4 2 8 PrintArray Capacity Size Sort 0 3 AddToArray 6 -5 -1 -3 -2 -4 MedianOfThree 0 12 Partition 0 12 4 SortAll Stats Clear QuickSort 2 OK AddToArray 1,6,5,4,2,8 OK PrintArray 1,6,5,4,2,8 Capacity 8 Size 6 Sort 0,3 OK PrintArray 1,5,6,4,2,8 AddToArray 6,-5,-1,-3,-2,-4 OK PrintArray 1,5,6,4,2,8,6,-5,-1,-3,-2,-4 MedianOfThree 0,12 = 6 PrintArray -4,5,6,4,2,8,1,-5,-1,-3,-2,6 Partition 0,12,4 = 6 PrintArray 1,-2,-3,-1,-4,-5,2,8,4,6,5,6 Size 12 SortAll OK PrintArray -5,-4,-3,-2,-1,1,2,4,5,6,6,8 Stats 53,22 Clear OK PrintArray Empty

Steps to Success Step 1 - Begin with a main function. Sorting Step 1 - Begin with a main function. As with most labs this semester, you will need to write your own main function. The main needs to be able to parse a given input file. Step 2 - Begin the QuickSort class. The templated QuickSort class is derived from the abstract template interface class QSInterface. Your QuickSort class should contain a dynamically-allocated array. Before you focus too much on the actual sorting algorithm, make sure the logistics of the class work correctly. Focus on createArray(), addToArray(), clear(), getSize(), and toString() member functions. Step 3 - Write your medianOfThree() function in the QuickSort class. The Median of Three function should take a left index and right index as parameters. It should then calculate the index in the middle of the left and right indexes (rounding down if necessary). Sort the left, middle, and right numbers from smallest to largest in the array. Finally, return the index of the middle value. This will be your pivot value in the sortAll() function. Note that medianOfThree() is not used in your partition() function. The pivot value will be supplied by the input files.

Steps to Success Sorting Step 4 - Write your partition() function in the QuickSort class. The partition() function should begin by swapping the leftmost element from the array with whatever value is contained at the pivot index. Now, follow the revised pivot algorithm presented in the textbook (pg. 611) to arrange the array such that all elements lower than the given pivot are at its left and all elements higher are at its right. The partition() function should return the location of the pivot index. Step 5 - Write the sortAll() function, using your medianOfThree() and partition() functions. Note that this function will be recursive. Most people use sortAll() as a recursive starter function that then calls another function to do the sorting. To sort, you should first call your medianOfThree() function and sort the first, middle, and last elements. This function returns the pivot index. Now, call your partition() function, using the number returned from medianOfThree() as the pivot index. This function will return a pivot index, or the index where your array is split. Finally, you will recursively call your sort() function on two halves of your array, with one half from the left until the pivot, and the other half from the pivot to the right.

Requirements Trees Points Requirement (40 Points) 5 argv[1] and argv[2] used for input / output streams respectively. No execution user interaction (i.e. system("pause"); or getchr();). Your template QuickSort class contains a dynamically-allocated element array and implements the abstract interface QSInterface class. The QuickSort command deletes the current array (if any) and then dynamically allocates a new element array. The Capacity and Clear commands execute as described above. No STL container is used anywhere in your QuickSort class. (lab10_in_01.txt). Items are added to the QuickSort array using the AddToArray command. Duplicate/multiple items can be added with one command. The QuickSort array dynamically grows (by doubling) as the number of items added exceeds the current array capacity. The PrintArray command outputs a comma-separated string representation of the array using an insertion (<<) friend operator and the toString() method. The Size command outputs the number of elements in the QuickSort array. (lab10_in_02.txt). Your MedianOfThree command (medianOfThree() function) properly sorts the first, middle, and last elements (as described above.) (lab10_in_03.txt). Your Partition command (partition() function) arranges the array such that all elements less than the given pivot are to the left and all elements greater than the given pivot are to the right (using the revised pivot algorithm.) (lab10_in_04.txt). The SortAll command (sortAll() function) utilizes your medianOfThree() and partition() functions to recursively sort the entire array. (lab10_in_05.txt). The Sort command (sort() function) utilizes your medianOfThree() and partition() functions to recursively sort a QuickSort subarray. (lab10_in_06.txt). BONUS: The Stats command outputs the number of comparisons and exchanges used by the sort commands (Sort and SortAll.) (lab10_in_07.txt). VS_MEM_CHECK macro is included in main to detect memory leaks. No Memory leaks are reported.

10.9 Quicksort