Skip to content

Instantly share code, notes, and snippets.

@5hirish
Created July 12, 2016 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5hirish/399182b254871933e99ac6d4a8fef737 to your computer and use it in GitHub Desktop.
Save 5hirish/399182b254871933e99ac6d4a8fef737 to your computer and use it in GitHub Desktop.
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