aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/algorithms/impl/bubble_sort.py
diff options
context:
space:
mode:
Diffstat (limited to 'algorithms/impl/bubble_sort.py')
-rw-r--r--algorithms/impl/bubble_sort.py28
1 files changed, 18 insertions, 10 deletions
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()