Now , we have the code of Bubble sort right here :

It generates a list(unsorted) of numbers :

17 , 18, 10, 1, 20, 25, 4, 9, 13, 15

Here are 10 number,therefore 9 pairs right ?

Ok, now how does Bubble sort works ?

Ok, you got the idea by now.Now we will analyse the code,but lets tweek the code a bit :

I have only added the marked lines:

Now, as we run the code , the output ?

Ok , now in the output we have list of 10 unsorted numbers , some A,B,C’s & the sorted list .Why does the A,B,C’s appear in group’s for 6 times? Because according to bubble sorted we required 6 iterations to complete the sorting, such that goals[i]>goals[i+1].

In first group in the code.Now why does B appears for 9 times ? because there are 9 pairs for 10 numbers.C 9 times? as simple as that.

Now, lets look the first 3 groups !

A =0 all the time cause, each time while runs sorted again becomes =0.

Now B=0 for first line, first group ? cause it doesnt enters if statement as 17<18 & sorted still =0.

Now B=1 for second line, second group ?cause now it enters if statement , as 18>10 ,now sorted =1 inside the loop.(sorting is done)

Now list becomes :

17

10

18

1

..

Now ,

Now B=1 for third line, third group ?cause now it enters if statement , as 18>1 ,now sorted =1 inside the loop.

Now list becomes :

17

10

1

18

20

..

Now B=1 for fourth line, fourth group ? cause it doesnt enters if statement , neither it is assigned = 0, cause sorted was 1 last time the loop runned ,sorted still =1.

.. Goes on.