From b8359dc468050eaed1a222f3712ecb9e1a69773a Mon Sep 17 00:00:00 2001 From: Leon Date: Tue, 1 Aug 2023 10:13:30 +0330 Subject: [PATCH 1/3] feat: add `env` files to `.gitignore` --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index 96374c4e..da3f1831 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,8 @@ $RECYCLE.BIN/ Network Trash Folder Temporary Items .apdisk + + +# env files +env + From 9815cb73aa332683c14708fdc7fbdb0975bdb4a6 Mon Sep 17 00:00:00 2001 From: Leon Date: Tue, 1 Aug 2023 10:13:34 +0330 Subject: [PATCH 2/3] feat: code check with black formatter --- Sorting Algorithms/quick_sort.py | 105 ++++++++++++++++--------------- 1 file changed, 55 insertions(+), 50 deletions(-) diff --git a/Sorting Algorithms/quick_sort.py b/Sorting Algorithms/quick_sort.py index 9731a8ee..cfea285f 100644 --- a/Sorting Algorithms/quick_sort.py +++ b/Sorting Algorithms/quick_sort.py @@ -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) \ No newline at end of file +# --------------------------------------- +# 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) From 83737dc666e96818392771677acdf3009e389b60 Mon Sep 17 00:00:00 2001 From: Leon Date: Tue, 1 Aug 2023 10:19:57 +0330 Subject: [PATCH 3/3] fix: removed `threshold` variable --- Sorting Algorithms/quick_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorting Algorithms/quick_sort.py b/Sorting Algorithms/quick_sort.py index cfea285f..ff2d4bae 100644 --- a/Sorting Algorithms/quick_sort.py +++ b/Sorting Algorithms/quick_sort.py @@ -6,7 +6,7 @@ def quick_sort(A): def quick_sort2(A, low, hi): - if hi - low < threshold and low < hi: + if hi - low < low < hi: quick_selection(A, low, hi) elif low < hi: p = partition(A, low, hi)