Log base 3 of the time search algorithm

Full disclosure of this problem for homework, so I will not ask for specific solutions, just general answers to some questions. The text of the problem is as follows:

Given a sorted array of type T that the Comparable interface should implement, write a generic Java method that finds a specific element in the array and returns it, or returns null if it is not found. Please note that your algorithm should work in the worst case in the database 3 times, in database 2 (binary search) it will not receive credit

There is also an additional condition that the algorithm must be iterative and not recursive.

1) So, my first question is: how to determine that the algorithm works in database 3, and not in database 2? These two differ only in the constant factor, therefore, even if I analyze the structure of the algorithm, how can I find out if I work in Log3 (n) or just work (~ 0.63) (Log2 (n)), time? Does it even matter?

2) I get the impression that binary search is a more or less standard fast search algorithm for sorted arrays, rather than binary and linear search, what other search algorithms exist that I can count on?

3) Is there something that I am missing, some condition that allows me to search for an array faster than a regular binary search?

Any help is greatly appreciated, sorry if this question is quite specific, but I think some parts of it may be generally applicable.

+4
3
  • , O (log (n)) , 2 3.

  • , , . , log 3 (N) . -, , , . , . " ", , , N , , short .
    , N, - ( ). X, K K , 2 K X . (, 3 ), , , X. X .

+2

; x y ( [x,(x+y)/2], [(x+y)/2, y].

, , [x, (x+y)/3], [(x+y)/3, 2*(x+y)/3] [2*(x+y)/3, y] . , .

O(lg n). , , , , , , . , , , , . ( ).

, - , - node (), , node , . , , , , - B-tree, node ( , node) , , O (lg (n)), .

+1

I believe that you can do better than a simple binary search if you are doing something smarter than just half the collection in each iteration.

Instead, you can select the reference point by checking the input viewed more as a monotonic function than as a sequence of values.

The Brent algorithm uses this approach and will accelerate convergence.

See https://en.wikipedia.org/wiki/Brent%27s_method

If this was my task, I would base my decision on this.

-2
source

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


All Articles