Skip to content
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

Fix/quick sort #142

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ $RECYCLE.BIN/
Network Trash Folder
Temporary Items
.apdisk


# env files
env

105 changes: 55 additions & 50 deletions Sorting Algorithms/quick_sort.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,55 @@
#---------------------------------------
# Quick Sort
#---------------------------------------
def quick_sort(A):
quick_sort2(A, 0, len(A)-1)

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

def get_pivot(A, low, hi):
mid = (hi + low) // 2
s = sorted([A[low], A[mid], A[hi]])
if s[1] == A[low]:
return low
elif s[1] == A[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]
border = low

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

return (border)

def quick_selection(x, first, last):
for i in range (first, last):
minIndex = i
for j in range (i+1, last+1):
if x[j] < x[minIndex]:
minIndex = j
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)
# ---------------------------------------
# Quick Sort
# ---------------------------------------
def quick_sort(A):
quick_sort2(A, 0, len(A) - 1)


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


def get_pivot(A, low, hi):
mid = (hi + low) // 2
s = sorted([A[low], A[mid], A[hi]])
if s[1] == A[low]:
return low
elif s[1] == A[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]
border = low

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

return border


def quick_selection(x, first, last):
for i in range(first, last):
minIndex = i
for j in range(i + 1, last + 1):
if x[j] < x[minIndex]:
minIndex = j
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)