This code implements quick sort and takes advantage of passing the list by reference. I'm looking for performance and general best practices.
import random
def quicksort(array, l=0, r=-1):
# array is list to sort, array is going to be passed by reference, this is new to me, try not to suck
# l is the left bound of the array to be acte on
# r is the right bound of the array to act on
if r == -1:
r = len(array)
# base case
if r-l <= 1:
return
if r == -1:
r = len(array)
piviot = int((r-l)*random.random()) + l
i = l+1 # Barrier between below and above piviot, first higher element
swap(array, l, piviot) #piviot is now in index l
for j in range(l+1,r):
if array[j] < array[l]:
swap(array, i, j)
i = i+1
swap(array, l, i-1) # i-1 is now the piviot
quicksort(array, l, i-1)
quicksort(array, i, r)
return array
def swap(array, a, b):
# a and b are the indicies of the array to be swapped
a2 = array[a]
array[a] = array[b]
array[b] = a2
return