Page 376 - CA_Blue( J )_Class10
P. 376

Iteration 3:
              Step 1:
                                            index     0       1       2       3       4
                                              ar     10      44      21      51      124

              Since ar[0] > ar[1] , i.e., 10 > 44 is false.

              No swapping is done.
              Step 2:
                                            index     0       1       2       3       4
                                              ar     10      44      21      51      124

              Since ar[1] > ar[2] , i.e., 44 > 21 is true.
              Swapping the numbers in the position, the array becomes:

                                            index     0       1       2       3       4
                                              ar     10      21      44      51      124

              After the third iteration, the third largest element is placed in third last position.

              Iteration 4:
              Step 1:
                                            index     0       1       2       3       4
                                              ar     10      21      44      51      124
              Since ar[0] > ar[1] , i.e., 10 > 21 is false.

              No swapping is done.
              After the fourth iteration, the whole array is sorted.


              Selection Sort
              In this technique, the smallest element is searched and the first element is interchanged with the smallest element.
              Then the second element is interchanged with the second smallest element and so on.
              Let us take the previous example in which the array is to be sorted in ascending order:

                                            index     0       1       2       3       4
                                              ar     124     51      10      44      21

              Step 1:

                                            index     0       1       2       3       4
                                              ar     124     51      10      44      21
              The smallest element is in position ar[2] : 10

              After swapping between ar[0] and ar[2] is executed, the array becomes:
                                            index     0       1       2       3       4
                                              ar     10      51      124     44      21

              After the first step, the smallest element is stored in the first position of the array.






                374374  Touchpad Computer Applications-X
   371   372   373   374   375   376   377   378   379   380   381