Skip to content

Instantly share code, notes, and snippets.

@prat0318
Created November 11, 2013 07:02
Show Gist options
  • Save prat0318/7409068 to your computer and use it in GitHub Desktop.
Save prat0318/7409068 to your computer and use it in GitHub Desktop.
pivot position correction
def swap(arr, a, b):
temp = arr[a]; arr[a] = arr[b]; arr[b] = temp
def correct_pivot_pos(arr):
pivot = 0
if len(arr) < 2 : return arr
start = 1; end = len(arr) - 1
while(start < end):
while((start < len(arr)) and (arr[start] < arr[pivot])): start+= 1
while((end > 0) and (arr[end] > arr[pivot])): end -= 1
if(start < end):
swap(arr, start, end)
start += 1; end -= 1
swap(arr, 0, end)
return arr
print correct_pivot_pos([2,7,0,3,4,5,1,6])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment