Merge Sort Time and Space Complexity

后端 未结 7 1496
慢半拍i
慢半拍i 2020-12-04 14:47

Let\'s take this implementation of Merge Sort as an example

void mergesort(Item a[], int l, int r) {
if (r <= l) return;
int m = (r+l)/2;
mergesort(a, l,          


        
7条回答
  •  半阙折子戏
    2020-12-04 14:57

    merge sort space complexity is O(nlogn), this is quite obvious considering that it can go to at maximum of O(logn) recursions and for each recursion there is additional space of O(n) for storing the merged array that needs to be reassigned. For those who are saying O(n) please don't forget that it is O(n) for reach stack frame depth.

提交回复
热议问题