How would you modify \(\textsc{Quicksort}\) to sort into nonincreasing order?
To sort in nonincreasing (descending) order instead of nondecreasing (ascending) order, we need to change how \(\textsc{Partition}\) divides elements around the pivot. The key is to reverse the comparison in the partitioning step.
The standard \(\textsc{Partition}\) places elements \(\leq pivot\) on the left and elements \(> pivot\) on the right. For nonincreasing order, we want elements \(\geq pivot\) on the left and elements \(< pivot\) on the right.
This requires changing only one character in the \(\textsc{Partition}\) procedure:
The \(\textsc{Quicksort}\) procedure itself remains completely unchanged:
For example, with the array \(\langle 2, 8, 7, 1, 3, 5, 6, 4 \rangle\) and pivot 4, the modified partition places elements \(\{8, 7, 5, 6\}\) (those \(\geq 4\)) on the left and \(\{2, 1, 3\}\) (those \(< 4\)) on the right, with 4 in between. Recursive sorting then produces the final nonincreasing sequence \(\langle 8, 7, 6, 5, 4, 3, 2, 1 \rangle\).