Page 274 - computer science (868) class 11
P. 274

25                       break;
                26                   }

                27                   else if(ar[mid].compareTo(ns)>0)
                28                          max= mid - 1;

                29                   else
                30                          min=mid+1;

                31               }
                32               if(pos!=-1)
                33                   System.out.println(ns + " found in position "+ (mid+1));

                34               else

                35                   System.out.println(ns + " not found");
                36           }
                37       }

              The output of the preceding program is as follows:
              How many names do you want to enter? 4
              Enter a name: Deepak
              Enter a name: Amit
              Enter a name: Ankit
              Enter a name: Sonu

              Enter a name to search: Ankit
              Ankit found in position 3

              10.4.2 Sorting
              Sorting is the process of arranging the elements either in ascending or descending order. Once sorted, we can see that
              the elements of the array are placed in such an order, that they lie from either small to big or from big to small. In this
              class, we shall learn about the three types of sorting:
              •  Bubble sort
              •  Selection sort
              •  Insertion sort


              Bubble Sort
              In the bubble sort technique, each element is compared to its adjacent elements and arranged accordingly in ascending
              or descending order. If it is to be sorted in ascending order, then it is to be checked whether the previous element is
              larger than the next element. If it is so, then the elements in these positions are swapped, i.e., interchanged, and so
              on. This process continues till the entire list of elements is sorted.
              Let us understand this by taking an example.

              The array is to be sorted in descending order:
                                   Index        0           1           2          3           4
                                    Ar          2           16         12          10          21





                272272  Touchpad Computer Science-XI
   269   270   271   272   273   274   275   276   277   278   279