filmov
tv
Binary Search Algorithm (Working, Algorithm & Diagram) in Data Structures | Part 1 | DSA
Показать описание
Support Simple Snippets by Donations -
--------------------------------------------------------------------------------------------- Binary search algorithm falls under the category of interval search algorithms. This algorithm is much more efficient compared to linear search algorithm. Binary search only works on sorted data structures. This algorithm repeatedly target the center of the sorted data structure & divide the search space into half till the match is found.
The time complexity of binary search algorithm is O(Log n).
Working -
1. Search the sorted array by repeatedly dividing the search interval in half
2. Begin with an interval covering the whole array.
3. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half.
4. Otherwise narrow it to the upper half.
5. Repeatedly check until the value is found or the interval is empty
Pseudo code for Binary Search Algorithm -
1. take input array, left, right & x
2. START LOOP - while(left greater than or equal to right)
2.1 mid = left + (right-left)/2
2.2 if(arr[mid]==x) then
2.2.1 return m
2.3 else if(arr[mid] less than x) then
2.3.1 left = m + 1
2.4 else
2.4.1 right= mid - 1
END LOOP
3. return -1
Simple Snippets Official Website -
Simple Snippets on Facebook -
Simple Snippets on Instagram -
Simple Snippets on Twitter -
Simple Snippets Google Plus Page -
Simple Snippets email ID -
#binarysearch #searchingalgorithms #datastructures
--------------------------------------------------------------------------------------------- Binary search algorithm falls under the category of interval search algorithms. This algorithm is much more efficient compared to linear search algorithm. Binary search only works on sorted data structures. This algorithm repeatedly target the center of the sorted data structure & divide the search space into half till the match is found.
The time complexity of binary search algorithm is O(Log n).
Working -
1. Search the sorted array by repeatedly dividing the search interval in half
2. Begin with an interval covering the whole array.
3. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half.
4. Otherwise narrow it to the upper half.
5. Repeatedly check until the value is found or the interval is empty
Pseudo code for Binary Search Algorithm -
1. take input array, left, right & x
2. START LOOP - while(left greater than or equal to right)
2.1 mid = left + (right-left)/2
2.2 if(arr[mid]==x) then
2.2.1 return m
2.3 else if(arr[mid] less than x) then
2.3.1 left = m + 1
2.4 else
2.4.1 right= mid - 1
END LOOP
3. return -1
Simple Snippets Official Website -
Simple Snippets on Facebook -
Simple Snippets on Instagram -
Simple Snippets on Twitter -
Simple Snippets Google Plus Page -
Simple Snippets email ID -
#binarysearch #searchingalgorithms #datastructures
Комментарии