diff options
Diffstat (limited to 'algorithms/impl/selection_sort.py')
-rw-r--r-- | algorithms/impl/selection_sort.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/algorithms/impl/selection_sort.py b/algorithms/impl/selection_sort.py new file mode 100644 index 0000000..d48c058 --- /dev/null +++ b/algorithms/impl/selection_sort.py @@ -0,0 +1,22 @@ +# 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 selection_sort(xs): + for i in range(len(xs) - 1): + min_i = i + for j in range(i + 1, len(xs)): + if xs[j] < xs[min_i]: + min_i = j + if min_i != i: + xs[i], xs[min_i] = xs[min_i], xs[i] + return xs + +if __name__ == '__main__': + import sys + print(selection_sort(list(map(int, sys.argv[1:])))) +else: + from algorithms.algorithm import SortingAlgorithm + _ALGORITHMS = [ + SortingAlgorithm('selection', 'Selection sort', selection_sort), + ] |