Page 59 - Touchcode_C8_Flipbook
P. 59

7.  The  first  round  of  list  comparison  is  finished,  and  you  can  see  that  6  is  the  largest
                     number, and is arranged at the last position.
                 8.  We again start from the beginning and compare 2 with 5. Since 5 is greater than 2, no
                     change is made.


                                               2         5        4         3        6

                 9.  Next, we compare 5 and 4; Since 5 is greater than 4, we will swap these two numbers.


                                               2         4        5         3        6

                 10. Next, we compare 5 and 3; Since 5 is greater than 3, we will swap these two numbers.


                                               2         4        3         5        6
                 11. Lastly, we compare 5 and 6; Since 6 is greater than 5, no change is made.


                                               2         4        3         5        6

                 12. The second round of list comparison is finished, and you can see that 5 and 6 is the
                     largest number, and is arranged at the second last and last position.

                 13. We again start from the beginning and compare 2 with 4. Since 4 is greater than 2,
                     no change is made.

                                               2         4        3         5        6

                 14. Next, we compare 4 and 3; Since 4 is greater than 3, we will swap these two numbers.


                                               2         3        4         5        6

                 15. Next, we compare 4 and 5; Since 5 is greater than 4, no change is made.

                                               2         3        4         5        6

                 16. Next, we compare 5 and 6; Since 6 is greater than 5, no change is made.


                                               2         3        4         5        6

                 17. We again start from the beginning and compare 2 with 3; 3 with 4; 4 with 5; and 5 with 6.
                     Since 3 is greater than 2; 4 is greater than 3; 5 is greater than 4; 6 is greater than 5. So,
                     no change is made.
                 18. The numbers are now all sorted in ascending order.


                                               2         3        4         5        6
                 19. The exercise of sorting is done until no more numbers need to be swapped.




                                                                                     Programming with Arrays     57
   54   55   56   57   58   59   60   61   62   63   64