|
|
|
|
|
|
|
|
from SoSe24.algodat.foundation import AlgoDatArray, AlgoDatValue, read_int_sequence |
|
|
from SoSe24.algodat.foundation import AlgoDatArray, AlgoDatValue, read_int_sequence |
|
|
from time import perf_counter as pfc |
|
|
from time import perf_counter as pfc |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def partition(z: AlgoDatArray, left: int, right: int) -> int: |
|
|
def partition(z: AlgoDatArray, left: int, right: int) -> int: |
|
|
global pivot |
|
|
global pivot |
|
|
pivot.value = z[right] |
|
|
|
|
|
|
|
|
pivot.value = z[right] |
|
|
i = left |
|
|
i = left |
|
|
j = right - 1 |
|
|
j = right - 1 |
|
|
while i < j: |
|
|
while i < j: |
|
|
|
|
|
|
|
|
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: |
|
|
def median_pivot(z: AlgoDatArray, left: int, right: int) -> int: |
|
|
mid = left + (right - left) // 2 |
|
|
mid = left + (right - left) // 2 |
|
|
if z[left] < z[right]: |
|
|
if z[left] < z[right]: |
|
|
|
|
|
|
|
|
z[left], z[right] = z[right], z[left] |
|
|
z[left], z[right] = z[right], z[left] |
|
|
return partition(z, left, right) |
|
|
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) |
|
|
|
|
|
|
|
|
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__": |