Home » Python » Quicksort with Python

# Quicksort with Python

Questions:

I am totally new to python and I am trying to implement quicksort in it.

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

``````def sort(array=[12,4,5,6,7,3,1,15]):
less = []
equal = []
greater = []

if len(array) > 1:
pivot = array
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)
``````
``````def sort(array=[12,4,5,6,7,3,1,15]):
less = []
equal = []
greater = []

if len(array) > 1:
pivot = array
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
``````

Questions:

Quick sort without additional memory (in place)

Usage:

``````array = [97, 200, 100, 101, 211, 107]
quicksort(array)
# array -> [97, 100, 101, 107, 200, 211]
``````
``````def partition(array, begin, end):
pivot = begin
for i in xrange(begin+1, end+1):
if array[i] <= array[begin]:
pivot += 1
array[i], array[pivot] = array[pivot], array[i]
array[pivot], array[begin] = array[begin], array[pivot]
return pivot

def quicksort(array, begin=0, end=None):
if end is None:
end = len(array) - 1
def _quicksort(array, begin, end):
if begin >= end:
return
pivot = partition(array, begin, end)
_quicksort(array, begin, pivot-1)
_quicksort(array, pivot+1, end)
return _quicksort(array, begin, end)
``````

Questions:

There is another concise and beautiful version

``````def qsort(arr):
if len(arr) <= 1:
return arr
else:
return qsort([x for x in arr[1:] if x<arr]) + [arr] + qsort([x for x in arr[1:] if x>=arr])
``````

Let me explain the above codes for details

1. pick the first element of array `arr` as pivot

`[arr]`

2. `qsort` those elements of array which are less than pivot with `List Comprehension`

`qsort([x for x in arr[1:] if x<arr])`

3. `qsort` those elements of array which are larger than pivot with `List Comprehension`

`qsort([x for x in arr[1:] if x>=arr])`

Questions:

There are many answers to this already, but I think this approach is the most clean implementation:

``````def quicksort(arr):
""" Quicksort a list

:type arr: list
:param arr: List to sort
:returns: list -- Sorted list
"""
if not arr:
return []

pivots = [x for x in arr if x == arr]
lesser = quicksort([x for x in arr if x < arr])
greater = quicksort([x for x in arr if x > arr])

return lesser + pivots + greater
``````

You can of course skip storing everything in variables and return them straight away like this:

``````def quicksort(arr):
""" Quicksort a list

:type arr: list
:param arr: List to sort
:returns: list -- Sorted list
"""
if not arr:
return []

return quicksort([x for x in arr if x < arr]) \
+ [x for x in arr if x == arr] \
+ quicksort([x for x in arr if x > arr])
``````

Questions:

If I search “python quicksort implementation” in Google, this question is the first result to pop up. I understand that the initial question was to “help correct the code” but there already are many answers that disregard that request: the currently second most voted one, the horrendous one-liner with the hilarious “You are fired” comment and, in general, many implementations that are not in-place (i.e. use extra memory proportional to input list size). This answer provides an in-place solution but it is for `python 2.x`. So, below follows my interpretation of the in-place solution from Rosetta Code which will work just fine for `python 3` too:

``````import random

def qsort(l, fst, lst):
if fst >= lst: return

i, j = fst, lst
pivot = l[random.randint(fst, lst)]

while i <= j:
while l[i] < pivot: i += 1
while l[j] > pivot: j -= 1
if i <= j:
l[i], l[j] = l[j], l[i]
i, j = i + 1, j - 1
qsort(l, fst, j)
qsort(l, i, lst)
``````

And if you are willing to forgo the in-place property, below is yet another version which better illustrates the basic ideas behind quicksort. Apart from readability, its other advantage is that it is stable (equal elements appear in the sorted list in the same order that they used to have in the unsorted list). This stability property does not hold with the less memory-hungry in-place implementation presented above.

``````def qsort(l):
if not l: return l # empty sequence case
pivot = l[random.choice(range(0, len(l)))]

