# Implement the Quicksort Algorithm - Implement the Quicksort Algorithm

**URL:** <https://forum.freecodecamp.org/t/implement-the-quicksort-algorithm-implement-the-quicksort-algorithm/802712>\
**Category:** Curriculum Help\
**Tags:** python\
**Created:** [October 25, 2026, 2:45am UTC](https://forum.freecodecamp.org/t/implement-the-quicksort-algorithm-implement-the-quicksort-algorithm/802712 "2026-10-25T02:45:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashley.varghese11](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ashley.varghese11/32/420476_2.png) [@ashley.varghese11](https://forum.freecodecamp.org/u/ashley.varghese11)\
**Post date:** [October 25, 2026, 2:45am UTC](https://forum.freecodecamp.org/t/implement-the-quicksort-algorithm-implement-the-quicksort-algorithm/802712/1 "2026-10-25T02:45:19Z")

</div>

### 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

```py
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

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/python-v9/lab-quicksort/implement-the-quicksort-algorithm)**
>
> Learn to Code for Free

GitHub Link: [freeCodeCamp/curriculum/challenges/english/blocks/lab-quicksort/681dbcca65bbda5f286dc0ca.md at main · freeCodeCamp/freeCodeCamp · GitHub](https://github.com/freeCodeCamp/freeCodeCamp/blob/main/curriculum/challenges/english/blocks/lab-quicksort/681dbcca65bbda5f286dc0ca.md)

---

<div class="post-metadata">

**Author:** ![Teller](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/teller/32/412338_2.png) [@Teller](https://forum.freecodecamp.org/u/Teller)\
**Post date:** [October 25, 2026, 3:30am UTC](https://forum.freecodecamp.org/t/implement-the-quicksort-algorithm-implement-the-quicksort-algorithm/802712/2 "2026-10-25T03:30:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ashley.varghese11](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ashley.varghese11/32/420476_2.png) [@ashley.varghese11](https://forum.freecodecamp.org/u/ashley.varghese11)\
**Post date:** [October 26, 2026, 1:30am UTC](https://forum.freecodecamp.org/t/implement-the-quicksort-algorithm-implement-the-quicksort-algorithm/802712/3 "2026-10-26T01:30:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Teller](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/teller/32/412338_2.png) [@Teller](https://forum.freecodecamp.org/u/Teller)\
**Post date:** [October 26, 2026, 4:22am UTC](https://forum.freecodecamp.org/t/implement-the-quicksort-algorithm-implement-the-quicksort-algorithm/802712/4 "2026-10-26T04:22:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GammaGool](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gammagool/32/421620_2.png) [@GammaGool](https://forum.freecodecamp.org/u/GammaGool)\
**Post date:** [October 26, 2026, 11:03am UTC](https://forum.freecodecamp.org/t/implement-the-quicksort-algorithm-implement-the-quicksort-algorithm/802712/5 "2026-10-26T11:03:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ashley.varghese11](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ashley.varghese11/32/420476_2.png) [@ashley.varghese11](https://forum.freecodecamp.org/u/ashley.varghese11)\
**Post date:** [October 27, 2026, 2:22am UTC](https://forum.freecodecamp.org/t/implement-the-quicksort-algorithm-implement-the-quicksort-algorithm/802712/6 "2026-10-27T02:22:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Teller](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/teller/32/412338_2.png) [@Teller](https://forum.freecodecamp.org/u/Teller)\
**Post date:** [October 27, 2026, 2:34am UTC](https://forum.freecodecamp.org/t/implement-the-quicksort-algorithm-implement-the-quicksort-algorithm/802712/7 "2026-10-27T02:34:55Z")

</div>

Hi @ashley.varghese11

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

Happy coding

---

<div class="post-metadata">

**Author:** ![ashley.varghese11](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ashley.varghese11/32/420476_2.png) [@ashley.varghese11](https://forum.freecodecamp.org/u/ashley.varghese11)\
**Post date:** [October 27, 2026, 2:31pm UTC](https://forum.freecodecamp.org/t/implement-the-quicksort-algorithm-implement-the-quicksort-algorithm/802712/8 "2026-10-27T14:31:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ashley.varghese11](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ashley.varghese11/32/420476_2.png) [@ashley.varghese11](https://forum.freecodecamp.org/u/ashley.varghese11)\
**Post date:** [October 27, 2026, 2:42pm UTC](https://forum.freecodecamp.org/t/implement-the-quicksort-algorithm-implement-the-quicksort-algorithm/802712/9 "2026-10-27T14:42:55Z")

</div>

Nevermind I managed to figure it out.
