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 /algorithms/impl/insertion_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 'algorithms/impl/insertion_sort.py')
-rw-r--r-- | algorithms/impl/insertion_sort.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/algorithms/impl/insertion_sort.py b/algorithms/impl/insertion_sort.py new file mode 100644 index 0000000..f671712 --- /dev/null +++ b/algorithms/impl/insertion_sort.py @@ -0,0 +1,20 @@ +# 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 insertion_sort(xs): + for i in range(1, len(xs)): + j = i + while j > 0 and xs[j - 1] > xs[j]: + xs[j], xs[j - 1] = xs[j - 1], xs[j] + j -= 1 + return 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), + ] |