Implemented Algo_4 with O(n)

This commit is contained in:
Bernhard Schoeffel 2025-03-27 16:41:26 +01:00
parent c0d376cd5c
commit 79f0fc36fd

View File

@ -101,6 +101,24 @@ def max_sequence_3(z: MemoryArray):
rend = Literal(len(z) - 1)
return _max_sequence_3(z, lstart, rend)
def max_sequence_4(z: MemoryArray):
n = z.length()
max = MemoryCell(MIN_VALUE)
aktLinks = MemoryCell()
links = MemoryCell()
rechts = MemoryCell()
aktSum = MemoryCell()
for i in mrange(n):
aktSum += z[i]
if aktSum > max:
max.set(aktSum)
links.set(aktLinks)
rechts.set(i)
if aktSum < Literal(0):
aktSum.set(0)
aktLinks.set(MemoryCell(1) + i)
return (max, links, rechts)
def example(max_sequence_func):
l = [-59, 52, 46, 14, -50, 58, -87, -77, 34, 15]
print(l)
@ -133,7 +151,7 @@ def analyze_complexity(max_sequence_func, sizes):
if __name__ == '__main__':
fn = max_sequence_3
fn = max_sequence_4
example(fn)
for filename in ["data/seq0.txt", "data/seq1.txt", "data/seq2.txt"]:
print(filename)