Bubble Sort Algorithm :
Bubble sort algorithm is a method to sort the unsorted list. In Bubble sort technique largest number reaches its correct position /index on pass one. Pass 1 requires n-1 comparisons where n is the number of elements in the array. Similarly pass 2 requires one less comparison so number of comparisons are n-2 and so on until number of pass becomes equal to n-1.
So, total number of comparisons required are -
(n-1) + (n-2) + (n-3) + ........ + 3 + 2 + 1 = n(n-1)/2
Thus, the complexity of bubble sort algorithm is O(n^2).
Example:-
Sort the following array using Bubble Sort Algorithm-
No comments:
Post a Comment