Suppose that you have two different algorithms for solving a
Suppose that you have two different algorithms for solving a problem. Algorithm 1 has a complexity of O(n^2 ) while Algorithm 2 has a complexity of O(n log n). Which algorithm should you use, and why?
Solution
We will use second algorithm with complexity of O(nlogn).
The time complexity of other algorithm is O(n^2).
O(nlogn) is better than O(n^2) for larger values of n.
As we know that time complexity of merge sort and quick sort is O(nlogn), while the time complexity of insertions sort, selection sort and bubble sort is O(n^2). Hence , we always prefer quick sort and meger sort over other sorting algorithms
