Merge Sort & Quick Sort
Disadvantages of Quick Sort:
It has a worst-case time complexity of O(N^2), which occurs when the pivot is chosen poorly. It is not a good choice for small data sets. It is not a stable sort, meaning that if two elements have the same key, their relative order will not be preserved in the sorted output in case of quick sort, because here we are swapping elements according to the pivot's position (without considering their original positions).
Advantages of Quick Sort:
It is a divide-and-conquer algorithm that makes it easier to solve problems. It is efficient on large data sets. It has a low overhead, as it only requires a small amount of memory to function.
How does Merge Sort work?
Merge sort is a recursive algorithm that continuously splits the array in half until it cannot be further divided i.e., the array has only one element left (an array with one element is always sorted). Then the sorted subarrays are merged into one sorted array.
Quick Sort Space complexity if we don't consider the recursive stack space:
O(1)
Quick Sort Space complexity If we consider the recursive stack space then in the worst case quick sort could make:
O(N)
Quick Sort Time Complexity Best and Avg Case:
O(n log n)
Quick Sort TIme Complexity Worst Case:
O(n^^2)
When is worst case time complexity possible for quick sort?
When array is sorted and pivot is smallest or largest element
Merge sort is defined as a sorting algorithm that works by
dividing an array into smaller subarrays, sorting each subarray, and then merging the sorted subarrays back together to form the final sorted array.