diff options
| author | Egor Tensin <Egor.Tensin@gmail.com> | 2016-04-17 00:49:33 +0300 |
|---|---|---|
| committer | Egor Tensin <Egor.Tensin@gmail.com> | 2016-04-17 00:49:33 +0300 |
| commit | 2183f3f860af34e45058ef078045322062b51f42 (patch) | |
| tree | 1f61c51be301db314720fe4a945d6ddaaffa8249 /bubble_sort.py | |
| parent | README update (diff) | |
| download | sorting-algorithms-2183f3f860af34e45058ef078045322062b51f42.tar.gz sorting-algorithms-2183f3f860af34e45058ef078045322062b51f42.zip | |
rearrange source files
* Add a useful `algorithms` package to provide convinient access to the
implemented algorithms.
* This allows to e.g. dynamically list available algorithms, which
greatly simplifies a lot of things.
Diffstat (limited to 'bubble_sort.py')
| -rw-r--r-- | bubble_sort.py | 33 |
1 files changed, 0 insertions, 33 deletions
diff --git a/bubble_sort.py b/bubble_sort.py deleted file mode 100644 index 8309201..0000000 --- a/bubble_sort.py +++ /dev/null @@ -1,33 +0,0 @@ -# Copyright 2015 Egor Tensin <Egor.Tensin@gmail.com> -# This file is licensed under the terms of the MIT License. -# See LICENSE.txt for details. - -def bubble_sort(xs): - while True: - swapped = False - for i in range(1, len(xs)): - if xs[i - 1] > xs[i]: - xs[i], xs[i - 1] = xs[i - 1], xs[i] - swapped = True - if not swapped: - break - return xs - -def bubble_sort_optimized(xs): - n = len(xs) - while True: - new_n = 0 - for i in range(1, n): - if xs[i - 1] > xs[i]: - xs[i], xs[i - 1] = xs[i - 1], xs[i] - new_n = i - n = new_n - if not n: - break - return xs - -if __name__ == '__main__': - import sys - xs = list(map(int, sys.argv[1:])) - print(bubble_sort(list(xs))) - print(bubble_sort_optimized(list(xs))) |
