Page 339 - ComputerScience_Class_11
P. 339

Iteration 2:
                                     Index         0          1           2           3           4

                                       Ar         16          12          10         21           2
                 Step 1:  There is no interchange in the first three numbers as they are already in descending order. Thus, the third and
                        fourth elements are checked.

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

                        Since, ar[2] < ar[3], i.e., 10 < 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          21         10           2

                 Step 2:  The next two elements are compared.

                                     Index         0          1           2           3           4
                                       Ar         16          12          21         10           2
                        Since, ar[3] < ar[4], i.e., 10 < 2 is false; there will be no interchange.

                        With this, the second smallest number is placed in the second last position.

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

                 Step 1:  There is no interchange in the first two numbers as they are already in descending order. Thus, the second and
                        third elements are compared.

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

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

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

                 Step 2:
                                       Ar         16          21          12         10           2

                        There is no interchange in the last three numbers as they are already in descending order.

                 Iteration 4:
                 Step 1:  The first two elements are compared.

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




                                                                                                           Arrays  337
   334   335   336   337   338   339   340   341   342   343   344