Skip to content

Done the Suggestions #143 #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions Sorting Algorithms/quick_sort.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
#---------------------------------------
# Quick Sort
#---------------------------------------
def quick_sort(A):
quick_sort2(A, 0, len(A)-1)
def quick_sort(ArrayToSort):
quick_sort2(ArrayToSort, 0, len(ArrayToSort)-1)

def quick_sort2(A, low, hi):
def quick_sort2(ArrayToSort, low, hi, threshold = 10):
if hi-low < threshold and low < hi:
quick_selection(A, low, hi)
quick_selection(ArrayToSort, low, hi)
elif low < hi:
p = partition(A, low, hi)
quick_sort2(A, low, p - 1)
quick_sort2(A, p + 1, hi)
p = partition(ArrayToSort, low, hi)
quick_sort2(ArrayToSort, low, p - 1)
quick_sort2(ArrayToSort, p + 1, hi)

def get_pivot(A, low, hi):
def get_pivot(ArrayToSort, low, hi):
mid = (hi + low) // 2
s = sorted([A[low], A[mid], A[hi]])
if s[1] == A[low]:
s = sorted([ArrayToSort[low], ArrayToSort[mid], ArrayToSort[hi]])
if s[1] == ArrayToSort[low]:
return low
elif s[1] == A[mid]:
elif s[1] == ArrayToSort[mid]:
return mid
return hi

def partition(A, low, hi):
pivotIndex = get_pivot(A, low, hi)
pivotValue = A[pivotIndex]
A[pivotIndex], A[low] = A[low], A[pivotIndex]
def partition(ArrayToSort, low, hi):
pivotIndex = get_pivot(ArrayToSort, low, hi)
pivotValue = ArrayToSort[pivotIndex]
ArrayToSort[pivotIndex], ArrayToSort[low] = ArrayToSort[low], ArrayToSort[pivotIndex]
border = low

for i in range(low, hi+1):
if A[i] < pivotValue:
if ArrayToSort[i] < pivotValue:
border += 1
A[i], A[border] = A[border], A[i]
A[low], A[border] = A[border], A[low]
ArrayToSort[i], ArrayToSort[border] = ArrayToSort[border], ArrayToSort[i]
ArrayToSort[low], ArrayToSort[border] = ArrayToSort[border], ArrayToSort[low]

return (border)

Expand All @@ -44,7 +44,7 @@ def quick_selection(x, first, last):
if minIndex != i:
x[i], x[minIndex] = x[minIndex], x[i]

A = [5,9,1,2,4,8,6,3,7]
print(A)
quick_sort(A)
print(A)
ArrayToSort = [5,9,1,2,4,8,6,3,7]
print(ArrayToSort)
quick_sort(ArrayToSort)
print(ArrayToSort)