Skip to content

Commit 815c6d5

Browse files
Merge pull request #292 from MkshSahani/patch-1
update merge_sort.cpp
2 parents dfdb02a + 2ba6754 commit 815c6d5

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

Searching and Sorting/Merge sort/CPP/merge_sort.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,24 @@ void Merge(int *a, int low, int high, int mid)
5353
}
5454

5555
// A function to split array into two parts.
56+
57+
/**
58+
another way to doing it by recursion
59+
contain the base case if low goes higher or equal to high
60+
than stop adding more activitaion record of copy of function to stack, and
61+
start giving the result to parent call.
62+
*/
63+
5664
void MergeSort(int *a, int low, int high)
5765
{
58-
int mid;
59-
if (low < high)
66+
if(low >= high)
6067
{
61-
mid=(low+high)/2;
62-
// Split the data into two half.
63-
MergeSort(a, low, mid);
64-
MergeSort(a, mid+1, high);
65-
66-
// Merge them to get sorted output.
67-
Merge(a, low, high, mid);
68+
return;
6869
}
70+
int mid = (low + high);
71+
MergeSort(a, low, mid);
72+
MergeSort(a, mid + 1, high);
73+
Merge(a, low, high, mid);
6974
}
7075

7176
int main()
@@ -89,4 +94,4 @@ int main()
8994
cout<<"->"<<arr[i];
9095
cout<<endl;
9196
return 0;
92-
}
97+
}

0 commit comments

Comments
 (0)