Page 337 - ComputerScience_Class_11
P. 337
23 {
24 pos=mid;
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:
BlueJ: Terminal Window - Java
Options
How many names 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
11.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. Bubble
Sort is usually the slowest due to excessive swaps. This process continues until the entire list of elements is sorted.
Arrays 335

