loop and inner For loop. The inner For loop makes a pass on the list. If during the pass, any exchange(s)
is made then it is recorded in a variable called flag, i.e., flag is set to false. The outer while loop keeps
track of the flag. As soon as the flag informs that no exchange(s) took place during the current pass
indicating that the list is now sorted, the algorithm stops.
Fig. 3.14 Second pass of the bubble sort
89101119121320
89101112191320
89101112131920
Fig. 3.13 First pass of bubble sort on a list of numbers
82091011191213
89201011191213
89102011191213
89101120
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.