aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/algorithms/impl
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2016-06-24 01:54:13 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2016-06-24 01:54:13 +0300
commit82a674e409fce161299efeb43e1176f869af64af (patch)
tree3b544bd96688f3847233e011452c754c38755116 /algorithms/impl
parentadd Pylint configuration (diff)
downloadsorting-algorithms-82a674e409fce161299efeb43e1176f869af64af.tar.gz
sorting-algorithms-82a674e409fce161299efeb43e1176f869af64af.zip
major refactoring
With the focus on (re)usability. That includes adding separate modules for plotting, input generation and things like that.
Diffstat (limited to '')
-rw-r--r--algorithms/impl/__init__.py26
-rw-r--r--algorithms/impl/bubble_sort.py28
-rw-r--r--algorithms/impl/heapsort.py23
-rw-r--r--algorithms/impl/insertion_sort.py23
-rw-r--r--algorithms/impl/median.py32
-rw-r--r--algorithms/impl/merge_sort.py23
-rw-r--r--algorithms/impl/quicksort.py31
-rw-r--r--algorithms/impl/selection_sort.py23
8 files changed, 132 insertions, 77 deletions
diff --git a/algorithms/impl/__init__.py b/algorithms/impl/__init__.py
index 29cd51c..dcb8fc4 100644
--- a/algorithms/impl/__init__.py
+++ b/algorithms/impl/__init__.py
@@ -2,18 +2,16 @@
# This file is licensed under the terms of the MIT License.
# See LICENSE.txt for details.
-_ALL_ALGORITHMS = {}
+from importlib import import_module
+import os.path
+from pkgutil import iter_modules
-def _refresh_algorithms():
- _ALGORITHMS_NAME = '_ALGORITHMS'
- global _ALL_ALGORITHMS
- _ALL_ALGORITHMS = {}
+from .. import algorithm
- from algorithms.algorithm import Algorithm
+_ALGORITHMS_NAME = '_ALGORITHMS'
- from importlib import import_module
- import os.path
- from pkgutil import iter_modules
+def refresh_algorithms():
+ all_algorithms = {}
for _, module_name, is_pkg in iter_modules([os.path.dirname(__file__)]):
if is_pkg:
@@ -21,9 +19,9 @@ def _refresh_algorithms():
module = import_module('.' + module_name, __package__)
if hasattr(module, _ALGORITHMS_NAME):
module_algorithms = getattr(module, _ALGORITHMS_NAME)
- for algorithm in module_algorithms:
- assert isinstance(algorithm, Algorithm)
- assert algorithm.get_codename() not in _ALL_ALGORITHMS
- _ALL_ALGORITHMS[algorithm.get_codename()] = algorithm
+ for descr in module_algorithms:
+ assert isinstance(descr, algorithm.Algorithm)
+ assert descr.codename not in all_algorithms
+ all_algorithms[descr.codename] = descr
-_refresh_algorithms()
+ return all_algorithms
diff --git a/algorithms/impl/bubble_sort.py b/algorithms/impl/bubble_sort.py
index 2abfc43..e6aa645 100644
--- a/algorithms/impl/bubble_sort.py
+++ b/algorithms/impl/bubble_sort.py
@@ -2,6 +2,10 @@
# This file is licensed under the terms of the MIT License.
# See LICENSE.txt for details.
+import sys
+
+from ..algorithm import SortingAlgorithm
+
def bubble_sort(xs):
while True:
swapped = False
@@ -14,7 +18,7 @@ def bubble_sort(xs):
return xs
def bubble_sort_optimized(xs):
- n = len(xs)
+ n = len(xs)
while True:
new_n = 0
for i in range(1, n):
@@ -26,14 +30,18 @@ def bubble_sort_optimized(xs):
break
return xs
-if __name__ == '__main__':
- import sys
- xs = list(map(int, sys.argv[1:]))
+_ALGORITHMS = [
+ SortingAlgorithm('bubble_sort', 'Bubble sort', bubble_sort),
+ SortingAlgorithm('bubble_sort_optimized', 'Bubble sort (optimized)', bubble_sort_optimized),
+]
+
+def _parse_args(args=sys.argv):
+ return list(map(int, args[1:]))
+
+def main(args=sys.argv):
+ xs = _parse_args(args)
print(bubble_sort(list(xs)))
print(bubble_sort_optimized(list(xs)))
-else:
- from algorithms.algorithm import SortingAlgorithm
- _ALGORITHMS = [
- SortingAlgorithm('bubble_sort', 'Bubble sort', bubble_sort),
- SortingAlgorithm('bubble_sort_optimized', 'Bubble sort (optimized)', bubble_sort_optimized),
- ]
+
+if __name__ == '__main__':
+ main()
diff --git a/algorithms/impl/heapsort.py b/algorithms/impl/heapsort.py
index db3b6bd..c92a04c 100644
--- a/algorithms/impl/heapsort.py
+++ b/algorithms/impl/heapsort.py
@@ -2,6 +2,10 @@
# This file is licensed under the terms of the MIT License.
# See LICENSE.txt for details.
+import sys
+
+from ..algorithm import SortingAlgorithm
+
# Disclaimer: implemented in the most literate way.
def heapsort(xs):
@@ -49,11 +53,16 @@ def _siftdown(xs, start, end):
else:
break
+_ALGORITHMS = [
+ SortingAlgorithm('heapsort', 'Heapsort', heapsort),
+]
+
+def _parse_args(args=sys.argv):
+ return list(map(int, args[1:]))
+
+def main(args=sys.argv):
+ xs = _parse_args(args)
+ print(heapsort(list(xs)))
+
if __name__ == '__main__':
- import sys
- print(heapsort(list(map(int, sys.argv[1:]))))
-else:
- from algorithms.algorithm import SortingAlgorithm
- _ALGORITHMS = [
- SortingAlgorithm('heapsort', 'Heapsort', heapsort),
- ]
+ main()
diff --git a/algorithms/impl/insertion_sort.py b/algorithms/impl/insertion_sort.py
index f671712..006ab66 100644
--- a/algorithms/impl/insertion_sort.py
+++ b/algorithms/impl/insertion_sort.py
@@ -2,6 +2,10 @@
# This file is licensed under the terms of the MIT License.
# See LICENSE.txt for details.
+import sys
+
+from ..algorithm import SortingAlgorithm
+
def insertion_sort(xs):
for i in range(1, len(xs)):
j = i
@@ -10,11 +14,16 @@ def insertion_sort(xs):
j -= 1
return xs
+_ALGORITHMS = [
+ SortingAlgorithm('insertion_sort', 'Insertion sort', insertion_sort),
+]
+
+def _parse_args(args=sys.argv):
+ return list(map(int, args[1:]))
+
+def main(args=sys.argv):
+ xs = _parse_args(args)
+ print(insertion_sort(list(xs)))
+
if __name__ == '__main__':
- import sys
- print(insertion_sort(list(map(int, sys.argv[1:]))))
-else:
- from algorithms.algorithm import SortingAlgorithm
- _ALGORITHMS = [
- SortingAlgorithm('insertion_sort', 'Insertion sort', insertion_sort),
- ]
+ main()
diff --git a/algorithms/impl/median.py b/algorithms/impl/median.py
index ba51c71..e6b9901 100644
--- a/algorithms/impl/median.py
+++ b/algorithms/impl/median.py
@@ -2,9 +2,11 @@
# This file is licensed under the terms of the MIT License.
# See LICENSE.txt for details.
-from algorithms.impl.quicksort import quicksort_random
+from heapq import heappush, heappop
+import sys
-from heapq import *
+from ..algorithm import Algorithm
+from .quicksort import quicksort_random
def calc_median_heaps(xs):
cur_median = 0.0
@@ -30,7 +32,7 @@ def calc_median_heaps(xs):
cur_median = min_heap[0]
return cur_median
-def calc_median_sort_first(xs):
+def calc_median_sorting(xs):
if not xs:
return 0.0
quicksort_random(xs)
@@ -39,14 +41,18 @@ def calc_median_sort_first(xs):
else:
return xs[len(xs) // 2 - 1] / 2 + xs[len(xs) // 2] / 2
-if __name__ == '__main__':
- import sys
- xs = list(map(int, sys.argv[1:]))
- print(calc_median_sort_first(list(xs)))
+_ALGORITHMS = [
+ Algorithm('median_sorting', 'Median value (using explicit sorting)', calc_median_sorting),
+ Algorithm('median_heaps', 'Median value (using heaps)', calc_median_heaps),
+]
+
+def _parse_args(args=sys.argv):
+ return list(map(int, args[1:]))
+
+def main(args=sys.argv):
+ xs = _parse_args(args)
+ print(calc_median_sorting(list(xs)))
print(calc_median_heaps(list(xs)))
-else:
- from algorithms.algorithm import Algorithm
- _ALGORITHMS = [
- Algorithm('median_sort_first', 'Median (input is sorted first)', calc_median_sort_first),
- Algorithm('median_heaps', 'Median (using heaps)', calc_median_heaps),
- ]
+
+if __name__ == '__main__':
+ main()
diff --git a/algorithms/impl/merge_sort.py b/algorithms/impl/merge_sort.py
index 9fa96ec..8d0b573 100644
--- a/algorithms/impl/merge_sort.py
+++ b/algorithms/impl/merge_sort.py
@@ -2,6 +2,10 @@
# This file is licensed under the terms of the MIT License.
# See LICENSE.txt for details.
+import sys
+
+from ..algorithm import SortingAlgorithm
+
def merge(left, right):
result = []
l, r = 0, 0
@@ -24,11 +28,16 @@ def merge_sort(xs):
mid = len(xs) // 2
return merge(merge_sort(xs[:mid]), merge_sort(xs[mid:]))
+_ALGORITHMS = [
+ SortingAlgorithm('merge_sort', 'Merge sort', merge_sort),
+]
+
+def _parse_args(args=sys.argv):
+ return list(map(int, args[1:]))
+
+def main(args=sys.argv):
+ xs = _parse_args(args)
+ print(merge_sort(list(xs)))
+
if __name__ == '__main__':
- import sys
- print(merge_sort(list(map(int, sys.argv[1:]))))
-else:
- from algorithms.algorithm import SortingAlgorithm
- _ALGORITHMS = [
- SortingAlgorithm('merge_sort', 'Merge sort', merge_sort),
- ]
+ main()
diff --git a/algorithms/impl/quicksort.py b/algorithms/impl/quicksort.py
index 32100b0..ddc8269 100644
--- a/algorithms/impl/quicksort.py
+++ b/algorithms/impl/quicksort.py
@@ -3,6 +3,9 @@
# See LICENSE.txt for details.
from random import randrange
+import sys
+
+from ..algorithm import SortingAlgorithm
def _partition(xs, beg, end, select_pivot):
pivot = select_pivot(xs, beg, end)
@@ -55,20 +58,24 @@ def quicksort_random(xs):
_quicksort(xs, 0, len(xs) - 1, _select_random)
return xs
-if __name__ == '__main__':
- import sys
- xs = list(map(int, sys.argv[1:]))
+_ALGORITHMS = [
+ SortingAlgorithm('quicksort_first', 'Quicksort (first element as pivot)', quicksort_first),
+ SortingAlgorithm('quicksort_second', 'Quicksort (second element as pivot)', quicksort_second),
+ SortingAlgorithm('quicksort_middle', 'Quicksort (middle element as pivot)', quicksort_middle),
+ SortingAlgorithm('quicksort_last', 'Quicksort (last element as pivot)', quicksort_last),
+ SortingAlgorithm('quicksort_random', 'Quicksort (random element as pivot)', quicksort_random),
+]
+
+def _parse_args(args=sys.argv):
+ return list(map(int, args[1:]))
+
+def main(args=sys.argv):
+ xs = _parse_args(args)
print(quicksort_first(list(xs)))
print(quicksort_second(list(xs)))
print(quicksort_middle(list(xs)))
print(quicksort_last(list(xs)))
print(quicksort_random(list(xs)))
-else:
- from algorithms.algorithm import SortingAlgorithm
- _ALGORITHMS = [
- SortingAlgorithm('quicksort_first', 'Quicksort (first element as pivot)', quicksort_first),
- SortingAlgorithm('quicksort_second', 'Quicksort (second element as pivot)', quicksort_second),
- SortingAlgorithm('quicksort_middle', 'Quicksort (middle element as pivot)', quicksort_middle),
- SortingAlgorithm('quicksort_last', 'Quicksort (last element as pivot)', quicksort_last),
- SortingAlgorithm('quicksort_random', 'Quicksort (random element as pivot)', quicksort_random),
- ]
+
+if __name__ == '__main__':
+ main()
diff --git a/algorithms/impl/selection_sort.py b/algorithms/impl/selection_sort.py
index 27b319f..d5d11d2 100644
--- a/algorithms/impl/selection_sort.py
+++ b/algorithms/impl/selection_sort.py
@@ -2,6 +2,10 @@
# This file is licensed under the terms of the MIT License.
# See LICENSE.txt for details.
+import sys
+
+from ..algorithm import SortingAlgorithm
+
def selection_sort(xs):
for i in range(len(xs) - 1):
min_i = i
@@ -12,11 +16,16 @@ def selection_sort(xs):
xs[i], xs[min_i] = xs[min_i], xs[i]
return xs
+_ALGORITHMS = [
+ SortingAlgorithm('selection_sort', 'Selection sort', selection_sort),
+]
+
+def _parse_args(args=sys.argv):
+ return list(map(int, args[1:]))
+
+def main(args=sys.argv):
+ xs = _parse_args(args)
+ print(selection_sort(list(xs)))
+
if __name__ == '__main__':
- import sys
- print(selection_sort(list(map(int, sys.argv[1:]))))
-else:
- from algorithms.algorithm import SortingAlgorithm
- _ALGORITHMS = [
- SortingAlgorithm('selection_sort', 'Selection sort', selection_sort),
- ]
+ main()