From 8ddb4fa427a666ba62d9e4c212250a426d627466 Mon Sep 17 00:00:00 2001 From: raj_mathe Date: Mon, 25 Oct 2021 11:27:33 +0200 Subject: [PATCH] master > master: code - designfehler mit `index == -1` korrigiert --- code/algorithms/search/binary.py | 4 +++- code/algorithms/search/jump.py | 8 ++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/code/algorithms/search/binary.py b/code/algorithms/search/binary.py index 3ef9901..c6762ab 100644 --- a/code/algorithms/search/binary.py +++ b/code/algorithms/search/binary.py @@ -58,4 +58,6 @@ def BinarySearch(L: List[int], x: int) -> int: else: # x > L[m] logDebug('Suche in L[m+1], L[m+2], ..., L[len(L)-1] fortsetzen, m = {}.'.format(m)); index = BinarySearch(L=L[m+1:], x=x); - return (m + 1) + index; # NOTE: muss Indexwert kompensieren + if index >= 0: + index += (m + 1); # NOTE: muss Indexwert kompensieren + return index; diff --git a/code/algorithms/search/jump.py b/code/algorithms/search/jump.py index 3721463..a579a90 100644 --- a/code/algorithms/search/jump.py +++ b/code/algorithms/search/jump.py @@ -56,7 +56,9 @@ def JumpSearchLinear(L: List[int], x: int, m: int) -> int: if x < elementAfterBlock: logDebug('Element muss sich im Block [{i0}, {i1}) befinden.'.format(i0 = i*m, i1 = (i+1)*m)); index = SequentialSearch(L=block, x=x); - return offset + index; # NOTE: muss wegen Offset kompensieren + if index >= 0: + index += offset; # NOTE: muss wegen Offset kompensieren + return index; logDebug('Element befindet sich nicht im im Block [{i0}, {i1}) befinden.'.format(i0 = i*m, i1 = (i+1)*m)); i += 1; return -1; @@ -85,7 +87,9 @@ def JumpSearchExponentiell(L: List[int], x: int) -> int: if x < elementAfterBlock: logDebug('Element muss sich im Block [{i0}, {i1}) befinden.'.format(i0 = i0, i1 = i1)); index = SequentialSearch(L=block, x=x); - return i0 + index; # NOTE: muss wegen Offset kompensieren + if index >= 0: + index += i0; # NOTE: muss wegen Offset kompensieren + return index; logDebug('Element befindet sich nicht im Block [{i0}, {i1}) befinden.'.format(i0 = i0, i1 = i1)); i0 = i1; i1 *= 2;