Page 377 - CA_Blue( J )_Class10
P. 377

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

                 The second smallest element is in position ar[4] : 21
                 After swapping between ar[1] and ar[4] is executed, the array becomes:

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

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

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

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

                 After the third step, the third smallest element is stored in the third position of the array.
                 Step 4:
                                              index     0       1       2       3       4
                                                ar      10      21      44     124      51
                 The fourth smallest element is in position ar[4] : 51

                 After swapping between ar[3] and ar[4] is executed, the array becomes:

                                              index     0       1       2       3       4
                                                ar      10      21      44      51     124
                 After the fourth step, the array has been sorted in ascending order.


                 Difference between Bubble Sort and Selection Sort
                                         Bubble Sort                                 Selection Sort
                         Compares with the next element and swaps if the  Selects the minimum element from the unsorted
                         condition matches                            part of the array and places it in the next position in
                                                                      the sorted part of the array.
                         Execution is slow.                           Execution is fast.
                         Less efficient                               More efficient

                 15.5.3 Insertion
                 To insert an element in any position of the array, it is required to check whether the index is there in the array or not
                 and there should be at one empty position in the last. To do so, it is required to shift the element from the insertion
                 position to the next index value. Then the new number can be inserted in the array.
                 Let us take an example in which the number to be inserted is 20 in the index position 2.

                                              index     0       1       2       3       4
                                                ar      12      51      10      44


                                                                                                                       375
                                                                                                              Arrays   375
   372   373   374   375   376   377   378   379   380   381   382