i want a code for bubble sort with explanation
Answers
Answered by
1
Bubble sort is the simplest sorting algorithm that works by repeat lyrics swapping the adjacent elements if they are in wrong order.
example:
First pass:(51428)-(15428),Here,algorithm compares the first two elements, and swaps sing 5-1. (15428)-(14258),swap since 5-4, (14528)-(14258),swap since 5-2, (14258)-(14258), Now since these elements are already in order (8-5), algorithm does not swap them.
second pass:
(14258)-(14258), (14258)-(12458),swap since 4-2 (12458)-(12458), (12458)-(12458)Now,the array is already stored, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is stored.
third pass:(12458)-(12458)
(12458)-(12458)
(12458)-(12458)
(12458)-(12458)
example:
First pass:(51428)-(15428),Here,algorithm compares the first two elements, and swaps sing 5-1. (15428)-(14258),swap since 5-4, (14528)-(14258),swap since 5-2, (14258)-(14258), Now since these elements are already in order (8-5), algorithm does not swap them.
second pass:
(14258)-(14258), (14258)-(12458),swap since 4-2 (12458)-(12458), (12458)-(12458)Now,the array is already stored, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is stored.
third pass:(12458)-(12458)
(12458)-(12458)
(12458)-(12458)
(12458)-(12458)
Similar questions