Created
July 12, 2016 07:40
-
-
Save 5hirish/399182b254871933e99ac6d4a8fef737 to your computer and use it in GitHub Desktop.
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 partition(array, first, last): | |
pivot = array[last] | |
print "Pivot: ", pivot | |
index = first | |
for j in range(first,last): | |
print "Comparing ", array[j]," and ",pivot | |
if array[j] <= pivot: | |
print "Swapping ",array[j], " and ", array[index] | |
temp = array[j] | |
array[j] = array[index] | |
array[index] = temp | |
index += 1 | |
print "After Swapping: ",array | |
print "\nPlacing Pivot:" | |
temp_pivot = array[index] | |
array[index] = array[last] | |
array[last] = temp_pivot | |
print array | |
return index | |
def quicksort(array, first, last): | |
if first < last: | |
print "Partition : ", array[first:last + 1] | |
pivot_index = partition(array, first, last) | |
print "Left Partition : ", array[first : pivot_index] | |
quicksort(array, first, pivot_index - 1) | |
print "Right Partition : ", array[pivot_index + 1: last] | |
quicksort(array, pivot_index + 1, last) | |
array = [50, 60, 20, 10, 80, 90, 30] | |
print "Unsorted Array:",array | |
quicksort(array, 0, len(array) - 1) | |
#print partition(array, 0, len(array) - 1) | |
print array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment