⼆进制树型搜索算法_⼆进制搜索算法
⼆进制树型搜索算法
O(log n) makes it very fast as
Binary Search is applied on the sorted array or list of large size. It's time complexity of O(log n)
compared to other sorting algorithms. The only limitation is that the array or list of elements must be sorted for the binary search algorithm to work on it.
O(log n)的时间复杂性使其与其他排序算法相⽐⾮常快。 唯⼀的限制是必须对元素的数组或⼆进制搜索应⽤于排序后的数组或⼤型列表。 O(log n)
列表进⾏排序,以便⼆进制搜索算法可以对其进⾏处理。
实现⼆进制搜索算法 (Implementing Binary Search Algorithm)
Following are the steps of implementation that we will be following:
以下是我们将要执⾏的实现步骤:
1. Start with the middle element:
从中间元素开始:
1. target
⽬标值等于数target value is equal to the middle element of the array, then return the index of the middle element.⽬标值
组的中间元素,则返回中间元素的索引。
2.
1. If the target value is greater than the number in the middle index, then pick the elements to the right of the
middle index, and start with Step 1.
2. If the target value is less than the number in the middle index, then pick the elements to the left of the middle
index, and start with Step 1.
2. When a match is found, return the index of the element matched.
到匹配项后,返回匹配元素的索引。
3. If no match is found, then return -1
如果不到匹配项,则返回-1
/*
function for carrying out binary search on given array
- values[] => given sorted array
- len => length of the array
- target => value to be searched
*/
int binarySearch(int values[], int len, int target)
{
int max = (len - 1);
int min = 0;
int guess; // this will hold the index of middle elements
int step = 0; // to find out in how many steps we completed the search
while(max >= min)
{
数学二进制的算法guess = (max + min) / 2;
// we made the first guess, incrementing step by 1
step++;
if(values[guess] == target)
{
printf("Number of steps required for search: %d \n", step);
return guess;
}
else if(values[guess] > target)
{
// target would be in the left half
max = (guess - 1);
}
else
{
// target would be in the right half
min = (guess + 1);
}
}
// We reach here when element is not
// present in array
return -1;
}
int main(void)
{
int values[] = {13, 21, 54, 81, 90};
int n = sizeof(values) / sizeof(values[0]);
int target = 81;
int result = binarySearch(values, n, target);
if(result == -1)
{
printf("Element is not present in the given array.");
}
else
{
printf("Element is present at index: %d", result);
}
return 0;
}
We hope the above code is clear, if you have any confusion, post your question in our .我们希望上⾯的代码清晰明了,如果您有任何疑问,请在我们的“ 发布您的问题。
O(log n) and how can we calculate the number of Now let's try to understand, why is the time complexity of binary search O(log n)
steps required to search an element from a given array using binary search without doing any calculations. It's super easy! Are you ready?
O(log n),以及如何在不进⾏任何计算的情况下使⽤⼆分查来计算从给定数组中现在让我们尝试理解,为什么⼆分查的时间复杂度为O(log n),
搜索元素所需的步数。 超级容易! 你准备好了吗?
⼆元搜索的时间复杂度O(log n) (Time Complexity of Binary Search O(log n))
base of the log doesn't matter in asymptotic When we say the time complexity is log n, we actually mean log2 n, although the base
notations, but still to understand this better, we generally consider a base of 2.
底数在渐进表⽰法中并不重要,但是为了更好地理解这⼀点,我们通常当我们说时间复杂度是log n ,我们实际上是指log 2 n ,尽管对数的底数
考虑2的底数。
Let's first understand what log2(n) means.
⾸先让我们了解log 2 (n)含义。
Expression: log2(n) - - - - - - - - - - - - - - - For n = 2: log2(21) = 1 Output = 1 - - - - - - - - - - - - - - - For n = 4 log2(22) = 2 Output = 2 - - - - - - - - - - - - - - - For n = 8 log2(23) = 3 Output = 3 - - - - - - - - - - - - - - - For n = 256 log2(28) = 8 Output = 8 - - - - - - - - - - - - - - - For n = 2048 log2(211) = 11 Output = 11
表达式:log 2 (n)---------------对于n = 2:log 2 (2 1 )= 1输出= 1------------ ---对于n = 4 log 2 (2 2 )= 2输
出= 2----------------对于n = 8 log 2 (2 3 )= 3输出= 3-- --------------对于n = 2048 log 2 (n = 256 log 2 (2 8 )= 8输出= 8---------------- 2 11 )= 11输出= 11 Now that we know how log2(n) works with different values of n, it will be easier for us to relate it with the time complexity of the binary search algorithm and also to understand how we can find out the number of steps required to search any
number using binary search for any value of n.
现在我们知道如何log 2 (n)与不同价值观的作品n ,这将是我们更容易与⼆进制搜索算法的时间复杂度有关,同时还应了解我们如何能够出需要的步数使⽤⼆进制搜索n任何值来搜索任何数字。
计算步数 (Counting the Number of Steps)
As we have already seen, that with every incorrect guess, binary search cuts down the list of elements into half. So if we start with 32 elements, after first unsuccessful guess, we will be left with 16 elements.
正如我们已经看到的,每进⾏⼀次错误的guess ,⼆进制搜索就会将元素列表缩减为⼀半。 因此,如果我们从32个元素开始,则在第⼀次失败的猜测之后,我们将剩下16个元素。
So consider an array with 8 elements, after the first unsuccessful, binary sort will cut down the list to
half, leaving behind 4 elements, then 2 elements after the second unsuccessful guess, and finally only 1 element will be left, which will either be the target or not, checking that will involve one more step. So all in all binary search needed at most 4 guesses to search the target in an array with 8 elements.
因此,考虑⼀个包含8个元素的数组,在第⼀个不成功的⼆进制排序之后,将列表减半,剩下4个元素,然后在第⼆个不成功的猜测之后保留2个元素,最后只剩下1个元素,或者是否达到target ,检查将涉及更多步骤。 因此,所有⼆进制搜索最多需要4个猜测才能在具有8个元素的数组中搜索target 。
If the size of the list would have been 16, then after the first unsuccessful guess, we would have been left with 8 elements. And after that, as we know, we need atmost 4 guesses, add 1 guess to cut down the list from 16 to 8, that brings us to 5 guesses.
如果列表的⼤⼩为16,则在第⼀次不成功的猜测之后,我们将剩下8个元素。 然后,据我们所知,我们最多需要4个猜测,再加上1个猜测就可以将列表从16个减少到8个,这使我们得到5个猜测。
So we can say, as the number of elements are getting doubled, the number of guesses required to find the target increments by 1.
因此,可以说,随着元素数量增加⼀倍,到target所需的猜测数量将增加1 。
Seeing the pattern, right?
看到图案了吧?
Generalizing this, we can say, for an array with n elements,
概括地说,对于具有n元素的数组,
n, until we get the value 1, plus one.n开始可以重复减半的次数,直到获得值1加1。
log function works And guess what, in mathematics, the function log2 n means exactly same. We have already seen how the log
above, did you notice something there?
log函数的⼯作⽅式,您在那⼉注意到了吗?
并猜想在数学上,函数log 2 n含义是完全相同的。 我们已经在上⾯看到了log
For n = 8, the output of log2 n comes out to be 3, which means the array can be halved 3 times maximum, hence the number of steps(at most) to find the target value will be (3 + 1) = 4.
对于n = 8 ,对log 2 n的输出为3 ,这意味着该数组可以减半为最⼤3倍,因此(最多)查⽬标值的步骤数将为(3 +1)= 4。
Question for you: What will be the maximum number of guesses required by Binary Search, to search a number in a list of Question for you:
2,097,152 elements?
2097152个元素列表中的数字,最⼤猜想数是多少?
您的问题:
您的问题:⼆进制搜索要搜索2097152个
⼆进制树型搜索算法
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论