occurs when there is no sorting required,
i.e. when the given array itself is sorted. The best-case time complexity of merge
sort would be
O(n*log n).
Average Case Complexity –
This case will occur when the elements of array are
in a jumbled order, Which means that the elements are neither
properly
ascending nor properly descending. The time complexity of merge sort for
average case is
O(n*log n).
Worst Case Complexity –
The worst case occurs when the array elements are
required to be sorted in the reverse order. That means suppose you have to sort
the array elements in ascending order, but its elements in the array are in a
descending order. The complexity in this case would be
O(n*log n).
Space Complexity
The space complexity of merge sort is O(n). It is because we require an extra
variable for swapping in merge sort.
Average Case Time Complexity –
The average case occurs when the elements of the
array are in jumbled order that is not properly ascending or not properly descending. In
this case the time complexity of quicksort algorithm would be
O(n*logn).
Worst Case Time Complexity –
In quick sort, the worst case will occur when the pivot
element is either the greatest or the smallest element of the array. Suppose that the pivot
element is always the last element of the array, the worst case would occur when the given
array is sorted already in an ascending or the descending order. The
time complexity of
quicksort in the worst-case is
O(n
2
).
2. Space Complexity
The space complexity for the quicksort algorithm is