aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/algorithms/impl/median.py
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/median.py
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 'algorithms/impl/median.py')
-rw-r--r--algorithms/impl/median.py32
1 files changed, 19 insertions, 13 deletions
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()