Algorithm |
||
Binary Search
Linear Search is an algorithm to find a specified elements in a list or array by repeately splitting the array into two halves and searching in the halves that contains the item to be found. In order to apply this algorith, the items in the array (or list) should be in a certain order (e.g, numbers in increasing order). I know this may not sound clear to you, but it would look clearer if you see the pseudo code below and it will become even more clear if you implement this in program.
Overall sequence of Binary Search algorithm is as follows (This is a kind of pseudo code, try to implement this using any language you are familiar with): i) An array (or List) is given. Let's call this array as A[] (let's assume that A[] contains numbers in increasing order) ii) An element to be found is given. Let's call this elements as 'eTofind'. iii) iv) set the status variable IsFound = False iv) Do the followings Label : Loop Set a position of index i = midpoint index of A[] (i.e, i = round(length(A)/2) if A[i] == eToFind then IsFound = True and goto step v) else if A[i] > eToFind then A[] = First Half of A[] else A[] = Second Half of A[] go to Loop v) Print(or return) IsFound
|
||