Skip to content

Instantly share code, notes, and snippets.

@vikaskumar185
Last active June 3, 2018 07:33
Show Gist options
  • Save vikaskumar185/af75e7d51797f2d5043426f5255242fc to your computer and use it in GitHub Desktop.
Save vikaskumar185/af75e7d51797f2d5043426f5255242fc to your computer and use it in GitHub Desktop.
QuickSort
#Author: VikasKumar<vikaskumar185@gmail.com>
#Time Complexity:
#Best Case : O(nlogn)
#Worst Case : O(n^2)
#Suggestion : Always use middle element as Pivot
#QuickSort
def QuickSort(arr,low,high):
if low<high:
j = Partition(arr,low,high)
QuickSort(arr,low,j-1)
QuickSort(arr,j+1,high)
def Partition(arr,low,high):
pivot = arr[low]
i = low+1
j = high
flag = False
while not flag:
while i <= j and arr[i] <= pivot:
i = i + 1
while arr[j] >= pivot and j >= i:
j = j -1
if j < i:
flag = True
else:
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
temp = arr[low]
arr[low] = arr[j]
arr[j] = temp
return j
arr = [4,8,5,1,2,6,3,9,7]
QuickSort(arr,0,len(arr)-1)
print(arr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment