Binary Search in Data Structure Using C Language
The binary search is very faster and efficient searching technique. The binary searching can be applied only sorted list of elements only. In this technique, to search an element we compare it with the element present at the middle of the list. If it found then the search stopped, otherwise, the array is divided into two parts. Here parts of array can apply as following formula : n/2 First part : 0 to n/2-1 Second part : n/2+1 to n-1 Because an array index always start with 0 index. Thus, first part all elements are greater than our search element and second part all elements are less then search element. After our search element either of these parts and searching continue will be start of these two parts, depending upon checking the element is greater or less than mid element. If element is less than mid element, then searching continue on first part otherwise on second part. If search element found of first part or second part of middle element than search stopped otherwise same process apply on these two parts for division of their parts. Al last any of these parts we are found of our search element as middle element of these parts, than search successfully otherwise all parts compared with our search element and we are not found our search element than give the message ‘Search element is not found in list’. Then search unsuccessfully, continue this process until we find our search element or not in the list. For example: we take the array containing 7 elements in sorted order shown in below table: Index 0 1 2 3 4 5 6 1 3 4 7 8 9 10 x Address 1000 1002 1004 1006 1008 1010 1012 We are searching 4 element of array than searching start at middle element. Step :1 we are comparing middle element with our search element, but our search 4 and middle element in array list is 7. So, do not match. This shown in below figure : Figure: search element 4!=7 but less than 7 Step :2 we are comparing middle element with our search element, but our search 4 less than to middle element 7, so we are searching first part 0th to 2nd index and in first part middle element in array list is 3 and our search element is 4. So, do not match. This shown below figure : Figure: first part search element 4!=3 but grater than 3 Step :3 we are comparing middle element with our search element, but our search 4 greater than to middle element 3 in first part , so we are searching right hand side element first part 0th to 2nd index and in first part right hand element in array list is 4 and our search element is 4. So, this is correct match with our search element. This shown below figure : Figure: first part search element 4==4 and this our search element Advantages of binary searching as follow: 1. It is very faster searching technique. 2. It is easy to hand for large list. Disadvantages of Binary searching are as follow: 1. It is works only on sorted list. 2. It is also implement recursive logic.
Download
0 formatsNo download links available.