From d6de118675a192ce6e01574eb4eb2541db7aca7a Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 6 May 2015 06:14:54 +0300 Subject: initial commit --- merge_sort.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 merge_sort.py (limited to 'merge_sort.py') diff --git a/merge_sort.py b/merge_sort.py new file mode 100644 index 0000000..6d5403d --- /dev/null +++ b/merge_sort.py @@ -0,0 +1,29 @@ +# Copyright 2015 Egor Tensin +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +def merge(left, right): + result = [] + l, r = 0, 0 + while l < len(left) and r < len(right): + if left[l] <= right[r]: + result.append(left[l]) + l += 1 + else: + result.append(right[r]) + r += 1 + if left: + result.extend(left[l:]) + if right: + result.extend(right[r:]) + return result + +def merge_sort(xs): + if len(xs) < 2: + return xs + mid = len(xs) // 2 + return merge(merge_sort(xs[:mid]), merge_sort(xs[mid:])) + +if __name__ == '__main__': + import sys + print(merge_sort(list(map(int, sys.argv[1:])))) -- cgit v1.2.3