Consider your partition method
for x in xrange(lo + 1, hi):
if array[x] <= array[lo]:
array[x], array[y] = array[y], array[x]
y += 1
Abstracted out it looks something like this.
def partition(array, lo, hi, pivot):
y = lo
for x in xrange(lo, hi):
if array[x] <= pivot:
array[x], array[y] = array[y], array[x]
y += 1
return y
# eg
pivot = partition(array, lo + 1, hi, array[lo])
array[lo], array[pivot] = array[pivot], array[lo]
That struck me as an odd partition method. It's not the one I learned yet it is simpler and seems to work. When searching for an example I came across this question on cs.exchange this question on cs.exchange. You should read the very detailed answer and consider using the Hoare partition as it always slightly more efficient.
Also you should consider implementing a separate swap method. The idiom you use is perfectly fine it just gets verbose and you use it often enough
def swap_indexes(array, a, b):
array[a], array[b] = array[b], array[a]
Consider your partition method
for x in xrange(lo + 1, hi):
if array[x] <= array[lo]:
array[x], array[y] = array[y], array[x]
y += 1
Abstracted out it looks something like this.
def partition(array, lo, hi, pivot):
y = lo
for x in xrange(lo, hi):
if array[x] <= pivot:
array[x], array[y] = array[y], array[x]
y += 1
return y
# eg
pivot = partition(array, lo + 1, hi, array[lo])
array[lo], array[pivot] = array[pivot], array[lo]
That struck me as an odd partition method. It's not the one I learned yet it is simpler and seems to work. When searching for an example I came across this question on cs.exchange. You should read the very detailed answer and consider using the Hoare partition as it always slightly more efficient.
Also you should consider implementing a separate swap method. The idiom you use is perfectly fine it just gets verbose and you use it often enough
def swap_indexes(array, a, b):
array[a], array[b] = array[b], array[a]
Consider your partition method
for x in xrange(lo + 1, hi):
if array[x] <= array[lo]:
array[x], array[y] = array[y], array[x]
y += 1
Abstracted out it looks something like this.
def partition(array, lo, hi, pivot):
y = lo
for x in xrange(lo, hi):
if array[x] <= pivot:
array[x], array[y] = array[y], array[x]
y += 1
return y
# eg
pivot = partition(array, lo + 1, hi, array[lo])
array[lo], array[pivot] = array[pivot], array[lo]
That struck me as an odd partition method. It's not the one I learned yet it is simpler and seems to work. When searching for an example I came across this question on cs.exchange. You should read the very detailed answer and consider using the Hoare partition as it always slightly more efficient.
Also you should consider implementing a separate swap method. The idiom you use is perfectly fine it just gets verbose and you use it often enough
def swap_indexes(array, a, b):
array[a], array[b] = array[b], array[a]
Consider your partition method
for x in xrange(lo + 1, hi):
if array[x] <= array[lo]:
array[x], array[y] = array[y], array[x]
y += 1
Abstracted out it looks something like this.
def partition(array, lo, hi, pivot):
y = lo
for x in xrange(lo, hi):
if array[x] <= pivot:
array[x], array[y] = array[y], array[x]
y += 1
return y
# eg
pivot = partition(array, lo + 1, hi, array[lo])
array[lo], array[pivot] = array[pivot], array[lo]
That struck me as an odd partition method. It's not the one I learned yet it is simpler and seems to work. When searching for an example I came across this question on cs.exchange. You should read the very detailed answer and consider using the Hoare partition as it always slightly more efficient.
Also you should consider implementing a separate swap method. The idiom you use is perfectly fine it just gets verbose and you use it often enough
def swap_indexes(array, a, b):
array[a], array[b] = array[b], array[a]