Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
B.Sc. Applied Physical Science Part I- Sem –II Paper V ELPT – 202: Data Structures Lecture No . 13 Title of Programme: Linear and Binary Search Script 1. Introduction Hi friends, today we shall be discussing two popular searching algorithms. A search algorithm, as we know, is a method of locating a specific item of information in a larger collection of data (array, list etc). The algorithm returns true if the item is found and false if it is not present in the list. Today we shall discuss two algorithms for searching the contents of an array. Linear Search Binary Search 2. Linear Search Linear search is one of the simplest and basic search algorithm. It uses a loop to sequentially step through an array, starting with the first element. It compares each element with the value being searched for and stops when that value is found or the end of the array is reached. This is shown with a simple example. For example if one has to search for 8 in a list of integers 99, 97, 18, 21, 5, 8, 64, 0, -37. The algorithm will first compare with 99, then 97, then 18, 21, 5 and finally with 8 and stops as there is a match. We now look at the performance of linear search. 2.1 Performance Best, worst, and average cases In the best case, the target value is in the first element of the array. So there is only a single comparison and the running time is O(1). In real life, we don’t care about the best case, because it so rarely actually happens. In the worst case (when the element we are seeking is in the last position in the array or is not in the array at all), the total number of array item comparisons performed in a linear search equals the total length of the array being searched. For an array with 2048 elements this means 2048 comparisons in the worst case. Thus the running time is O(n). For determining average case of linear search, we consider the probability of presence of given element at any position in the array. If the element is present at first position then only one comparison takes place or when element is found at second position then two comparisons take place and so on. In general if an element is present at kth position then k comparisons take place. To find the average case complexity we calculate the average number of comparisons that may take place. Expression for average-case running time is: (1+2+…+n)/n = n(n+1)/2n = (n+1)/2 Therefore, average case time complexity for linear search is O(n). 2.2 Linear Search Tradeoffs Benefits – Easy algorithm to understand and implement – Array can be in any order – Can work both on arrays as well as linked lists Disadvantage – Inefficient (slow): for array of N elements, examines N/2 elements on average for value present in the array, N elements for value not present in the array 3. Binary Search Linear search is acceptable for relatively small arrays, but the process of examining each element in a large array is time consuming. An alternative to linear search is binary search. In order to perform binary search an array must be in sorted order. Binary search takes advantage of this organization by using a clever but simple strategy that quickly zeros in on the element to find. But before moving forward, we look at two real life examples of linear and binary search. • Suppose you have a telephone book and you want to search for a person’s telephone number. You know the person’s first and last name. How would you find it? How difficult is this? • Suppose you have the same telephone book and you want to find a person that has a certain telephone number. How would you find it? How difficult is this? Well in the first case, what we do is to open the phone book at some page near the middle of the book, compare the name that we want against any name in the page and based on this comparison decide whether we should look for the name in the succeeding or in the preceding pages. The search is continued in the same fashion, namely, we take the pages where the name might be, select a page and compare the name against any name from that page. When there is only one page that might contain the name of interest, then we look for name in basically the same manner. But in second case, we open the phone book in the first page, compare all the telephone numbers there against the number that we are looking for, then turn to the second page, do the same thing, and so on until we find the desired number. The first case is an example of binary search while the second case uses linear search to locate the telephone number. Obviously as we can see linear search is much more time consuming as compared to binary search. One might ask that why can't we use binary search to search for phone number in the directory in same way as we used to search for name. Well the reason is simple, the phone book is sorted on names but not on telephone numbers. With this background we now introduce binary search. The binary search is perhaps the most famous and best suitable search algorithm for sorted arrays. Indeed when the array is sorted it is useless to check every single item against the desired value. Binary search is based on the “divide-andconquer” strategy and may only be used on a sorted array. The algorithm eliminates one half of the elements after each comparison and works as follows: Locate the middle of the array. Compare the value at that location with the search key. If they are equal - done! Otherwise, decide which half of the array contains the search key. Repeat the search on that half of the array and ignore the other half. Repeat this process until the element is found, or until the entire array has been eliminated. 4. Implementation of binary search The code in C for binary search is: int binary_search(int a[], int size, int key) { int min=0, max=size-1, mid; while(min<=max) { mid=(min+max)/2; if(key==a[mid]) return mid; else if(key <a[mid]) max = mid-1; else min = mid+1; } return -1; } The function uses binary search to search for key in an array a, containing size number of elements. We use the variables min and max for setting the search boundaries. Initially min is zero and max is equal to highest index of the array. Mid is taken as average of min and max. The key is checked with the value present at mid index. If it is found we return back index. Else if key is smaller we reduce the max end otherwise we increase the min end. The while loop runs while the array is not exhausted and this is checked by seeing that the variable min does not exceed max . If array is exhausted and key is not found, -1 is returned. In the following slides, we have some examples of how binary search is used to search for elements present and not present in the list. 5. Performance of Binary Search Best, worst, and average cases In the best case, the target value is in the middle of the array. In first comparison itself the element is found. Thus the running time in best case is O(1). The worst case occurs when the element we are seeking is not in the array at all. Binary Search cuts the “search space” in half each time. Keeps cutting the search space in half until the target is found or has exhausted the all possible locations. To count the number of comparisons, we calculate the number of times the loop is executed by taking an example. Suppose the list has 8 elements. Each time through the loop, half the elements are removed. After one loop, 4 elements remain. – After two loops, 2 elements remain. – After three loops, 1 element remains. Thus for n elements at most log2n loops will be executed. So, the time complexity of binary search in worst case is O(log n). The running time in average case is also O(log n). 6. Comparison of binary search and linear search A comparison of linear and binary search reveals that binary search is much faster than linear search as shown in following table. Number of elements 10 100 1,000 10,000 1,00,000 10,00,000 10,00,00,000 Max. comparisons in linear search 10 100 1,000 10,000 1,00,000 10,00,000 10,00,00,000 Max. comparisons in binary search 4 7 10 14 17 20 30 For searching in a list containing one thousand elements, linear search makes 1000 comparisons while binary search makes 10 comparisons. While for one billion elements, linear search makes one billion comparisons while binary search makes 30 comparisons only. In fact the bigger the array the better a binary search is as compared to a linear search. The only constraint with binary search is the list has to be sorted. Before we conclude our talk, we have a look at where each of the two searching algorithms will be used. Linear search is good: – for arrays with a small number of elements – when you will not search the array many times – when the values in the array will be changed Binary search is good: – For arrays with a large number of elements – When the values in the array are less likely to change (cost of maintaining sorted order) With this I come to end to our talk on search algorithms. Thanking you. Objective The main objective of the lecture is to introduce linear and binary search algorithms. The implementation and performance of the algorithms along with their strengths and limitations are also discussed in detail. B.Sc. Applied Physical Science Part I- Sem –II Paper V ELPT – 202: Data Structures Lecture No . 13 Title of Programme: Linear and Binary Search Summary Linear search is one of the simplest and basic search algorithm. It uses a loop to sequentially step through an array, starting with the first element. It compares each element with the value being searched for and stops when that value is found or the end of the array is reached. The running time of linear search in both average and worst case is O(n). Linear search works well for arrays with a small number of elements. Binary search requires a sorted array. Binary search uses "Divide and Conquer" approach and works by cutting the search space in half until the target is found or has exhausted the all possible locations. The running time of binary search in both average and worst case is O(log n). B.Sc. Applied Physical Science Part I- Sem –II Paper V ELPT – 202: Data Structures Lecture No . 13 Title of Programme: Linear and Binary Search FAQs Q1. What are the strengths of linear search ? Ans : The main strengths of linear search are: Very simple and easy algorithm to understand and implement Array can be in any order Can work both on arrays as well as linked lists Q2. What are the strengths of binary search ? Ans : The main strengths of binary search are: Simple and easy Very fast Q3. What are the limitations of linear and binary search algorithms ? Ans : Linear search is very slow as compared to binary search. Binary search works only on sorted data. Q4. What is the performance of linear and binary search algorithms ? Ans : The running time of linear search in both average and worst case is O(n). The running time of binary search in both average and worst case is O(log n). Q5. In which situations do we prefer to use linear search over binary search and vice versa ? Ans : Linear search is preferred over binary search if the data is unsorted or if the array to be searched is very small. In all other cases binary search is preferred. B.Sc. Applied Physical Science Part I- Sem –II Paper V ELPT – 202: Data Structures Lecture No . 13 Title of Programme: Linear and Binary Search Glossary Linear Search : Linear search, also known as sequential search, is a process that checks every element in the list sequentially until the desired element is found. Binary Search : A binary search locates an item in a sorted array by repeatedly dividing the search interval in half. Binary Search is an O(log n)algorithm, which is more efficient than a linear search for large arrays. Assignments: 1. Consider the list {14, 20, 21, 25, 40, 58, 60, 75}. How many comparisons will be made to search for keys 60 and 80 by linear search and binary search? 2. In the above list, list down the keys for which linear search takes fewer comparisons than binary search. 3. List down the advantages and disadvantages of binary search. 4. Suppose you needed to look up a number in the local phone book of a large city (where the phone book has 6,000,000 entries). How many checks would be required, in the best and worst cases, to find the phone number using sequential search? How many checks would be required, in the best and worst cases, to find the phone number using binary search? 5. Fill in the blanks in the following code segment to implement sequenti al search of the given array. The method should return an integer value indicating whether or not the parameter value was found in the array. int seqSearch(int s) { int[] a = {9, 12, 14, 3, 25}; for( int i = ________; _______________________; i++) { if (___________________________ ) return ______________________; } return _________________ ; } Quiz Q.1) Which of the following is false ? A. Binary search and linear search have the same best-case performance. B. Binary search and linear search have the same worst-case performance. C. Linear search requires no special constraint on the array. D. Binary search requires the array is sorted. Q.2) Which of the following is true ? A. The maximum number of comparisons the linear search performs in searching a value in an array of size N is N. B. The linear search does not work unless the array is sorted. C. For the binary search routine, the successful search requires only logN comparisons, but the unsuccessful search requires N comparisons. D. Binary search does not work well on small arrays. Q.3) Suppose A is an array containing numbers in increasing order, but some numbers occur more than once. When using a binary search for one such value, A. The binary search always finds the first occurrence of the value B. The binary search always finds the last occurrence of the value C. Sometimes the binary search finds the first, sometimes the last, and sometimes a value in between. D. The binary search may fail to find any occurrence of the value Q.4) The worst case in binary search occurs ____. A. when the object to be searched is in the middle of the list B. when the object to be searched is at the end of the list C. when the object to be searched is at the beginning of the list D. when the object to be searched is not in the list B.Sc. Applied Physical Science Part I- Sem –II Paper V ELPT – 202: Data Structures Lecture No . 13 Title of Programme: Linear and Binary Search References 1. Data structures, Algorithms and Applications in C++ (2nd Edition) by Sartaj Sahni 2. Data Structures and Algorithm Analysis in C++ (3rd Edition) by Mark A. Weiss 3. Data Structures Using C and C++ (2nd Edition) by Yedidyah Langsam, Moshe J. Augenstein and Aaron M. Tenenbaum Links https://www.youtube.com/watch?v=ROalU379l3U