From 33fd706e10203cae7cc22a3365e6b2b8b0119401 Mon Sep 17 00:00:00 2001 From: RathodKinjal76 <12bce076@gmail.com> Date: Wed, 31 Oct 2018 22:23:31 +0530 Subject: [PATCH] Added merge sort algo using python --- sorting/merge_sort_algo.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 sorting/merge_sort_algo.py diff --git a/sorting/merge_sort_algo.py b/sorting/merge_sort_algo.py new file mode 100644 index 0000000..e0daaf4 --- /dev/null +++ b/sorting/merge_sort_algo.py @@ -0,0 +1,27 @@ +def partition(arr,low,high): + i = ( low-1 ) + pivot = arr[high] + + for j in range(low , high): + + if arr[j] <= pivot: + i = i+1 + arr[i],arr[j] = arr[j],arr[i] + + arr[i+1],arr[high] = arr[high],arr[i+1] + return ( i+1 ) + +# Function to do Quick sort +def quickSort(arr,low,high): + if low < high: + pi = partition(arr,low,high) + quickSort(arr, low, pi-1) + quickSort(arr, pi+1, high) + +# Driver code to test above +arr = [10, 7, 8, 9, 1, 5] +n = len(arr) +quickSort(arr,0,n-1) +print ("Sorted array is:") +for i in range(n): + print ("%d" %arr[i]), \ No newline at end of file