head = qsort([elem for elem in l if elem < pivot])
tail = qsort([elem for elem in l if elem > pivot])
return head + [elem for elem in l if elem == pivot] + tail
``````

Questions:

functional approach:

``````def qsort(list):
if len(list) < 2:
return list

pivot = list.pop()
left = filter(lambda x: x <= pivot, list)
right = filter(lambda x: x > pivot, list)

return qsort(left) + [pivot] + qsort(right)
``````

Questions:

# Quicksort with Python

In real life, we should always use the builtin sort provided by Python. However, understanding the quicksort algorithm is instructive.

My goal here is to break down the subject such that it is easily understood and replicable by the reader without having to return to reference materials.

The quicksort algorithm is essentially the following:

1. Select a pivot data point.
2. Move all data points less than (below) the pivot to a position below the pivot – move those greater than or equal to (above) the pivot to a position above it.
3. Apply the algorithm to the areas above and below the pivot

If the data are randomly distributed, selecting the first data point as the pivot is equivalent to a random selection.

First, let’s look at a readable example that uses comments and variable names to point to intermediate values:

``````def quicksort(xs):
"""Given indexable and slicable iterable, return a sorted list"""
if xs: # if given list (or tuple) with one ordered item or more:
pivot = xs
# below will be less than:
below = [i for i in xs[1:] if i < pivot]
# above will be greater than or equal to:
above = [i for i in xs[1:] if i >= pivot]
return quicksort(below) + [pivot] + quicksort(above)
else:
return xs # empty list
``````

To restate the algorithm and code demonstrated here – we move values above the pivot to the right, and values below the pivot to the left, and then pass those partitions to same function to be further sorted.

## Golfed:

This can be golfed to 88 characters:

``````q=lambda x:x and q([i for i in x[1:]if i<=x])+[x]+q([i for i in x[1:]if i>x])
``````

To see how we get there, first take our readable example, remove comments and docstrings, and find the pivot in-place:

``````def quicksort(xs):
if xs:
below = [i for i in xs[1:] if i < xs]
above = [i for i in xs[1:] if i >= xs]
return quicksort(below) + [xs] + quicksort(above)
else:
return xs
``````

Now find below and above, in-place:

``````def quicksort(xs):
if xs:
return (quicksort([i for i in xs[1:] if i < xs] )
+ [xs]
+ quicksort([i for i in xs[1:] if i >= xs]))
else:
return xs
``````

Now, knowing that `and` returns the prior element if false, else if it is true, it evaluates and returns the following element, we have:

``````def quicksort(xs):
return xs and (quicksort([i for i in xs[1:] if i < xs] )
+ [xs]
+ quicksort([i for i in xs[1:] if i >= xs]))
``````

Since lambdas return a single epression, and we have simplified to a single expression (even though it is getting more unreadable) we can now use a lambda:

``````quicksort = lambda xs: (quicksort([i for i in xs[1:] if i < xs] )
+ [xs]
+ quicksort([i for i in xs[1:] if i >= xs]))
``````

And to reduce to our example, shorten the function and variable names to one letter, and eliminate the whitespace that isn’t required.

``````q=lambda x:x and q([i for i in x[1:]if i<=x])+[x]+q([i for i in x[1:]if i>x])
``````

Note that this lambda, like most code golfing, is rather bad style.

## Conclusion

This algorithm is frequently taught in computer science courses and asked for on job interviews. It helps us think about recursion and divide-and-conquer.

Quicksort is not very practical in Python since our builtin timsort algorithm is quite efficient, and we have recursion limits. We would expect to sort lists in-place with `list.sort` or create new sorted lists with `sorted` – both of which take a `key` and `reverse` argument.

Questions:

functional programming aproach

``````smaller = lambda xs, y: filter(lambda x: x <= y, xs)
larger = lambda xs, y: filter(lambda x: x > y, xs)
qsort = lambda xs: qsort(smaller(xs[1:],xs)) + [xs] + qsort(larger(xs[1:],xs)) if xs != [] else []

