From 3ab450228025a6b9564727bb8828ebc0681ad2aa Mon Sep 17 00:00:00 2001 From: John Mafura <43677834+johnmaf21@users.noreply.github.com> Date: Sat, 12 Oct 2019 16:40:51 +0100 Subject: [PATCH] Added merge sort in python --- Python/MergeSort.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Python/MergeSort.py diff --git a/Python/MergeSort.py b/Python/MergeSort.py new file mode 100644 index 0000000..232e50a --- /dev/null +++ b/Python/MergeSort.py @@ -0,0 +1,32 @@ +def mergeSort(list): + + if len(list) > 1: + midPointer = len(list) // 2 + left = list[:midPointer] + right = list[midPointer:] + + mergeSort(left) + mergeSort(right) + + i = 0 + j = 0 + k = 0 + + while (i < len(left)) and (j < len(right)): + if left[i] < right[j]: + list[k] = left[i] + i += 1 + else: + list[k] = right[j] + j += 1 + k += 1 + + while i < len(left): + list[k] = left[i] + i += 1 + k += 1 + + while j < len(right): + list[k] = right[j] + j += 1 + k += 1