Browse Source

Lecture 3

master
Oliver Hofmann 8 months ago
parent
commit
2790f31141

+ 6
- 0
SoSe24/algodat/foundation.py 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:

+ 2
- 2
SoSe24/lec03_sort_alg/quick_plot.py 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
SoSe24/lec03_sort_alg/quick_sort.py 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
- 0
seq3_sorted.txt
File diff suppressed because it is too large
View File


Loading…
Cancel
Save