print qsort([3,1,4,2,5]) == [1,2,3,4,5]
``````

Questions:

I think both answers here works ok for the list provided (which answer the original question), but would breaks if an array containing non unique values is passed. So for completeness, I would just point out the small error in each and explain how to fix them.

For example trying to sort the following array [12,4,5,6,7,3,1,15,1] (Note that 1 appears twice) with Brionius algorithm .. at some point will end up with the less array empty and the equal array with a pair of values (1,1) that can not be separated in the next iteration and the len() > 1…hence you’ll end up with an infinite loop

You can fix it by either returning array if less is empty or better by not calling sort in your equal array, as in zangw answer

``````def sort(array=[12,4,5,6,7,3,1,15]):
less = []
equal = []
greater = []

if len(array) > 1:
pivot = array
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
``````

The fancier solution also breaks, but for a different cause, it is missing the return clause in the recursion line, which will cause at some point to return None and try to append it to a list ….

``````def qsort(arr):
if len(arr) <= 1:
return arr
else:
return qsort([x for x in arr[1:] if x<arr]) + [arr] + qsort([x for x in arr[1:] if x>=arr])
``````

Questions:
``````def quick_sort(array):
return quick_sort([x for x in array[1:] if x < array]) + [array] \
+ quick_sort([x for x in array[1:] if x >= array]) if array else []
``````

Questions:
``````def Partition(A,p,q):
i=p
x=A[i]
for j in range(p+1,q+1):
if A[j]<=x:
i=i+1
tmp=A[j]
A[j]=A[i]
A[i]=tmp
l=A[p]
A[p]=A[i]
A[i]=l
return i

def quickSort(A,p,q):
if p<q:
r=Partition(A,p,q)
quickSort(A,p,r-1)
quickSort(A,r+1,q)
return A
``````

Questions:

A “true” in-place implementation [Algorithms 8.9, 8.11 from the Algorithm Design and Applications Book by Michael T. Goodrich and Roberto Tamassia]:

``````from random import randint

def partition (A, a, b):
p = randint(a,b)
# or mid point
# p = (a + b) / 2

piv = A[p]

# swap the pivot with the end of the array
A[p] = A[b]
A[b] = piv

i = a     # left index (right movement ->)
j = b - 1 # right index (left movement <-)

while i <= j:
# move right if smaller/eq than/to piv
while A[i] <= piv and i <= j:
i += 1
# move left if greater/eq than/to piv
while A[j] >= piv and j >= i:
j -= 1

# indices stopped moving:
if i < j:
# swap
t = A[i]
A[i] = A[j]
A[j] = t
# place pivot back in the right place
# all values < pivot are to its left and
# all values > pivot are to its right
A[b] = A[i]
A[i] = piv

return i

def IpQuickSort (A, a, b):

while a < b:
p = partition(A, a, b) # p is pivot's location

#sort the smaller partition
if p - a < b - p:
IpQuickSort(A,a,p-1)
a = p + 1 # partition less than p is sorted
else:
IpQuickSort(A,p+1,b)
b = p - 1 # partition greater than p is sorted

def main():
A =  [12,3,5,4,7,3,1,3]
print A
IpQuickSort(A,0,len(A)-1)
print A

if __name__ == "__main__": main()
``````

Questions:

The algorithm has 4 simple steps:

1. Divide the array into 3 different parts: left, pivot and right, where pivot will have only one element. Let us choose this pivot element as the first element of array
2. Append elements to the respective part by comparing them to pivot element. (explanation in comments)
3. Recurse this algorithm till all elements in the array have been sorted
4. Finally, join left+pivot+right parts

Code for the algorithm in python:

``````def my_sort(A):

