From d8810951c55212681c6536afa6f3e8170bef40cf Mon Sep 17 00:00:00 2001 From: Prachi Date: Wed, 13 Oct 2021 11:10:56 +0530 Subject: [PATCH 1/6] updated MergeSort.py and algorithm.md --- DSA | 1 + sorting/Merge Sort/Python/mergesort.py | 42 ++++++++++++++++++++++++++ sorting/Merge Sort/algorithm.md | 16 ++++++++++ 3 files changed, 59 insertions(+) create mode 160000 DSA create mode 100644 sorting/Merge Sort/Python/mergesort.py create mode 100644 sorting/Merge Sort/algorithm.md 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..8d3c715 --- /dev/null +++ b/sorting/Merge Sort/Python/mergesort.py @@ -0,0 +1,42 @@ +def merge(array,beg,m,end): + + + 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, beg, end): + if beg Date: Wed, 13 Oct 2021 13:06:20 +0530 Subject: [PATCH 2/6] Update mergesort.py --- sorting/Merge Sort/Python/mergesort.py | 32 +++++++++++--------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/sorting/Merge Sort/Python/mergesort.py b/sorting/Merge Sort/Python/mergesort.py index 8d3c715..74ab339 100644 --- a/sorting/Merge Sort/Python/mergesort.py +++ b/sorting/Merge Sort/Python/mergesort.py @@ -1,20 +1,16 @@ -def merge(array,beg,m,end): +def merge(array: [int], beg: int, m: int, end: int): + temp = [0]* (end - beg + 1) + i,j,k = beg, m + 1, 0 - - temp = [0]* (end-beg+1) - i,j,k = beg, m+1, 0 - - - while i<=m and j<=end: - if array[i]<=array[j]: + while i <= m and j <= end: + if array[i] <= array[j]: temp[k] = array[i] - k+=1 - i+=1 + k += 1 + i += 1 else: temp[k] = array[j] - k+=1 - j+=1 - + k += 1 + j += 1 while i <= m: temp[k] = array[i] @@ -27,11 +23,11 @@ def merge(array,beg,m,end): k += 1 for i in range(beg,end+1): - array[i] = temp[i-beg] + array[i] = temp[i - beg] -def Merge_sort(array, beg, end): - if beg Date: Wed, 13 Oct 2021 20:56:36 +0530 Subject: [PATCH 3/6] Rename mergesort.py to MergeSort.py --- sorting/Merge Sort/Python/{mergesort.py => MergeSort.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename sorting/Merge Sort/Python/{mergesort.py => MergeSort.py} (100%) diff --git a/sorting/Merge Sort/Python/mergesort.py b/sorting/Merge Sort/Python/MergeSort.py similarity index 100% rename from sorting/Merge Sort/Python/mergesort.py rename to sorting/Merge Sort/Python/MergeSort.py From 81f7824dce463c8e37929d8b97ae15eef4bd30c7 Mon Sep 17 00:00:00 2001 From: PrachiDadhich <60838285+PrachiDadhich@users.noreply.github.com> Date: Wed, 13 Oct 2021 21:31:46 +0530 Subject: [PATCH 4/6] Rename sorting/Merge Sort/algorithm.md to Sorting/Merge Sort/algorithm.md --- {sorting => Sorting}/Merge Sort/algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename {sorting => Sorting}/Merge Sort/algorithm.md (99%) diff --git a/sorting/Merge Sort/algorithm.md b/Sorting/Merge Sort/algorithm.md similarity index 99% rename from sorting/Merge Sort/algorithm.md rename to Sorting/Merge Sort/algorithm.md index 7a0ee77..a08f3d4 100644 --- a/sorting/Merge Sort/algorithm.md +++ b/Sorting/Merge Sort/algorithm.md @@ -13,4 +13,4 @@ Step 5: merge(array,start,m,end) Step 6: end of if Step 7: END of Merge_sort Step 8:Exit -``` \ No newline at end of file +``` From 650e9c39f23b7c6fa1e39d853836624aeb334f82 Mon Sep 17 00:00:00 2001 From: PrachiDadhich <60838285+PrachiDadhich@users.noreply.github.com> Date: Wed, 13 Oct 2021 21:33:29 +0530 Subject: [PATCH 5/6] Rename sorting/Merge Sort/Python/MergeSort.py to Sorting/Merge Sort/Python/MergeSort.py --- {sorting => Sorting}/Merge Sort/Python/MergeSort.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {sorting => Sorting}/Merge Sort/Python/MergeSort.py (100%) diff --git a/sorting/Merge Sort/Python/MergeSort.py b/Sorting/Merge Sort/Python/MergeSort.py similarity index 100% rename from sorting/Merge Sort/Python/MergeSort.py rename to Sorting/Merge Sort/Python/MergeSort.py From 6ad6dc8a74098926768bb6ac550b43bb89cb723c Mon Sep 17 00:00:00 2001 From: Prachi Date: Sun, 17 Oct 2021 23:48:12 +0530 Subject: [PATCH 6/6] Added Preorder traversal --- .../Python/PreorderTraversal.py | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Traversal Algorithms/Tree Traversals/Preorder Traveral - Binary Tree/Python/PreorderTraversal.py diff --git a/Traversal Algorithms/Tree Traversals/Preorder Traveral - Binary Tree/Python/PreorderTraversal.py b/Traversal Algorithms/Tree Traversals/Preorder Traveral - Binary Tree/Python/PreorderTraversal.py new file mode 100644 index 0000000..3350714 --- /dev/null +++ b/Traversal Algorithms/Tree Traversals/Preorder Traveral - Binary Tree/Python/PreorderTraversal.py @@ -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) + + + +