Skip to content

Commit f8747a3

Browse files
committed
fixed get_pivot function in QuickSort
1 parent 802830f commit f8747a3

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

SortingAlgorithms.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,12 @@ def quick_sort2(A, low, hi):
118118

119119
def get_pivot(A, low, hi):
120120
mid = (hi + low) // 2
121-
pivot = hi
122-
if low < mid:
123-
if mid < hi:
124-
pivot = mid
125-
elif low < hi:
126-
pivot = low
127-
128-
return pivot
121+
s = sorted([A[low], A[mid], A[hi]])
122+
if s[1] == A[low]:
123+
return low
124+
elif s[1] == A[mid]:
125+
return mid
126+
return hi
129127

130128
def partition(A, low, hi):
131129
pivotIndex = get_pivot(A, low, hi)

0 commit comments

Comments
 (0)