diff --git a/code/algorithms/search/exports.py b/code/algorithms/search/exports.py index 6d3b841..ed8bbd7 100644 --- a/code/algorithms/search/exports.py +++ b/code/algorithms/search/exports.py @@ -9,4 +9,5 @@ from code.algorithms.search.sequential import SequentialSearch; from code.algorithms.search.binary import BinarySearch; from code.algorithms.search.interpol import InterpolationSearch; from code.algorithms.search.jump import JumpSearchLinear; -from code.algorithms.search.selection import SelectionAlgorithm; +from code.algorithms.search.ith_smallest import FindIthSmallest; +from code.algorithms.search.ith_smallest_dc import FindIthSmallestDC; diff --git a/code/algorithms/search/ith_smallest.py b/code/algorithms/search/ith_smallest.py new file mode 100644 index 0000000..fe6c4c2 --- /dev/null +++ b/code/algorithms/search/ith_smallest.py @@ -0,0 +1,65 @@ +#!/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; +from code.algorithms.methods import *; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# GLOBAL VARIABLES/CONSTANTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +# + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# CHECKS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +def preChecks(L: List[int], i: int, **_): + assert 1 <= i and i <= len(L), 'Der Wert von i muss zw. {lb} und {ub} liegen.'.format(lb = 1, ub = len(L)); + assert sorted(L) == sorted(list(set(L))), 'Ungültiger Input: L darf keine Duplikate enthalten!'; + return; + +def postChecks(L: List[int], i: int, value: int, **_): + L_ = sorted(L); + assert L_[i-1] == value, 'Der Algorithmus hat versagt.'; + return; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# ALGORITHM jump search +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +@algorithmInfos(name='Auswahlproblem (i. kleinstes Element)', outputnames='value', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks) +def FindIthSmallest(L: List[int], i: int) -> int: + ''' + Inputs: L = Liste von Zahlen, i = Ordinalzahl + + Annahmen: + + - L enthält keine Duplikate. + - L enthält mindestens i Elemente. + + Outputs: Wert des i. kleinste Element in L. + Beachte 1.kleinstes <==> Minimum. + ''' + index = 0; + minValue = L[0]; + AddToCounter(len(L)); + for i_ in range(1, len(L)): + if L[i_] < minValue: + index = i_; + minValue = L[i_]; + if i == 1: + logDebug('Das i. kleinste Element wurde gefunden.'); + return minValue; + else: + logDebug('Entfernte Minimum: {value}.'.format(value = minValue)); + i = i - 1; + return FindIthSmallest(L=L[:index] + L[(index+1):], i=i); diff --git a/code/algorithms/search/ith_smallest_dc.py b/code/algorithms/search/ith_smallest_dc.py new file mode 100644 index 0000000..a7ed6d1 --- /dev/null +++ b/code/algorithms/search/ith_smallest_dc.py @@ -0,0 +1,65 @@ +#!/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; +from code.algorithms.methods import *; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# GLOBAL VARIABLES/CONSTANTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +# + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# CHECKS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +def preChecks(L: List[int], i: int, **_): + assert 1 <= i and i <= len(L), 'Der Wert von i muss zw. {lb} und {ub} liegen.'.format(lb = 1, ub = len(L)); + assert sorted(L) == sorted(list(set(L))), 'Ungültiger Input: L darf keine Duplikate enthalten!'; + return; + +def postChecks(L: List[int], i: int, value: int, **_): + L_ = sorted(L); + assert L_[i-1] == value, 'Der Algorithmus hat versagt.'; + return; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# ALGORITHM jump search +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +@algorithmInfos(name='Auswahlproblem (i. kleinstes Element, D & C)', outputnames='value', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks) +def FindIthSmallestDC(L: List[int], i: int) -> int: + ''' + Inputs: L = Liste von Zahlen, i = Ordinalzahl + + Annahmen: + + - L enthält keine Duplikate. + - L enthält mindestens i Elemente. + + Outputs: Wert des i. kleinste Element in L. + Beachte 1.kleinstes <==> Minimum. + ''' + AddToCounter(); + p = L[len(L)-1]; # NOTE: Pivotelement kann beliebig gewählt werden + Ll = [ x for x in L if x < p ]; + Lr = [ x for x in L if x > p ]; + if len(Ll) == i - 1: + logDebug('Es gibt genau i-1 Elemente vorm Pivotelment, p. Also ist p das i. kleinste Element.'); + return p; + elif len(Ll) >= i: + logDebug('Es gibt mindestens i Elemente vorm Pivotelement, p. Suche wird in linker Hälfte fortgesetzt.'); + return FindIthSmallestDC(L=Ll, i=i); + else: + i = i - (len(Ll) + 1) + logDebug('Es gibt weniger als i-1 Elemente vorm Pivotelement, p. Suche wird in rechte Hälfte nach {i}. kleinstem Element fortgesetzt.'.format(i=i)); + return FindIthSmallestDC(L=Lr, i=i); diff --git a/code/algorithms/search/jump.py b/code/algorithms/search/jump.py index eac95f9..3a9ce48 100644 --- a/code/algorithms/search/jump.py +++ b/code/algorithms/search/jump.py @@ -24,7 +24,7 @@ from code.algorithms.methods import *; def preChecks(L: List[int], **_): assert L == sorted(L), 'Ungültiger Input: L muss aufsteigend sortiert sein!'; - assert L == list(sorted(set(L))), 'Ungültiger Input: L darf keine Duplikate enthalten!'; + assert L == sorted(list(set(L))), 'Ungültiger Input: L darf keine Duplikate enthalten!'; return; def postChecks(L: List[int], x: int, index: int, **_): diff --git a/code/algorithms/search/selection.py b/code/algorithms/search/selection.py deleted file mode 100644 index e0f9f1f..0000000 --- a/code/algorithms/search/selection.py +++ /dev/null @@ -1,46 +0,0 @@ -#!/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; -from code.algorithms.methods import *; - -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# GLOBAL VARIABLES/CONSTANTS -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -# - -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# CHECKS -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -def preChecks(L: List[int], **_): - assert L == sorted(L), 'Ungültiger Input: L muss aufsteigend sortiert sein!'; - assert L == list(sorted(set(L))), 'Ungültiger Input: L darf keine Duplikate enthalten!'; - return; - -def postChecks(L: List[int], x: int, index: int, **_): - value = L[index] if index >= 0 else None; - assert value == x, 'Der Algorithmus hat versagt.'; - return; - -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# ALGORITHM jump search -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -@algorithmInfos(name='Auswahlproblem (i. Element)', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks) -def SelectionAlgorithm(L: List[int], i: int) -> int: - ''' - Inputs: L = Liste von Zahlen, i = Ordinalzahl - Outputs: Position des i. kleinste Element, x, in L, sonst −1. Beachte i=0 <==> Minimum. - ''' - ## TODO - return -1; diff --git a/code/main.py b/code/main.py index d999728..01c27a8 100644 --- a/code/main.py +++ b/code/main.py @@ -65,6 +65,10 @@ def LoopThroughCases(path: str): InterpolationSearch(L=inputs['L'], x=inputs['x'], u=0, v=len(inputs['L'])-1); elif command == 'algorithm-search-jump': JumpSearchLinear(L=inputs['L'], x=inputs['x'], m=inputs['m']); + elif command == 'algorithm-search-ith-element': + FindIthSmallest(L=inputs['L'], i=inputs['i']); + elif command == 'algorithm-search-ith-element-dc': + FindIthSmallestDC(L=inputs['L'], i=inputs['i']); else: raise ValueError('Command \033[1m{}\033[0m nicht erkannt'.format(command)); except Exception as e: