Explain bubble sort algorithm with suitable example
Answers
Answered by
16
let us consider n=5 elements which are to be sorted in order say 5,2,3,1,4
so now in order to sort for first time it will take only(n-1) 4 comparisons
first comparison between 5 and 2
so sorted one will become 2,5,3,1,4
second comp. between 3 and 5
change since 3 is smaller than 5
2,3,5,1,4
3rd comp. again 2,3,1,5,4
4th comp. again 2,3,1,4,5
so now number 5 is sorted
let us consider the above set of comparisons be set 1
there will be (n-1)=4 sets of comparisons
it is one less because u saw above that in first set one element is sorted
in second one 2nd element will be sorted
in third set third element will be sorted but in 4th comparison 2 elements will be sorted
hope it helps and u understand well
Answered by
1
Answer:
explain bubble sort algorithm with suitable exam
Similar questions