Presentation is loading. Please wait.

Presentation is loading. Please wait.

SEARCHING.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found.

Similar presentations


Presentation on theme: "SEARCHING.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found."— Presentation transcript:

1 SEARCHING

2

3  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found then search is successful, otherwise search is said to be unsuccessful.  This is applicable to a table of element or linked list

4 Analysis:- Best case:- If the 1 st element is the required element. The time complexity is 1. Worst case:- If none of the element is same s the search element. The time complexity is ‘n’.

5 public static int linearSearch(Object[] array, Object key) { for(int k = 0; k < array.length; k++) if(array[k].equals(key)) return k; return -1; }

6  In this search all the elements in the array are sorted in ascending order.  Find the position of the mid element using the boundaries of an array.  If the searching element is grater than the mid element then adjust the lower boundary of an array. Ex:- low=mid+1  If the searching element is small than the mid element the adjust the higher boundary of an array. Ex:- high=mid-1  If the searching the element is equal to the middle element then stop the process and print search successful.  Repeat the steps until(low<=high)

7  Binary search uses a recursive method to search an array to find a specified value  Each execution of the recursive method reduces the search space by about a half  The binary search algorithm is extremely fast compared to an algorithm that tries all array elements in order  About half the array is eliminated from consideration right at the start  Then a quarter of the array, then an eighth of the array, and so forth

8

9


Download ppt "SEARCHING.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found."

Similar presentations


Ads by Google