diff options
Diffstat (limited to 'algorithms')
-rw-r--r-- | algorithms/algorithm.py | 2 | ||||
-rw-r--r-- | algorithms/impl/bubble_sort.py | 8 | ||||
-rw-r--r-- | algorithms/impl/heapsort.py | 8 | ||||
-rw-r--r-- | algorithms/impl/insertion_sort.py | 8 | ||||
-rw-r--r-- | algorithms/impl/median.py | 8 | ||||
-rw-r--r-- | algorithms/impl/merge_sort.py | 8 | ||||
-rw-r--r-- | algorithms/impl/quicksort.py | 8 | ||||
-rw-r--r-- | algorithms/impl/selection_sort.py | 8 | ||||
-rw-r--r-- | algorithms/inputgen.py | 2 | ||||
-rw-r--r-- | algorithms/params.py | 6 | ||||
-rw-r--r-- | algorithms/timer.py | 3 |
11 files changed, 42 insertions, 27 deletions
diff --git a/algorithms/algorithm.py b/algorithms/algorithm.py index 2d793bc..673a525 100644 --- a/algorithms/algorithm.py +++ b/algorithms/algorithm.py @@ -13,7 +13,7 @@ class Algorithm: @staticmethod def gen_input(n, case=inputgen.InputKind.AVERAGE): - #raise NotImplementedError('inputgen generation is not defined for generic algorithms') + #raise NotImplementedError('input generation is not defined for generic algorithms') return inputgen.gen_input_for_sorting(n, case) class SortingAlgorithm(Algorithm): diff --git a/algorithms/impl/bubble_sort.py b/algorithms/impl/bubble_sort.py index e75ceab..95fb661 100644 --- a/algorithms/impl/bubble_sort.py +++ b/algorithms/impl/bubble_sort.py @@ -36,10 +36,12 @@ _ALGORITHMS = [ SortingAlgorithm('bubble_sort_optimized', 'Bubble sort (optimized)', bubble_sort_optimized), ] -def _parse_args(args=sys.argv): - return list(map(int, args[1:])) +def _parse_args(args=None): + if args is None: + args = sys.argv[1:] + return list(map(int, args)) -def main(args=sys.argv): +def main(args=None): xs = _parse_args(args) print(bubble_sort(list(xs))) print(bubble_sort_optimized(list(xs))) diff --git a/algorithms/impl/heapsort.py b/algorithms/impl/heapsort.py index 6615e14..bf9f464 100644 --- a/algorithms/impl/heapsort.py +++ b/algorithms/impl/heapsort.py @@ -58,10 +58,12 @@ _ALGORITHMS = [ SortingAlgorithm('heapsort', 'Heapsort', heapsort), ] -def _parse_args(args=sys.argv): - return list(map(int, args[1:])) +def _parse_args(args=None): + if args is None: + args = sys.argv[1:] + return list(map(int, args)) -def main(args=sys.argv): +def main(args=None): xs = _parse_args(args) print(heapsort(list(xs))) diff --git a/algorithms/impl/insertion_sort.py b/algorithms/impl/insertion_sort.py index d02b970..1abbf84 100644 --- a/algorithms/impl/insertion_sort.py +++ b/algorithms/impl/insertion_sort.py @@ -19,10 +19,12 @@ _ALGORITHMS = [ SortingAlgorithm('insertion_sort', 'Insertion sort', insertion_sort), ] -def _parse_args(args=sys.argv): - return list(map(int, args[1:])) +def _parse_args(args=None): + if args is None: + args = sys.argv[1:] + return list(map(int, args)) -def main(args=sys.argv): +def main(args=None): xs = _parse_args(args) print(insertion_sort(list(xs))) diff --git a/algorithms/impl/median.py b/algorithms/impl/median.py index d19dec4..b48e511 100644 --- a/algorithms/impl/median.py +++ b/algorithms/impl/median.py @@ -47,10 +47,12 @@ _ALGORITHMS = [ Algorithm('median_heaps', 'Median value (using heaps)', calc_median_heaps), ] -def _parse_args(args=sys.argv): - return list(map(int, args[1:])) +def _parse_args(args=None): + if args is None: + args = sys.argv[1:] + return list(map(int, args)) -def main(args=sys.argv): +def main(args=None): xs = _parse_args(args) print(calc_median_sorting(list(xs))) print(calc_median_heaps(list(xs))) diff --git a/algorithms/impl/merge_sort.py b/algorithms/impl/merge_sort.py index 3dd2bb9..2b96d21 100644 --- a/algorithms/impl/merge_sort.py +++ b/algorithms/impl/merge_sort.py @@ -33,10 +33,12 @@ _ALGORITHMS = [ SortingAlgorithm('merge_sort', 'Merge sort', merge_sort), ] -def _parse_args(args=sys.argv): - return list(map(int, args[1:])) +def _parse_args(args=None): + if args is None: + args = sys.argv[1:] + return list(map(int, args)) -def main(args=sys.argv): +def main(args=None): xs = _parse_args(args) print(merge_sort(list(xs))) diff --git a/algorithms/impl/quicksort.py b/algorithms/impl/quicksort.py index 3c4715a..1e835c4 100644 --- a/algorithms/impl/quicksort.py +++ b/algorithms/impl/quicksort.py @@ -69,10 +69,12 @@ _ALGORITHMS = [ SortingAlgorithm('quicksort_random', 'Quicksort (random element as pivot)', quicksort_random), ] -def _parse_args(args=sys.argv): - return list(map(int, args[1:])) +def _parse_args(args=None): + if args is None: + args = sys.argv[1:] + return list(map(int, args)) -def main(args=sys.argv): +def main(args=None): xs = _parse_args(args) print(quicksort_first(list(xs))) print(quicksort_second(list(xs))) diff --git a/algorithms/impl/selection_sort.py b/algorithms/impl/selection_sort.py index 0dbf4eb..ad4a420 100644 --- a/algorithms/impl/selection_sort.py +++ b/algorithms/impl/selection_sort.py @@ -21,10 +21,12 @@ _ALGORITHMS = [ SortingAlgorithm('selection_sort', 'Selection sort', selection_sort), ] -def _parse_args(args=sys.argv): - return list(map(int, args[1:])) +def _parse_args(args=None): + if args is None: + args = sys.argv[1:] + return list(map(int, args)) -def main(args=sys.argv): +def main(args=None): xs = _parse_args(args) print(selection_sort(list(xs))) diff --git a/algorithms/inputgen.py b/algorithms/inputgen.py index aeb4f29..998f1d6 100644 --- a/algorithms/inputgen.py +++ b/algorithms/inputgen.py @@ -20,7 +20,7 @@ def _gen_input_from(xs): def gen_input_for_sorting(n, case=InputKind.AVERAGE): if n < 0: - raise ValueError('input length must not be a negative number') + raise ValueError('input length cannot be less than zero') if case is InputKind.BEST: return _gen_input_from(range(n)) elif case is InputKind.AVERAGE: diff --git a/algorithms/params.py b/algorithms/params.py index b7fc38e..bf87976 100644 --- a/algorithms/params.py +++ b/algorithms/params.py @@ -57,7 +57,7 @@ class AlgorithmParameters: raise TypeError('must be an integral value') val = int(val) if val < 0: - raise ValueError('must not be a negative number') + raise ValueError('must be non-negative') if self.max_len is not None and self.max_len < val: raise ValueError('must not be greater than the maximum length') self._min_len = val @@ -72,7 +72,7 @@ class AlgorithmParameters: raise TypeError('must be an integral value') val = int(val) if val < 0: - raise ValueError('must not be a negative number') + raise ValueError('must be non-negative') if self.min_len is not None and self.min_len > val: raise ValueError('must not be lesser than the minimum length') self._max_len = val @@ -87,7 +87,7 @@ class AlgorithmParameters: raise TypeError('must be an integral value') val = int(val) if val < 1: - raise ValueError('must be a positive number') + raise ValueError('must be positive') self._iterations = val def measure_running_time(self): diff --git a/algorithms/timer.py b/algorithms/timer.py index 0d37812..bd5d044 100644 --- a/algorithms/timer.py +++ b/algorithms/timer.py @@ -3,7 +3,8 @@ # For details, see https://github.com/egor-tensin/sorting-algorithms. # Distributed under the MIT License. -import gc, time +import gc +import time def get_timestamp(): return time.perf_counter() |