Implement the Quicksort Algorithm - Implement the Quicksort Algorithm

Tell us what’s happening:

Can someone explain how should I go forward with the problem because I’m not sure if I’m on the right track or my recursion function works properly???

Your code so far

def quick_sort(input_list):
    if len(input_list) < 1:
        return []

    pivot_value = input_list[0]
    less_than_pivot = []
    equal_to_pivot = []
    greater_than_pivot = [] 

    for i in range(len(input_list)):
        if input_list[i] < pivot_value:
            less_than_pivot.append(input_list[i])
        elif input_list[i] == pivot_value:
            equal_to_pivot.append(input_list[i])
        else:
            greater_than_pivot.append(input_list[i])



if __name__ == "__main__":
    input_list = [20, 3, 14, 1, 5]
    print(input_list)
    print(quick_sort(input_list))
    
        



    

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/156.0.0.0 Safari/537.36

Challenge Information:

Implement the Quicksort Algorithm - Implement the Quicksort Algorithm

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-quicksort/681dbcca65bbda5f286dc0ca.md at main · freeCodeCamp/freeCodeCamp · GitHub

Hi @ashley.varghese11

You need to check if the three sub lists can combine into one sorted list.

If not, then call recursively call the function.

Happy coding

Sorry, I don’t think I understand, what do you mean by “check if the three sub lists can combine into one sorted list.” Is it possible if you can give an example?

After the for loop splits the original list into three lists, maybe check if the recombined list is sorted. If it isn’t then call the quick_sort function with the unsorted list.

Happy coding

They’re saying you need to check if the result of the looped-over ‘if/elif/else’ chain is sorted, otherwise the function needs to be called again, to continue the sorting.

Right now, if I’m understanding your code correctly, the sorting only happens once. So with that array example of [20, 3, 14, 1, 5] that is in your code, the results should look something like
less_than_pivot: [3, 14, 1, 5]
equal_to_pivot: [20]
greater_than_pivot: []

If you were to concatenate those three resulting arrays together, it would be [3, 14, 1, 5, 20], which is not a fully sorted array (this is what you need to determine in your code), and so the sorting method should be called again until it is fully sorted.

I combined all three sublists and got [3, 14, 1, 5, 20] as my result, so I called the quick_sort method on the combined list. But when I do that I get an error on the terminal saying: [Previous line repeated 993 more times]
File “main.py”, line 10, in quick_sort
RecursionError: maximum recursion depth exceeded in comparison.

Hi @ashley.varghese11

When the combined list is sorted you no longer need to recursively call quick_sort.

Happy coding

Okay but my combined list isn’t sorted, it returns [3, 14, 1, 5, 20]. Do I need to change my loop logic?? How would I use recursion in this case?

Nevermind I managed to figure it out.