Page 342 - ComputerScience_Class_11
P. 342

Selection Sort
              Using this technique, the smallest element is first found and then the first element is interchanged with the smallest
              element. Thus,  the first element after  one iteration  is  the smallest. Then  the second  element is  compared and
              interchanged with the second smallest element and swapped if required. Selection Sort is slightly better than bubble
              sort because it makes fewer swaps. This procedure keeps on continuing with every element being compared, till the
              list finally gets sorted.

              Let us take the following example.
              The array is to be sorted in ascending order:

                                   Index        0           1           2          3           4
                                    Ar          27          16         12          10          21
              Step 1:  Find the smallest element.

                                   Index        0           1           2          3           4
                                    Ar          27          16         12          10          21
                      The smallest element is in position ar[3]: 10. Interchange this number with the first element.

                      After swapping ar[0] and ar[3], the array becomes:

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

                      After the first step, the smallest element is stored in the first position of the array.
              Step 2:  Find the next smallest number.

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

                      The second smallest element is in position ar[2]: 12. Interchange this number with the second element.
                      After swapping ar[1] and ar[2], the array becomes:

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

                      After the second step, the second smallest element is stored in the second position of the array.
              Step 3:  Find the next smallest number.

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

                      The third smallest element is in position ar[2]: 16. Interchange this number with the third element.
                      After the third step, the third smallest element is already stored in the third position of the array.

              Step 4:  Find the next smallest number.

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

                      The fourth smallest element is in position ar[4]: 21. Interchange this number with the fourth element.
                      After swapping ar[3] and ar[4], the array becomes:




                  340  Touchpad Computer Science (Ver. 3.0)-XI
   337   338   339   340   341   342   343   344   345   346   347