diff --git a/DSA b/DSA new file mode 160000 index 0000000..8edf746 --- /dev/null +++ b/DSA @@ -0,0 +1 @@ +Subproject commit 8edf7466498f13063b693a3bd5eab703c9bd7cad diff --git a/Sorting/Merge Sort/Python/MergeSort.py b/Sorting/Merge Sort/Python/MergeSort.py new file mode 100644 index 0000000..74ab339 --- /dev/null +++ b/Sorting/Merge Sort/Python/MergeSort.py @@ -0,0 +1,38 @@ +def merge(array: [int], beg: int, m: int, end: int): + temp = [0]* (end - beg + 1) + i,j,k = beg, m + 1, 0 + + while i <= m and j <= end: + if array[i] <= array[j]: + temp[k] = array[i] + k += 1 + i += 1 + else: + temp[k] = array[j] + k += 1 + j += 1 + + while i <= m: + temp[k] = array[i] + i += 1 + k += 1 + + while j <= end: + temp[k] = array[j] + j += 1 + k += 1 + + for i in range(beg,end+1): + array[i] = temp[i - beg] + +def Merge_sort(array: [int], beg: int, end: int): + if beg < end: + m = (beg + end)/2 + Merge_sort(array,beg,m) + Merge_sort(array,m+1,end) + merge(array,beg,m,end) + +#Sample Input: 5 14 35 20 7 2 +#Sample Output: 2 5 7 14 20 35 +#Time Complexity: θ(nLogn) in all cases(worst, average and best) +#Space Complexity: O(n) diff --git a/Sorting/Merge Sort/algorithm.md b/Sorting/Merge Sort/algorithm.md new file mode 100644 index 0000000..a08f3d4 --- /dev/null +++ b/Sorting/Merge Sort/algorithm.md @@ -0,0 +1,16 @@ +## Merge Sort +### Merge Sort is a very efficient sorting algorithm. It uses Divide and Conquer principle to sort the elements. It divides the list into two equal halves. The sub-lists are divided again and again until each sublist consists of a single element.Then merge sublists in a way that results in a sorted list. +![This is an image](https://media.geeksforgeeks.org/wp-content/cdn-uploads/Merge-Sort-Tutorial.png) + +## Algorithm +``` +Merge_sort(array,start,end) +Step 1: if start