 Hello, welcome to the lecture on Searching Operation on Array in Java. In this lecture, we will be discussing about how to perform searching operation on array in Java and methods available to perform this operation in arrays class available in Java library. The learning outcome of this video lecture is that the student will be able to write a Java program to perform searching operation on array and will be able to use binary search method of arrays class in Java library. Searching operation is finding a particular element in given list of elements. There are basically two types of searching which we will be discussing here. First one is linear search and second one binary search. Linear search is the simplest way of searching a particular element in the array. In the linear search approach, the search element is compared with each value of that array with the target element. As shown here, in this example array is containing 7 elements 45, 23, 67, 34, 12, 28 and 56 and search element is 12. As per linear search algorithm, 12 is first compared with the first element in the array. So in this example, 12 is compared with 45. Now here, 12 is not equal to 45, therefore search will be continued to next element if next element is present in the array. Here next element is 23, so 12 is compared with 23. It is not equal, then continued and 12 is compared with 67, so 12 is not equal to 67. Now search is continued and 12 is compared with the next element 34, 12 is not equal to 34, continued with the next element, 12 is compared with 12. Similar search element is matching with array element. Now we found search element in the array at index 4 in this example. If not found case is arrived, then search element is compared with each element till last element in the array. Second searching algorithm is binary search. This is an efficient searching algorithm and in this approach, search is performed on the sorted list of numbers. It is also called as half interval search. The range of array will be divided into two parts at every iteration of comparison. The search element that is target element will be compared with the middle element of the array. If the target element equals to the middle element, then we found element and search is completed. Else, search will be continued in one of the divided part based on the search value smaller or greater than the middle element. Here is the example. List of sorted numbers stored in the array. The numbers are 8, 12, 16, 24, 29, 36. These numbers are stored in ascending order. Size of the array is 6. This list is divided at the middle by using lowest index plus highest index divided by 2. I shown here in the formula. It means for this example, 0 plus 5 divided by 2 which results in 2. So the middle element will be at index 2. Now the search element 12 is compared with the middle element 16 which is not equal to 12. Continue the search in the first half because the search element 12 is smaller than the middle element 18, middle element 16. Now search range is from index 0 to index 1. So middle element in this part is calculated using new lowest index and highest index that is 0 and 1. So middle element is at index 0 that is element 8 which is not matching with the search element 12. Continue the search in the second half because the search element 12 is greater than the middle element 8. Now search range is from index 1 to index 1. So middle element in this part is calculated using new left index and right index that is 1 plus 1 divided by 2. So the middle element is at an index 1 that is element 12 which is matching with the our search element 12. Here search element match found and element found at index 1. Now search is completed. In this way the binary search will work. Now here is a demo example demonstrating the binary search algorithm. First the array A is initialized with the value 37, 10, 15, 91, 110 and 150 which is in sorted order in ascending order. The variables declared here is the target that is the search element 91, left index initialized to 0. The middle variable is used to calculate the middle element index and right variable is initialized with the array length minus 1. Now the search will be continued if the left index is smaller or equal to the right index. If this condition is satisfied then only the search will be continued. Now the first we are going to calculate the middle element index using the formula left element index plus right element index divided by 2. Now this middle element is compared with the target element and if it is matching so we found the element at that particular position and we will stop here only our search. So that is why break statement is there. If the middle element is not matching with the target element then there are two possible cases that is target element is greater than the middle element or the target element is smaller than the middle element. If the target element is greater than the middle element means we have to continue the search in the second half. So left index will be changed that is middle element index plus 1 and if the target element is smaller than the middle element means we have to continue the search in the first half of the list. the in that case right index will be changed that will be calculated using the middle element index minus 1 and in this way the search will be continued till the left element left index position is smaller than or equal to right index position. The other case if we do not find the search element in the given list then it will continue the searching and it will come out of the loop in that scenario the left index will be greater than the right index so in that case we will found that element not found case. We have discussed how binary search works. Arrays class in java.util package containing binary search method which is most efficient method to find an element in a sorted array in Java. This method searches the specified array for the specified value using the binary search algorithm. The array must be sorted before making the call making the call to this particular method. If it is not sorted the results are undefined. The declaration of binary search method is shown here. This method is public static method it is accessible using the class name itself. Return type is integer and two parameters are passed to this method. The first one is array of elements of specified data type and second parameter is element of same type that is key to be searched. This method returns index of the search key if it is contained in the array list. As this method returns the minus insertion point minus one what it means we will discuss more detail using example. Here is the simple Java program to demonstrate use of binary search method from arrays class. Integer array A is declared and initialized with the sorted values that is 10, 11, 13, 14, 15. Now key variable is declared and initialized with the value 14. Binary search method from arrays class is called with the parameters integer array and key. The return value is stored in RET integer variable and RET value is displayed in the next line using system.out.println. Now here search value is 14 which is present at index position 3 in array A. This index value is written by the binary search method while searching 14. Next case assume K is 8 then 8 is not present in array A which value it returns if it will be negative number but not always the minus 1. In this case if 8 is 8 will be inserted in this sorted list then at which index it will be inserted that is it will be 0. At index 0 it will be inserted to keep the list in sorted order. The return value will be minus insertion point minus 1 so in this case it is minus 0 minus 1 which is equal to minus 1. So while searching the key value 8 the binary search method will return minus 1. Another case is if the key value which we are searching is 16 in this case 16 does not exist in array A therefore new insertion point for 16 would be 5. Hence the return value is minus 5 minus 1 which is equal to minus 6. One more case with the key value 12 in this case 12 is does not exist in array A therefore new insertion point for 12 would be 2. Hence the return value is minus 2 minus 1 which is equal to minus 3. So with this example program we have seen how binary search method works. Now pause the video for some time and answer the below question. What is the value written by the binary search method called in the given code snippet? Here the value 45 does not exist in array A so the new index position will be 3 if 45 would be inserted in this sorted array A. Therefore binary search will return minus insertion point minus 1 that is equal to minus 3 minus 1. So therefore minus 4 will be written by this method. Thank you for watching this video.