Lecture 3

This commit is contained in:
Oliver Hofmann
2024-04-22 15:39:10 +02:00
parent 5ec825647c
commit 2790f31141
4 changed files with 1038 additions and 3 deletions
+2 -2
View File
@@ -6,8 +6,8 @@ import quick_sort as qs
if __name__ == "__main__":
filename = "../../seq3.txt"
dummy = read_int_sequence("../../seq3.txt")
filename = "../../seq3_sorted.txt"
dummy = read_int_sequence(filename)
n = len(dummy)
step = n // 100
+30 -1
View File
@@ -21,16 +21,45 @@ def partition(z: AlgoDatArray, left: int, right: int) -> int:
z[i], z[right] = z[right], z[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):
if left < right:
q = partition(z, left, right)
#q = median_pivot(z, left, right)
quick_sort(z, left, q-1)
quick_sort(z, q+1, right)
pivot = AlgoDatValue(0)
if __name__ == "__main__":
z = read_int_sequence("../../seq0.txt")
z = read_int_sequence("../../seq3.txt")
print(z, len(z))
start = pfc()
quick_sort(z, 0, z.size-1)