FAQ: Quicksort: Python - Recurse, Rinse, Repeat

This community-built FAQ covers the “Recurse, Rinse, Repeat” exercise from the lesson “Quicksort: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Sorting Algorithms

FAQs on the exercise Recurse, Rinse, Repeat

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 (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 (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


The code from the above lesson:

greater_than_sub_list = whole_list[right_sub_list_start : right_sub_list_end]

Isn’t the varible right_sub_list_end == end == len(whole_list) - 1 == 5?
If so, the greater_than_sub_list = while_list[3 : 5], shouldn’t it be [4, 6]???

Am I wrong? Or in order to include the last element, the larger than list should be:

greater_than_sub_list = whole_list[right_sub_list_start : right_sub_list_end + 1]

or just:

greater_than_sub_list = whole_list[right_sub_list_start :]

if there is no recursion need.

2 Likes

This is the exact same issue I am having. I think it’s an error on the instructor’s part because the index to the right of the : operator is exclusive. It doesn’t include the last element. The same issue can be seen when they initialize lesser_than_sub_list. They initialize it to whole_list[left_sub_list_start : left_sub_list_end], however this doesn’t give you [2, 1] it just gives you the singleton list [2] because left_sub_list_end is initialized to lesser_than_pointer - 1 which is equal to 1. Therefore, when writing whole_list[left_sub_list_start : left_sub_list_end], it’s the same as writing whole_list[0 : 1] (left_sub_list_start == 0 btw), this will just return a list of the zero-th element [2] and NOT [2, 1]. I think they need to edit the lesson to account for this error.

2 Likes

Agree with this! Just ran into the same issue.

I am having an issue testing this quicksort function.

When I print sorted_list, and hit run several times, the list seems to change.

But I got over this and removed shuffle. And the result was correct , only when I used quicksort with an already sorted array.

This thing I figured out when I defined a new array for testing, which wasn’t sorted. In this case, quicksort seems to return different arrays, when I hit run sevaral times.

Example:

First run:

[3, 7, 12, 24, 36, 42]
[8, 15, 3, 17, 10]

[3, 7, 12, 24, 36, 42]
[3, 8, 10, 17, 15]

Second run:

[3, 7, 12, 24, 36, 42]
[8, 15, 3, 17, 10]

[3, 7, 12, 24, 36, 42]
[3, 8, 10, 15, 17]

And so on; only the array which was initially unsorted changes.

I am feeling confused :expressionless:

Everything was right, I was wrong. I needed to print the initial array at the end.

At this step of the program, the initial array is sorted at the end and doesn’t change, if we run the program many times. This is because the partition is complete.

Just ran into the same problem, glad i’m not going crazy, I agree with the previous reply, need to alter the lesson. I think this is a common human mistake, it can trip you up if you’re not parsing the example code carefully or simply don’t know that the last pointer is exclusive, but if you keep an eye out for it and report it when you see it I don’t think it will be much of a hinderance.