p=A                                       #determine pivot element.
left=[]                                      #create left array
right=[]                                     #create right array
for i in range(1,len(A)):
#if cur elem is less than pivot, add elem in left array
if A[i]< p:
left.append(A[i])
#the recurssion will occur only if the left array is atleast half the size of original array
if len(left)>1 and len(left)>=len(A)//2:
left=my_sort(left)                            #recursive call
elif A[i]>p:
right.append(A[i])                                #if elem is greater than pivot, append it to right array
if len(right)>1 and len(right)>=len(A)//2:        # recurssion will occur only if length of right array is atleast the size of original array
right=my_sort(right)
A=left+[p]+right                                        #append all three part of the array into one and return it
return A

my_sort([12,4,5,6,7,3,1,15])
``````

Carry on with this algorithm recursively with the left and right parts.

Questions:

For Version Python 3.x: a functional-style using `operator` module, primarily to improve readability.

``````from operator import ge as greater, lt as lesser

def qsort(L):
if len(L) <= 1: return L
pivot   = L
sublist = lambda op: [*filter(lambda num: op(num, pivot), L[1:])]

return qsort(sublist(lesser))+ [pivot] + qsort(sublist(greater))
``````

and is tested as

``````print (qsort([3,1,4,2,5]) == [1,2,3,4,5])
``````

Questions:
``````def quick_sort(self, nums):
def helper(arr):
if len(arr) <= 1: return arr
#lwall is the index of the first element euqal to pivot
#rwall is the index of the first element greater than pivot
#so arr[lwall:rwall] is exactly the middle part equal to pivot after one round
lwall, rwall, pivot = 0, 0, 0
#choose rightmost as pivot
pivot = arr[-1]
for i, e in enumerate(arr):
if e < pivot:
#when element is less than pivot, shift the whole middle part to the right by 1
arr[i], arr[lwall] = arr[lwall], arr[i]
lwall += 1
arr[i], arr[rwall] = arr[rwall], arr[i]
rwall += 1
elif e == pivot:
#when element equals to pivot, middle part should increase by 1
arr[i], arr[rwall] = arr[rwall], arr[i]
rwall += 1
elif e > pivot: continue
return helper(arr[:lwall]) + arr[lwall:rwall] + helper(arr[rwall:])
return helper(nums)
``````

Questions:

Full example with printed variables at partition step:

``````def partition(data, p, right):
print("\n==> Enter partition: p={}, right={}".format(p, right))
pivot = data[right]
print("pivot = data[{}] = {}".format(right, pivot))

i = p - 1  # this is a dangerous line

for j in range(p, right):
print("j: {}".format(j))
if data[j] <= pivot:
i = i + 1
print("new i: {}".format(i))
print("swap: {} <-> {}".format(data[i], data[j]))
data[i], data[j] = data[j], data[i]

print("swap2: {} <-> {}".format(data[i + 1], data[right]))
data[i + 1], data[right] = data[right], data[i + 1]
return i + 1

def quick_sort(data, left, right):
if left < right:
pivot = partition(data, left, right)
quick_sort(data, left, pivot - 1)
quick_sort(data, pivot + 1, right)

data = [2, 8, 7, 1, 3, 5, 6, 4]

print("Input array: {}".format(data))
quick_sort(data, 0, len(data) - 1)
print("Output array: {}".format(data))
``````

Questions:

Another quicksort implementation:

``````# A = Array
# s = start index
# e = end index
# p = pivot index
# g = greater than pivot boundary index

def swap(A,i1,i2):
A[i1], A[i2] = A[i2], A[i1]

def partition(A,g,p):
# O(n) - just one for loop that visits each element once
for j in range(g,p):
if A[j] <= A[p]:
swap(A,j,g)
g += 1

swap(A,p,g)
return g

def _quicksort(A,s,e):
# Base case - we are sorting an array of size 1
if s >= e:
return

# Partition current array
p = partition(A,s,e)
_quicksort(A,s,p-1) # Left side of pivot
_quicksort(A,p+1,e) # Right side of pivot

# Wrapper function for the recursive one
def quicksort(A):
_quicksort(A,0,len(A)-1)

A = [3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,-1]

print(A)
quicksort(A)
print(A)
``````

