Why are there so many sortung algorithms

what is the difference between bubble sort,insertion sort,selection sort,merge sort,quick sort except time complexity and the way they are applied,are there any special chases or exception like where bubble sort will perform better then merge sort or any of the above mention sorts.
and plz also tell which is the fastest sorting algorithm among these

@ubaidshaikh9999,

The primary use of a sorting algo is to sort an array/arraylist.

Overall if you have to sort any array, merge sort works best. Because time complexity of Merge Sort is ((n)Log(n)) in all 3 cases (worst, average and best) as merge sort always divides the array into two halves and take linear time to merge two halves. The only disadvantage is it has a space complexity of O(n).

You can also try Heap Sort because it sorts in ((n)Log(n)) in all the cases and it has a space complexity of O(1). The problem is heap sort is unstable whereas merge sort is a stable algorithm.

A stable sorting algorithm is the one that sorts the identical elements in their same order as they appear in the input, whilst unstable sorting may not satisfy the case.

Selection sort is the worst algo, you can use. (n^2) time complexity in all the cases.

When the array is almost sorted, insertion sort can be preferred.

When order of input is not known, merge sort is preferred as it has worst case time complexity of ((n)Log(n)) and it is stable as well.

When the array is sorted, insertion and bubble sort gives complexity of (n) but quick sort gives complexity of (n^2).

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.