|
|
|
|
|
|
|
|
z[i], z[right] = z[right], z[i] |
|
|
z[i], z[right] = z[right], z[i] |
|
|
return i |
|
|
return i |
|
|
|
|
|
|
|
|
|
|
|
def median_pivot(z: AlgoDatArray, left: int, right: int) -> int: |
|
|
|
|
|
mid = left + (right - left) // 2 |
|
|
|
|
|
if z[left] < z[right]: |
|
|
|
|
|
if z[mid] < z[right]: |
|
|
|
|
|
# right is the largest |
|
|
|
|
|
if z[mid] < z[left]: |
|
|
|
|
|
# left is in the middle |
|
|
|
|
|
z[left], z[right] = z[right], z[left] |
|
|
|
|
|
else: |
|
|
|
|
|
# mid is in the middle |
|
|
|
|
|
z[mid], z[right] = z[right], z[mid] |
|
|
|
|
|
else: |
|
|
|
|
|
# right is in the middle |
|
|
|
|
|
pass |
|
|
|
|
|
else: |
|
|
|
|
|
if z[mid] < z[right]: |
|
|
|
|
|
# right is in the middle |
|
|
|
|
|
pass |
|
|
|
|
|
else: |
|
|
|
|
|
#right is the smallest |
|
|
|
|
|
if z[mid] < z[left]: |
|
|
|
|
|
# mid is in the middle |
|
|
|
|
|
z[mid], z[right] = z[right], z[mid] |
|
|
|
|
|
else: |
|
|
|
|
|
# mid is in the middle |
|
|
|
|
|
z[left], z[right] = z[right], z[left] |
|
|
|
|
|
return partition(z, left, right) |
|
|
|
|
|
|
|
|
def quick_sort(z: AlgoDatArray, left: int, right: int): |
|
|
def quick_sort(z: AlgoDatArray, left: int, right: int): |
|
|
if left < right: |
|
|
if left < right: |
|
|
q = partition(z, left, right) |
|
|
q = partition(z, left, right) |
|
|
|
|
|
#q = median_pivot(z, left, right) |
|
|
quick_sort(z, left, q-1) |
|
|
quick_sort(z, left, q-1) |
|
|
quick_sort(z, q+1, right) |
|
|
quick_sort(z, q+1, right) |
|
|
|
|
|
|
|
|
pivot = AlgoDatValue(0) |
|
|
pivot = AlgoDatValue(0) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
if __name__ == "__main__": |
|
|
z = read_int_sequence("../../seq0.txt") |
|
|
|
|
|
|
|
|
z = read_int_sequence("../../seq3.txt") |
|
|
print(z, len(z)) |
|
|
print(z, len(z)) |
|
|
start = pfc() |
|
|
start = pfc() |
|
|
quick_sort(z, 0, z.size-1) |
|
|
quick_sort(z, 0, z.size-1) |