Sorting of array

which type of sorting we should prefer the most bubble ,insertion or selection ?

what is their worst and best case ?explain .

worst case for all 3 sorting algo is O(n^2) and best case for bubble(with some modification) and insertion sort is O(n) while as best case of selection sort is O(n^2)…best case time complexity in bubble and insertion sort occur when the array is already sorted…cant really comment which sorting algo you should prefer…it depends on input…for ex) if the array is almost sorted then go for insertion sort…otherwise speaking all these algorithms are equally bad in terms of their performance…internally java uses merge sort and quick sort for sorting…

please explain how it came o(n^2) for all three cases?

Hi Mansi

Look at the codes of all the three sorting algorithms carefully. For each element in the array, it does (n - 1) comparisons with all the other elements of the array. Hence, a complexity of O(n2).

You’ll get more clarity once you’ve watched Time & Space Complexity videos.

my doubt is resolved

Okay :slight_smile:

I am marking this doubt as resolved.