FAQs on the exercise Partitioning Part III - Swapping
There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
Since the outer ‘while’ condition is same as the ‘if’ condition, I don’t see any point in using the extra ‘if’ condition. Correct me if I am wrong and why do we need this ‘if’ condition?
I believe it’s because the while loops and “if” condition are all performed sequentially.
If we picture the code working, first the leftIndex’s are checked while array[leftIndex] < pivot. If the array[leftIndex] > pivot, the first loop stops and the 2nd while loop begins checking the elements at rightIndex. If array[rightIndex] < pivot, that while loop also breaks.
Thus, the last “if” condition is in case both of these while loops have been exited without checking all the elements in the array.
If such is the case, we know that:
array[leftIndex] > pivot
AND
array[leftIndex] < pivot
We can therefore assume with certainty that if we swap the elements at leftIndex and rightIndex, it will then re-satisfy our initial while loops and the process continues until eventually the outer while loop breaks at which point all elements will have been checked.
This is my best guess as to how it works–I definitely think the lesson could have been explained a bit better.
But I believe the clue is in the “partitioning Invariant”. On google it is found as “Loop Invariant”.
Example: If I run a for loop in JavaScript, and set a variable initially as maxValue. I assign the maxValue the first element of the array: arr[0]. I also assume that this element is the largest in the array (my loop variant). That will be my loop invariant. Whenever the for loop, while iterating the array, will find an element greater than the maxValue, it will update it. This will maintain the Loop Variant before the start of the loop, at the start of each iteration and after the end of the loop.
Since, in programming, loops can be tricky, they become infinite or terminate without accomplishing a desired goal, an invariant will help achieve the goal.
In the QuickSort code exercise, our partitioning invariant is that the “elements on the left side of the array are supposed to be less than the pivot and the elements on the right side are to be greater than the pivot”. Here is when the left and right indexes come into play. They act as markers, or boundary markers (not a programming term, I made it up). The left index is supposed to be less than the right index.
Here is a story I am playing in my head: When the left index enters the boundary of the right index, there it will find elements greater than the pivot and vice versa for the right index. When left index is greater than the right index it means the partition is complete. But, when the left index is less than the right index and on top of that, it has found an element greater than the pivot, then this will have violated the partitioning invariant and that is when the elements need to be swapped. Hence the if condition:
if(leftIndex <= rightIndex)
I hope this helps future learners and I am sorry to revive this topic again. I couldn’t find partitioning invariant notes when I googled it (partitioning invariant +codecademy). Hopefully this will now make up for it. So long as I have not said anything wrong.