Difference between bubble sort ,merge sort and insertion sort.
Time Complexity of Bubble sort
@vanshikasharma1645,
In bubble sort we sort the elements by repeatedly swapping the adjacent elements if they are in wrong order.
We use 2 for loops, time complexity is O(N^2) where N is size of array. Best case complexity can be O(N) when the array is sorted.
In insertion sort:
we compare the current element to its predecessor and If the element is smaller than its predecessor, compare it to the elements before. And finally place the element.
worst case and average case time complexity is O(N^2).
In merge sort:
we divide input array in two halves, call the function itself for the two halves and then merges the two sorted halves.
Time complexity is O(NLogN). The best sorting algorithm if the array is not sorted.