Created
November 11, 2013 07:02
-
-
Save prat0318/7409068 to your computer and use it in GitHub Desktop.
pivot position correction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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