Page 299 - Cs_withBlue_J_C11_Flipbook
P. 299

Iteration 1:
                 Step 1:  First two elements are compared.

                                     index         0          1           2           3           4

                                       Ar          2          16          12         10          21
                        Since, ar[0] < ar[1], i.e., 2 < 16 is true; the numbers are swapped.

                        Swapping the numbers in the position, the array becomes:
                                     index         0          1           2           3           4

                                       Ar         16          2           12         10          21
                 Step 2:  Next two elements are compared.

                                     Index         0          1           2           3           4

                                       Ar         16          2           12         10          21
                        Since, ar[1] < ar[2] , i.e., 2 < 12 is true; the numbers are swapped.

                        Swapping the numbers in the position, the array becomes:
                                     Index         0          1           2           3           4

                                       Ar         16          12          2          10          21
                 Step 3:  Next two elements are compared.

                                     Index         0          1           2           3           4

                                       Ar         16          12          2          10          21
                        Since, ar[2] < ar[3], i.e., 2 < 10 is true; the numbers are swapped.

                        Swapping the numbers in the position, the array becomes:
                                     Index         0          1           2           3           4

                                       Ar         16          12          10          2          21
                 Step 4:  Next two elements are compared.

                                     Index         0          1           2           3           4

                                       Ar         16          12          10          2          21
                        Since, ar[3] < ar[4], i.e., 2 < 21 is true; the numbers are swapped.
                        Swapping the numbers in the position, the array becomes:

                                     Index         0          1           2           3           4
                                       Ar         16          12          10         21           2

                 After the first iteration, the smallest element is placed in the last position.

                 Iteration 2:
                                     Index         0          1           2           3           4

                                       Ar         16          12          10         21           2





                                                                                                                       297
                                                                                                              Arrays   297
   294   295   296   297   298   299   300   301   302   303   304