FAQ: Quicksort: Python - Quicksort Introduction

This community-built FAQ covers the “Quicksort Introduction” 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 Quicksort Introduction

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!

Why doesnt my version work, they are the same?

# Define your quicksort function
def quicksort_my(list, start, end):
  print(start, end)
  if (start >= end):
    return
  print(list[start])
  quicksort_my(list, start+1,end)
  
colors = ["blue", "red", "green", "purple", "orange"]
quicksort_my(colors,0,4)

def quicksort_1(list, start, end):
  if start >= end:
    return

  print(list[start])
  start += 1
  quicksort_1(list, start, end)

colors = ["blue", "red", "green", "purple", "orange"]
quicksort_1(colors, 0, len(colors) - 1)

I dont get why quicksort_my fails on the codeacademy IDE and the quicksort_1 doesnt…

1 Like

In the “_1” version, the third argument in the calling statement depends upon the length of the input list. In the “_my” version, the third argument is a number, 4. The grader probably is testing on lists of varying length, not just the one you see.

1 Like

That makes sense, I just didn’t understand the error message that came with the failing test because I was still printing the list elements in the correct order. Thanks for getting back to me!

1 Like

start_of_sub_list = 1
end_of_sub_list = len(my_list) - 2

my_list[start_of_sub_list : end_of_sub_list]

[‘burrito’, ‘sandwich’, ‘salad’]

Shouldn’t it be :end_of_sub_list = len(my_list) - 1
This will give an index value of 4, and then include the middle three elements?

2 Likes

From the exercise:

[“race cars”, “lasers”, “airplanes”]

start = 1
end = 1
start == end
A sub-list starting at index 1 and concluding at index 1
[“lasers”]
This would actually return [] not [“lasers”]

start = 2
end = 1
start > end
A sub-list start index 2 and concluding at index 1

list[1:1] would return and
list[3,4] ie when start is greater then end will also return

I think the correct check for the base case should have been:
start == end or start == end + 1
the exercise uses start>= end which also works but a different explanation should suffice instead of the one given

3 Likes

I was thinking the same thing as well. i think they made a mistake?

1 Like

list is a reserved keyword. Albeit it is asked to use it as a parameter and do not have significant impact, it is not a good habit to take to use reserved keyword like that!

3 Likes

Not sure if this will be cleared in the future if we go more in depth with pointers, but the base case for our function ( if start >= end )seems problematic:
if start = 1 and end = 1 list[1:1] will be []
if start = 0 and end = -1 list[0:-1] can have any length, not limited to “one or zero elements”

1 Like

Thank you for the interesting question! I did not pay attention why not “if len(list) >= 0”, as usual…

1 Like

Isn’t it bad that we are using list, which is a built-in python function, as a variable name?

2 Likes

When does start > end actually occur?

Not sure if they read the comments on these forums. As a few others pointed out, list is a reserved keyword in Python and should not be used in code because it can produce unexpected outcomes. This is basic. Being an educational platform, they should fix it ASAP. People are already getting the wrong idea.