Page 287 - Computer science 868 Class 12
P. 287
14 while(min<=max)
15 {
16 mid=(min+max)/2;
17 if(name[mid].equalsIgnoreCase(ns))
18 {
19 pos=mid;
20 break;
21 }
22 else
23 if(name[mid].compareTo(ns)>0)
24 max= mid-1;
25 else
26 min=mid+1;
27 }
28 if(pos!=-1)
29 System.out.println(ns + " found in position "+ (pos+1));
30 else
31 System.out.println(ns + " not found");
32 }
33 }
The output of the preceding program is as follows:
Enter a name : Abhay
Enter a name : Neha
Enter a name : Mona
Enter a name : Riya
Enter a name : Zishan
RIYA found in position 4
Linear Search and Binary Search
Linear Search Binary Search
1. Array elements may or may not be in sorted order. 1. Array elements should be in sorted order.
2. Execution is slow. 2. Execution is fast.
3. Searching begins from the first element and proceeds 3. Search begins from the middle element and proceeds
to the last. either to the left or to the right depending on the value
of the searched element.
285
Arrays 285

