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

View File

@ -329,6 +329,12 @@ def read_int_sequence(filename: str) -> AlgoDatArray:
a[i].value = l[i]
return a
def write_int_sequence(z: AlgoDatArray, filename: str):
with open(filename, "w") as file:
for i in range(len(z)):
file.write(str(z[i].value)+ '\n')
def read_int_sequence_limited(filename: str, limit: int) -> AlgoDatArray:
"""Reads a sequence of integers from a file and returns an AlgoDatArray object."""
with open(filename, "r") as file:

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

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)

1000
seq3_sorted.txt Normal file

File diff suppressed because it is too large Load Diff