Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be #146

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Be #146

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DSA
Submodule DSA added at 8edf74
38 changes: 38 additions & 0 deletions Sorting/Merge Sort/Python/MergeSort.py
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 16 additions & 0 deletions Sorting/Merge Sort/algorithm.md
Original file line number Diff line number Diff line change
@@ -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<end
Step 2:set m = (start+end)/2
Step 3: Merge_sort(array,start,m)
Step 4: Merge_sort(array,m+1,end)
Step 5: merge(array,start,m,end)
Step 6: end of if
Step 7: END of Merge_sort
Step 8:Exit
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

# Sample Input::
# Input Tree :
# 1
# / \
# 6 5
# /
# 2

# Output: [1, 6, 5, 2]

# Recursive Code
# Binary Tree representation
# class TreeNode:

# def __init__(self,value):
# self.value = value
# self.left = None
# self.right = None

def preorder(root):
res = []

preorderTraversal(root, res)
return res

def preorderTraversal(root, res):

if root is None:
return

res.append(root.value)

preorderTraversal(root.left, res)

preorderTraversal(root.right, res)

return

# Time Complexity: O(n)
# Space Complexity: O(n)