CKTIS / Python
Zmogljivost in zahtevnost
Preučuje rast dela z velikostjo vhoda neodvisno od izmerjenega časa.
Različica: Python 3.14; repository image, production runtime not attested
Sintaksa
bisect_left(sorted_values, target)Preučuje rast dela z velikostjo vhoda neodvisno od izmerjenega časa.
Obhod lahko prebere celoten seznam, binarno iskanje v urejenih podatkih pa razpolavlja interval in potrebuje logaritemsko število primerjav. Indeks za vstavljanje zahteva še preverjanje meje in enakosti.
Razvrščanje in vstavljanje v seznam imata ločene stroške.
Primeri
from bisect import bisect_left
values = list(range(0, 32, 2))
def lookup(target):
reads = 0
def key(x):
nonlocal reads
reads += 1
return x
index = bisect_left(values, target, key=key)
found = index < len(values) and values[index] == target
return found, reads
print(lookup(30), lookup(31))
def scan(target):
checks = 0
for item in values:
checks += 1
if item == target:
return True, checks
return False, checks
print(scan(30), scan(31))
Rezultat
(True, 4) (False, 4) (True, 16) (False, 16)
Razvrščanje in vstavljanje v seznam imata ločene stroške.