Implement Quicksort in Python

+1 vote

I am trying to implement quicksort.

I do not know how to concatenate the three arrays and printing them.

Could someone please help me complete my code?

def sort(array=[2,5,1,6,9,8,7,10,21,12]):
    less = []
    equal = []
    greater = []

    if len(array) > 1:
        pivot = array[0]
        for x in array:
            if x < pivot:
                less.append(x)
            if x == pivot:
                equal.append(x)
            if x > pivot:
                greater.append(x)
            sort(less)
            sort(pivot)
            sort(greater)

Oct 30, 2018 in Python by findingbugs
• 3,260 points
1,078 views

1 answer to this question.

0 votes

This will help you.

def sort(array=[2,5,1,6,9,8,7,10,21,12]):
    less = []
    equal = []
    greater = []

    if len(array) > 1:
        pivot = array[0]
        for x in array:
            if x < pivot:
                less.append(x)
            if x == pivot:
                equal.append(x)
            if x > pivot:
                greater.append(x)       # Don't forget to return something!
        return sort(less)+equal+sort(greater)  # Just use the + operator to join lists
    # Note that you want equal ^^^^^ not pivot
    else:  # You need to hande the part at the end of the recursion - when you only have one element in your array, just return the array.
        return array
answered Oct 30, 2018 by Priyaj
• 58,020 points

Related Questions In Python

0 votes
1 answer

How to implement Hashmaps in Python

Python dictionary is a built-in type that supports ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,720 points
1,084 views
0 votes
1 answer

Is there a foreach function in python and is there a way to implement it if there isnt any

Every occurence of "foreach" I've seen (PHP, ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,720 points
1,157 views
0 votes
1 answer

How to implement Queue in python

You are missing this  from queue import * This ...READ MORE

answered Nov 2, 2018 in Python by Nabarupa
3,080 views
0 votes
1 answer

Pycharm warning: Must implement all abstract methods in Python. Why?

n vote As expected, python itself recognises that ...READ MORE

answered Nov 9, 2018 in Python by Nymeria
• 3,560 points
3,855 views
0 votes
0 answers

Quicksort with Python

I am totally new to python and ...READ MORE

Apr 26, 2022 in Python by Edureka
• 13,620 points
439 views
+1 vote
1 answer

Quicksort in Python

The following code may solve your problem: def ...READ MORE

answered Sep 20, 2018 in Python by SDeb
• 13,300 points
711 views
0 votes
2 answers
0 votes
1 answer

“stub” __objclass__ in a Python class how to implement it?

You want to avoid interfering with this ...READ MORE

answered Sep 27, 2018 in Python by Priyaj
• 58,020 points
2,355 views
0 votes
1 answer

How to implement Linked List in Python?

You can use Deque that works better than linked list ...READ MORE

answered Oct 26, 2018 in Python by Priyaj
• 58,020 points
1,053 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP