Browse Source

Lecture 3

master
Oliver Hofmann 7 months ago
parent
commit
2790f31141

+ 6
- 0
SoSe24/algodat/foundation.py View File

a[i].value = l[i] a[i].value = l[i]
return a 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: def read_int_sequence_limited(filename: str, limit: int) -> AlgoDatArray:
"""Reads a sequence of integers from a file and returns an AlgoDatArray object.""" """Reads a sequence of integers from a file and returns an AlgoDatArray object."""
with open(filename, "r") as file: with open(filename, "r") as file:

+ 2
- 2
SoSe24/lec03_sort_alg/quick_plot.py View File





if __name__ == "__main__": if __name__ == "__main__":
filename = "../../seq3.txt"
dummy = read_int_sequence("../../seq3.txt")
filename = "../../seq3_sorted.txt"
dummy = read_int_sequence(filename)
n = len(dummy) n = len(dummy)
step = n // 100 step = n // 100



+ 30
- 1
SoSe24/lec03_sort_alg/quick_sort.py View File

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)

+ 1000
- 0
seq3_sorted.txt
File diff suppressed because it is too large
View File


Loading…
Cancel
Save