We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16
SEARCHING
Searching
• Searching is the Process of finding some particular element in
the list. If the element is present in the list, then the process is called successful and the process returns the location of that element, otherwise the search is called unsuccessful. • There are two popular search method:- * Linear Search * Binary Search Linear Search
• Linear search is the simplest search algorithm and often
called sequential search. In this type of searching, we simply traverse the list completely and match each element of the list with the item whose location is to be found. If the match found then location of the item is returned otherwise the algorithm returns NULL. • Example LINEAR_SEARCH(A, N, VAL) Step 1: [INITIALIZE]SET POS = -1 Step 2: [INITIALIZE] SET I = 1 Step 3: REPEAT STEP 4 WHILE I <= N Step 4: IF A[I] == VAL SET POS = I PRINT POS GO TO STEP 6 [END OF IF] SET I = I + 1 [end of loop] Step 5: IF POS = -1 PRINT "VALUE IS NOT PRESENT IN THE ARRAY " [END OF IF] Step 6: EXIT Complexity
• Time Complexity of linear search is o(n).
*Best case= o(1)(Big O)occurs when the element we are finding is at the first position of the array. * average case=o(n) * worst case= o(n) the worst case occurs when the element we are looking is present at the end of the array. The worst-case in linear search could be when the target element is not present in the given array, and we have to traverse the entire array. //Linear search Program #include<stdio.h> Void main() { int a[5]={7,6,8,1,4}; int I, pos=-1,x; printf(“enter the element which u want to search”); scanf(“%d”, &x); for(i=0;i<=4;i++) { if(x==a[i]) { pos=I; printf(“\n number is found at index position %d “, pos); break; } }
If (pos==-1) { printf(“\n number is not found”); } } BINARY SEARCH Binary search
Binary search follows divide and conquer approach in which,
the list is divided into two half and the item is compared with the middle element of the list. If the match is found then, the location of middle element is required otherwise, we search into either of the half depending upon the result produced through the match. Binary Search can be implemented only on sorted list of element. //Binary Search Program #include<stdio.h> void main() { int a[5]={4,9,12,15,20}; int low=0;high=4,mid,item; printf(“enter the number you want to search”) scanf(“%d” ,& item); while (low<=high) { mid=(low+high)/2; if(item<a[mid]) { high=mid-1; } else if(item>a[mid]) { low=mid+1; } else { printf(“\n Number is found at index position %d “,mid); break; } } if (low>high) { printf(“\n Number is not found”); } Complexity
• The time complexity of the binary Search Algorithm is
O(log2n), where n is the size of the sorted linear array. It means the complexity grows logarithmically as the size of array increase and the space complexity of its algorithm is O(1).