Presentation is loading. Please wait.

Presentation is loading. Please wait.

10.8 Heapsort 10.9 Quicksort Chapter 10 - Sorting.

Similar presentations


Presentation on theme: "10.8 Heapsort 10.9 Quicksort Chapter 10 - Sorting."— Presentation transcript:

1 10.8 Heapsort 10.9 Quicksort Chapter 10 - Sorting

2 Attendance Quiz #36 Sorting

3 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());

4 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

5 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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

51 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

52 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

53 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.

54 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): Build(5): Build(6): Heapify(1): Heapify(2): Heapify(3): Heapify(4): Heapify(5): Heapify(6): Heapify(7): Heapify(8): Heapify(9): Heapify(10): How many exchanges were required? How many comparisons?

55 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;

56 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

57 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

58 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.

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

60 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

61 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

62 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

63 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

64 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

65 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

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

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

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

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

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

71 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

72 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

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

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

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

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

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

78 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

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

80 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

81 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.

82 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); }

83 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

84 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

85 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

86 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

87 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

88 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

89 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

90 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

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

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

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

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

95 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

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

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

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

99 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

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

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

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

103 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

104 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.

105 Algorithm for partition Function
Sorting

106 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.

107 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?

108

109 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

110 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.

111 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

112 A Revised Partition Algorithm
Sorting

113 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

114 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

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

116 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

117 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

118 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

119 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…

120 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

121 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. }

122 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?

123

124 Lab 10 - Quicksort

125 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."

126 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

127 Quicksort Example QuickSort 2 AddToArray 1 6 5 4 2 8 PrintArray
Sorting QuickSort 2 AddToArray PrintArray Capacity Size Sort 0 3 AddToArray MedianOfThree 0 12 Partition 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

128 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.

129 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.

130 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.

131 10.9 Quicksort


Download ppt "10.8 Heapsort 10.9 Quicksort Chapter 10 - Sorting."

Similar presentations


Ads by Google