Questions:
``````def quicksort(items):
if not len(items) > 1:
return items
items, pivot = partition(items)
return quicksort(items[:pivot]) + [items[pivot]] + quicksort(items[pivot + 1:])

def partition(items):
i = 1
pivot = 0
for j in range(1, len(items)):
if items[j] <= items[pivot]:
items[i], items[j] = items[j], items[i]
i += 1
items[i - 1], items[pivot] = items[pivot], items[i - 1]
return items, i - 1
``````

Questions:

inlace sort

``````def qsort(a, b=0, e=None):
# partitioning
def part(a, start, end):
p = start
for i in xrange(start+1, end):
if a[i] < a[p]:
a[i], a[p+1] = a[p+1], a[i]
a[p+1], a[p] = a[p], a[p+1]
p += 1
return p

if e is None:
e = len(a)
if e-b <= 1: return

p = part(a, b, e)
qsort(a, b, p)
qsort(a, p+1, e)
``````

without recursion:

``````deq = collections.deque()
deq.append((b, e))
while len(deq):
el = deq.pop()
if el - el > 1:
p = part(a, el, el)
deq.append((el, p))
deq.append((p+1, el))
``````

Questions:

Instead of taking three different arrays for less equal greater and then concatenating all try the traditional concept(partitioning method):

this is without using any inbuilt function.

partitioning function –

``````def partitn(alist, left, right):
i=left
j=right
mid=(left+right)/2

pivot=alist[mid]
while i <= j:
while alist[i] < pivot:
i=i+1

while alist[j] > pivot:
j=j-1

if i <= j:
temp = alist[i]
alist[i] = alist[j]
alist[j] = temp
i = i + 1
j = j - 1
``````

Questions:
1. First we declare the first value in the array to be the
pivot_value and we also set the left and right marks
2. We create the first while loop, this while loop is there to tell
the partition process to run again if it doesn’t satisfy the
necessary condition
3. then we apply the partition process
4. after both partition processes have ran, we check to see if it
satisfies the proper condition. If it does, we mark it as done,
if not we switch the left and right values and apply it again
5. Once its done switch the left and right values and return the
split_point

I am attaching the code below! This quicksort is a great learning tool because of the Location of the pivot value. Since it is in a constant place, you can walk through it multiple times and really get a hang of how it all works. In practice it is best to randomize the pivot to avoid O(N^2) runtime.

``````def quicksort10(alist):
quicksort_helper10(alist, 0, len(alist)-1)

def quicksort_helper10(alist, first, last):
"""  """
if first < last:
split_point = partition10(alist, first, last)
quicksort_helper10(alist, first, split_point - 1)
quicksort_helper10(alist, split_point + 1, last)

def partition10(alist, first, last):
done = False
pivot_value = alist[first]
leftmark = first + 1
rightmark = last
while not done:
while leftmark <= rightmark and alist[leftmark] <= pivot_value:
leftmark = leftmark + 1
while leftmark <= rightmark and alist[rightmark] >= pivot_value:
rightmark = rightmark - 1

if leftmark > rightmark:
done = True
else:
temp = alist[leftmark]
alist[leftmark] = alist[rightmark]
alist[rightmark] = temp
temp = alist[first]
alist[first] = alist[rightmark]
alist[rightmark] = temp
return rightmark
``````

Questions:
``````def quick_sort(l):
if len(l) == 0:
return l
pivot = l
pivots = [x for x in l if x == pivot]
smaller = quick_sort([x for x in l if x < pivot])
larger = quick_sort([x for x in l if x > pivot])
return smaller + pivots + larger
``````

Questions:
``````def quick_sort(list):
if len(list) ==0:
return []

return  quick_sort(filter( lambda item: item < list,list)) + [v for v in list if v==list ]  +  quick_sort( filter( lambda item: item > list, list))
``````

Questions:
``````qsort = lambda x=None, *xs: [] if x is None else qsort(*[a for a in xs if a<x]) + [x] + qsort(*[a for a in xs if a>=x])