+1 vote

I am trying to implement quicksort.

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

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 780 views

1 answer to this question.

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
• 58,090 points

How to implement Hashmaps in Python

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

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

How to implement Queue in python

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

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

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

Quicksort with Python

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

+1 vote

Quicksort in Python

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

how do i change string to a list?

suppose you have a string with a ...READ MORE

how can i randomly select items from a list?

You can also use the random library's ...READ MORE

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

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

How to implement Linked List in Python?

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