Page 79 - Touchcode_C7_Flipbook
P. 79

Step 2: Search for the minimum element in the list
                 Step 3: Swap the number at location Minimum number

                 Step 4: Increment the Minimum number to point it to the next element
                 Step 5: Repeat the above step 1 to 4, till the list get sorted


                                                                                                   Experiential Learning
                                                      Coding Task            01



                                                      Selection sort algorithm

                  In this task, you will learn to sort an array by using selection sort.
                  Consider an Array = {4,3,12, 8}

                  To perform the task, follow these steps:
                  Step 1: For i=0


                  (------------------------------------------------ Unsorted sub-array------------------------------------------------)
                              4                       3                       12                       8

                  You will start from 4, find the minimum element and then swap it with the 1  element of
                                                                                                       st
                  the array.
                  Step 2: For i=1

                  (--------------Sorted sub-array------------------)(--------------------Unsorted sub-array-------------------)

                              3                       4                       12                       8

                  You will start from 3, find the minimum element and then swap it with the 2   element
                                                                                                        nd
                  of the array.

                  Step 3: For i=2

                  (---------------Sorted sub-array------------------)(---------------------Unsorted sub-array-----------------)

                              3                       4                       12                       8

                  You will start from 12, find the minimum element and then swap it with the 3  element
                                                                                                         rd
                  of the array.

                  Step 4: For i=3
                  (--------------Sorted sub-array---------------------------------------------------------------------------------------)

                              3                       4                       8                       12

                  The loop is terminated as ‘I’ becomes 3. The final state of array after the loop is terminated

                  looks like:

                                                                                        Hello World with Code    77
   74   75   76   77   78   79   80   81   82   83   84