Skip to main content
Logo image

Section 22.8 Debugging QuickSort

Both programs below are supposed to implement QuickSort, but each has a bug that keeps it from correctly sorting the entire list. Run each one, compare the “Sorted array” output to what it should be, and find the mistake.

Activity 22.3. Debug 1.

This version doesn’t fully sort the array. What is missing?

admin.....open in new window

Hint.
Look closely at how many times quickSort() calls itself inside its own body. A correct divide-and-conquer sort needs to recurse on both sides of the pivot.

Activity 22.4. Debug 2.

This version also fails to sort correctly, for a different reason. What is wrong this time?

admin.....open in new window

Hint.
Compare the arguments passed to the two recursive calls to quickSort() here with the version from the previous section. Which array elements are being excluded from further sorting that shouldn’t be?