Finding a number in an array in minimal time

Elements of the array are arranged in non-decreasing order. I need to find an array for a given element in the least possible time.

+3
source share
4 answers

Since the array is sorted, you can use Binary Search .

A linearsearch will take O(n)because it does not use a sorted property. But the search binarywill take O(log n).

+2
source

Use binary search to find it.

+2
source

, , , , , O(log N) ( ).

, O(log log N) , , . ( - - it O(N)).

0

, , Binary, , Newton Search, , - , , , , .

split = 0.5;
while(1)
{
    point = lower+split*(upper-lower);
    if(x>a[point])
    {
        lower = point;
        split*= 1.2
    }
    else if(x<a[point])

    {
        upper=point;
        split /=1.2
    } else break;
}

This supposedly gives better results with sets that have polynomial layouts and unrelated sets. This gives the worst results with random or linear layouts. It can also crash with a split growing above 1 without proper guarantees.

0
source

Source: https://habr.com/ru/post/1742429/


All Articles