![]() Searching refers to finding the location of one element in the array of n elements. Here are the complexities of the binary search given below. Thus LOC = 6 The complexity of Binary Search Step 2: Now BEG =6 and END =9 thus MID = INT(/2)= 6 ![]() Step 1: ARR < ITEM : thus END =9 and BEG = MID +1 = 6. Repeat step 3 and 4 while BEG <= END and ARR != ITEMīEG = 1 and END =9 Hence MID = (1+9)/2 = 5 Set BEG = LB, END = UB and MID = INT(/2)Ģ. ITEM needs to be searched in the array and algorithm returns location LOC, index at which ITEM is present else return -1.ġ. It needs to be checked if ITEM ARR then ITEM can appear in the right subarray then BEG = MID+1 and END will be the same and repeat.Īfter this MID is again calculated for respective sub-arrays, if we didn’t find the ITEM, the algorithm returns -1 otherwise LOC = MID.īSEARCH(ARR, LB, UB, ITEM, LOC) Here, ARR is a sorted list of elements, with LB and UB are lower and upper bounds for the array. The index MID defines the middle index of the array where, With every step of this algorithm, the searching is confined within BEG and END, which are the beginning and ending index of sub-arrays. Suppose ARR is an array with sorted n number of elements present in increasing order. Instead of searching an element one by one in the list, it directly goes to the middle element of the list, divides the array into 2 parts, and decides element lies in which sub-array the element exists. This type of technique is used in the case of sorted lists. This is a technique to search an element in the list using the divide and conquer technique. Average complexity: O(n) – This means when an element is present somewhere in the middle of the array.Best case complexity: O(1) – This case occurs when the first element is the element to be searched. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |