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.

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

1 Like

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.

So I ran into a problem when I did as follows.

print(quicksort(unsorted_list, 0, len(unsorted_list)-1))

The result was none obviously(!) because the function doesn’t return anything.
To get it printed,

quicksort(unsorted_list, 0, len(unsorted_list)-1))
print(unsorted_list)

Which is a bit awkward.

Probably it’s best to return list itself at the end of the function, in my opinion.
And it would have been helpful if the instruction was more clear about this.

If you return a result then you probably shouldn’t modify the original. Do one or the other.

Yeah. In addition to using a reserved word as a variable this lesson is not done very well overall.

1 Like

It would be simpler if the variables could just be defined within the function, without needing to have parameters for start and end. But I guess one of the quirks of recursive functions is that you can’t define variables that carry across recursion levels any other way.

Maybe it could be refactored to use only list pointers, without relying on variables for start and end? I don’t know if that could work.

There is a problem with the pointers. lesser_than_sub_list is actually [1], but in the learn section it shows as [1, 2]. And it really makes the subject much more difficult to understand. There are more problems like this (making sub-lists with indices) in this Quicksort Module.

Dude this quick sort algorithm has been the most confusing section for me in this whole Computer Science Path. I didn’t even get a heck of it in the Quicksort: Python Part. I advice that you might learn through some external source like youtube or you may google search it. I completely agree with you :+1: :+1: