[英]Worst case time complexity to search an element in a closely sorted array of elements?
This was asked in my interview,Here the actual meaning of the question is to find the time complexity or specifically worst case time complexity of an array of elements which are already in the sorted order. 这在我的采访中被问到,这里问题的实际意义是找到时间复杂度,或者特别是最坏情况时间复杂性的元素数组已经在排序顺序中。
Main point to note here is the difference between the two adjacent numbers in the array are very small or insignificant. 这里要注意的要点是阵列中两个相邻数字之间的差异非常小或无关紧要。
I approached this problem as a simple binary search which requires the array to be in sorted order and thought the Worst case time complexity is O(log n)
. 我把这个问题看作一个简单的二进制搜索,它要求数组按顺序排列,并认为最差情况时间复杂度为O(log n)
。 But will this answer will change if the array elements are very close to each other as mentioned in the question. 但如果问题中提到的数组元素彼此非常接近,这个答案是否会改变。
What is the correct approach to solve this problem. 解决这个问题的正确方法是什么?
According to the question we can assume the array as below picture. 根据这个问题我们可以假设数组如下图所示。
Thisis defenitely not what iam asking which was shown below , because the elements are sparely differ in the difference between them and we can use binary seach. 这绝对不是iam询问下面显示的内容,因为元素之间的区别很大,我们可以使用二进制搜索。
The O(log n)
binary search complexity will not change even if all the elements are equal (or "very close to each other"), as long as array is sorted. 只要数组被排序,即使所有元素都相等(或“彼此非常接近”), O(log n)
二进制搜索复杂度也不会改变。 Perhaps we can improve performance by taking advantage on array values distribution and using interpolation search https://en.wikipedia.org/wiki/Interpolation_search But if implemented poorly Interpolation search could result in O(n)
complexity 也许我们可以通过利用数组值分布和使用插值搜索来提高性能https://en.wikipedia.org/wiki/Interpolation_search但是如果实现不好插值搜索可能导致O(n)
复杂性
If the array shows an almost linear slope, meaning that the difference between 2 consecutive elements is almost constant across the array, you could use linear interpolation to make a guess for the index where the value could be stored: 如果数组显示几乎线性的斜率,意味着两个连续元素之间的差异在整个数组中几乎是恒定的,您可以使用线性插值来猜测可以存储值的索引:
Here is an implementation in JavaScript, but without much of specific syntax. 这是JavaScript中的一个实现,但没有太多特定的语法。 It should be clear what is happening: 应该清楚发生了什么:
function search(arr, val) { var low, high, guess; low = 0; high = arr.length-1; while (low <= high && val >= arr[low] && val <= arr[high]) { // Use linear interpolation to make guess for index: guess = Math.round(low + (high - low) * (val - arr[low]) / (arr[high] - arr[low])); if (arr[guess] == val) return guess; console.log('Tried index ' + guess + '. No match yet for ' + val); if (arr[guess] < val) { low = guess + 1; } else { high = guess - 1; } } return -1; // not found } var arr = [1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16]; index = search(arr, 7); console.log('Search result: index ' + index);
When the array would be perfectly linear, the algorithm will find the element on the first guess, so in O(1) time. 当数组完全线性时,算法将在第一次猜测时找到元素,因此在O(1)时间内。 Depending on how much deviation is present in the intervals, the time will be somewhere between O(1) and O(long n) . 根据间隔中存在多少偏差,时间将介于O(1)和O(长n)之间 。
In a normal binary search, each time you have to split the remaining array into 2 halves and check the right half's median, BUT if you know the elements are very close, you can add a simple check to the procedure: 在正常的二进制搜索中,每次必须将剩余的数组拆分为两半并检查右半部分的中位数, 但如果您知道元素非常接近,则可以对过程添加一个简单的检查:
instead of: 代替:
you can modify 2 & 3 into: go right/left by abs(median - searched_number) this should shorten your average case time complexity, but not sure how to measure it 您可以将2和3修改为: 向右/向左按abs(median - searching_number)这会缩短您的平均案例时间复杂度,但不确定如何衡量它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.