Page 374 - CA_Blue( J )_Class10
P. 374

Step 3:   Check whether the element to be searched is greater than to middle element. In this case, 44 == ar[3], i.e., 44.
                     Thus, the searched element is found and the searching stopped.

              Difference Between Linear Search and Binary Search
                                     Linear Search                                 Binary Search
                     It works on both sorted and unsorted arrays.  It works only on sorted arrays.
                     It starts searching from 0th position.        It starts searching from the middle  element and
                                                                   proceeds either to the left or to the right depending on
                                                                   the value of the searched element.

              15.5.2 Sorting
              Sorting is the process of arranging the elements an array in a sequential order either ascending or descending order.
              According to syllabus, there are two types of sorting: bubble sort and selection sort.


              Bubble Sort
              In this technique, the consecutive elements are checked from the beginning. If it is to be searched in ascending order,
              then it is to be checked that the previous element is larger than the next element. If it is so, the elements in the position
              are swapped (interchanged). And this process continues.
              Let us take an example in which an array is to be sorted in ascending order:

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

              Iteration 1:
              Step 1:

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

              Since ar[0] > ar[1], i.e., 124 > 51 is true.
              Swapping the numbers in the position, the array becomes:

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

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

              Swapping the numbers in the position, the array becomes:

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

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

              Since ar[1] > ar[2] , i.e., 124 > 10 is true.



                372372  Touchpad Computer Applications-X
   369   370   371   372   373   374   375   376   377   378   379