![]() |
The Time Complexity of Merge Sort is O(n log n) in both the average and worst cases. The space complexity of Merge sort is O(n).
Time Complexity Analysis of Merge Sort:Consider the following terminologies:
So, it can be written
These N/2 elements are further divided into two halves. So,
It can be divided maximum until there is one element left. So, then N/2k = 1. k = log2N
Therefore the time complexity is O(N * log2N). So in the best case, the worst case and the average case the time complexity is the same. Space Complexity Analysis of Merge Sort:Merge sort has a space complexity of O(n). This is because it uses an auxiliary array of size n to merge the sorted halves of the input array. The auxiliary array is used to store the merged result, and the input array is overwritten with the sorted result. |
Reffered: https://www.geeksforgeeks.org
Analysis Of Algorithms |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |