Sequential Search slides Searching • Searching : – Information retrieval is one of the most important application of computers. – EG: Looking for a Name by giving the telephone number. – We give one piece of information (KEY) and we are asked to find a record that contains other information associated with the key Searching • Searching for the keys that locate records is the most time consuming action in a program • Two Types of Searching are there, 1) external searching Searching in external storage devices (Disk,Tapes etc..) 2) internal searching Searching within the computer memory Searching • Sequential Search : – Start at the first piece of data and look at it and if it no then keep going until you find what you are looking for or until you have reached the last. – EG : – Analysis : – When to Use? Sequential search • Find the marks of the students from the Data structures and algorithms course using an array. ( Key - ID) array name is ‘ a ‘ // create A R R A Y ID 456 60 80 For ( I =0; I < size; I++) { if a[I].id == ‘123’ cout << a[I].id<<a[I].mark1; found = true } if (found !=true) cout << “Not in the List” Sequential Search Algorithm2: cin >>target; found = 0; I =0; while(I<n) &&(found==0) if (array[I]==target) found =1; Prev Alg: Depends on n But this one depends not only on n but also the position of the target Sequential Search • Two Kinds of result 1) Successful 2) UnSuccessful Best Case - 1st element O(1) (456) Worst Case - nth element O(n) (last) Sequential search 1)It is useful for limited sized data sets as it is simple and does not require data to be structured in any way 2)when the data to be searched is constantly changing Dis : time consuming (large list) Any there other suitable data structures? This algorithm is well suited for implementation with linked list - follow ‘next’ pointer until we find or until we have reached the last adv: addition/deletion is easy Binary Search • How it works? • Binary - two . List is broken down to two parts and searched • working : Compare the target with the one in the center of the list and then restrict our attention to only the first or second half depending on whether the target comes before or after the central one. ( we reduce the length to be searched by half) Binary Search • Rule - numbers must be in sorted order • Steps : 1. L = Low ( first index) 2. H = High (last index) 3. M = Mid ( ( l + h )/ 2 ) • 1. mark l & h • 2. Find Mid Binary search 3. Check whether mid = x if so return index 4. If not , check whether x is > then bring low to mid +1 and carry on the same process 5. If < then bring high to mid-1 and carry on the same process 6. Until you find the value or if low and high cross each other Binary search EG : 10 20 30 40 50 60 70 Find X = 20 (search value) 10 20 30 40 50 60 70 low high 1. Mid = l+h/2 = 1+7/2 = 4 10 20 30 40 50 60 70 low h Mid high = mid -1 Binary search 10 20 30 40 50 60 70 low h mid = l +h/2 = 1+3/2 =2 10 20 30 40 50 60 70 l m h check x = a[mid] , yes matching so return index - ie 2