45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
|
||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||
# IMPORTS
|
||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||
|
||
from local.maths import *;
|
||
from local.typing import *;
|
||
|
||
from code.core.log import *;
|
||
from code.algorithms.search.sequential import SequentialSearch;
|
||
|
||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||
# GLOBAL VARIABLES/CONSTANTS
|
||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||
|
||
#
|
||
|
||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||
# ALGORITHM jump search
|
||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||
|
||
def JumpSearchLinear(L: List[int], x: int, m: int) -> int:
|
||
'''
|
||
Inputs: L = Liste von Zahlen, x = Zahl, [u, v] = Suchinterval.
|
||
Annahmen:
|
||
- L sei aufsteigend sortiert.
|
||
- L enthält keine Duplikate.
|
||
Outputs: Position von x in L, sonst −1 wenn x nicht in L.
|
||
'''
|
||
i = 0;
|
||
while i*m < len(L):
|
||
AddToCounter();
|
||
offset = i*m;
|
||
block = L[offset:][:m];
|
||
elementAfterBlock = block[-1] + 1;
|
||
if x < elementAfterBlock:
|
||
logDebug('Element muss sich im Block {} befinden.'.format(i));
|
||
index = SequentialSearch(block, x);
|
||
return offset + index; # NOTE: muss wegen Offset kompensieren
|
||
logDebug('Element befindet sich nicht im Block {}.'.format(i));
|
||
i += 1;
|
||
return -1;
|