CAPTER 6 SEARCHING ALGORITHM
WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value
Application Phone Number in List Word in Dictionary Accessing Website
LINEAR SEARCH
DEFINITION Sequential SearchLook for successive elementsStops when found the valueStops when no value has been foundGood for small arrays
Ad/Disad-Vantages Advantages Simple Does not necessarily need to be in order Good for item located in front list Disadvantages Not so efficient Looking at beginning of the list only Only efficient if the data is sorted
Algorithm in General For all the items in the list Compare the item with the desired item If the item was found Return the index value of the current item Endif EndFor Return -1 because the item was not found
Code in C++ #include using namespace std; const int NUMEL=10; int linearSearch(int[], int, int); int main() { int nums[NUMEL]={5,10,22,32,45,67,73,98,99,101}; int item,location; cout<<"Enter the item you are searching for: "; cin>>item; location=linearSearch(nums,NUMEL,item); if(location> -1) cout<<"The item was found at index location "<<location<<endl; else cout<<"The item was not found in the array\n"; return 0; }
Code in C++ (Cont.) //this function returns the location of key in the list //a -1 is returned if the value is not found int linearSearch(int list[ ],int size,int key) { int i; for(i=0;i<size;i++) { if (list[i]==key) return i; } return -1; }
Example Default Try to find “32” “5” with “32” = NO “10” with “32” = NO “22” with “32” = NO “32” with “32” = YES
BINARY SEARCH
DEFINITION Efficient if sortedAll must be sorted firstDivide the array into half
Process of Binary Search 1 st. Look at the middle element2 nd. If equal, then finish3 rd. If less, do left binary searching 4 th. If greater, do right binary searching
Algorithm Set the lower index to 0 Set the upper index to one less than the size of the list Begin with the first item in the list While the lower index is less than or equal to the upper index Set the midpoint index to the integer average of the lower and upper index values Compare the desired item to the midpoint element If the desired element equals the midpoint element Return the index value of the current item Else if the desired element is greater than the midpoint element Set the lower index value to the midpoint value plus 1 Else if the desired element is less than the midpoint element Set the upper index value to the midpoint value minus1 Endif EndWhile Return -1 because the item was not found
Code in C++ #include using namespace std; const int NUMEL=10; int binarySearch(int[], int, int); int main() { int nums[NUMEL]={5,10,22,32,45,67,73,98,99,101}; int item,location; cout<<"Enter the item you are searching for: "; cin>>item; location=binarySearch(nums,NUMEL,item); if(location> -1) cout<<"The item was found at index location "<<location<<endl; else cout<<"The item was not found in the array\n"; return 0; }
Code in C++ //this function returns the location of key in the list //a -1 is returned if the value is not found int binarySearch(int list[],int size,int key) { int left,right,midpt; left=0;//set the lower index to 0 right=size-1;//set the upper index to one less than the size of the list while(left<=right) { midpt=(int)((left+right)/2);//set the midpoint index if(key==list[midpt])//compare the desired item to the midpoint element { return midpt; } else if(key>list[midpt]) left=midpt+1;//set the lower index value to midpoint value plus 1 else right=midpt-1;//set the upper index value to midpoint value minus 1 } return -1;//return -1 if the item was not found }
Example Binary 1 Default st pass nd pass10 Find item “10” 1 st pass : Left = 0, Right = 9, Mid = 4, List[4]=45. Thus Key=10<List[4]. Right=3 2 nd pass : Left = 0, Right = 3, Mid = 1, List[1]=10. Thus Key=10==List[1]. Done
Example Binary 2 Find item “32” 1 st pass : Left = 0, Right = 9, Mid = 4, List[4]=45. Thus Key=32<List[4]. Right=3 Default st pass nd pass rd pass th pass32
Example Binary 2(Cont.) Find item “32” 2 nd pass : Left = 0, Right = 3, Mid = 1, List[1]=10. Thus Key=32>List[1]. Left = 2 Default st pass nd pass rd pass th pass32
Example Binary 2(Cont.) Find item “32” 3 rd pass : Left = 2, Right = 3, Mid = 2, List[2]=22.Thus Key=32>List[2]. Left = 3 Default st pass nd pass rd pass th pass32
Example Binary 2(Cont.) Find item “32” 4 th pass : Left = 3, Right =3, Mid = 3, List[3]=32. Thus Key=32==List[3]. Done Default st pass nd pass rd pass th pass32
COMPARISON LINEAR/BINARY LINEAR SEARCH O(n) Best for unsorted array (sequential access) Needs more space and time complexity. Not suitable for large set of data BINARY SEARCH O(nlog2n) Best for sorted array (liner access) Best practice in application since most data is sorted in real world application Suitable for large set of data