I have tried to implement an in-place quicksort Algorithm in Python 2. Please suggest how I can
- Make the code more efficient
- Increase readability
- Improve writing style
- Make it more Pythonic
def swap(x,y):
return y,x
def sort(ar, start, end, l):
if start<end-1:
# pivot element is taken as the last element
p = ar[end-1]
ind=start-1
# when a smaller no. than pivot is found, swap it with first bigger-tahn-pivot element.
# keep index as the recently swapped smaller integer, if no smaller number found, than index is start-1 because pivot is out as first element
for i in xrange(start,end-1):
if ar[i]<p:
ind=i
for j in xrange(0,i):
if ar[j]>p:
ind=j
ar[i],ar[j] = swap(ar[i],ar[j])
break
#Swap pivot with the first bigger-than-pivot number
ar[ind+1], ar[end-1] = swap(ar[ind+1], p)
ar = sort(ar, start, ind+1, l)
ar = sort(ar, ind+2, end, l)
return ar
else:
return ar
if start < end - 1: pass else: pass
, I would suggest making aif start >= end - 1: return ar
at the beginning and unindenting the rest of the body. \$\endgroup\$ – alisianoi Feb 6 '16 at 12:00start < end - 1
instead ofstart<end-1
and so on. \$\endgroup\$ – alisianoi Feb 6 '16 at 12:01