Binary Search

So, I want to learn more about binary searches, because I really do not understand. A binary search requires a prerequisite for sorting an array. Did I understand correctly? It seems that the method should check for this precondition and raise an exception if it fails. But why is a background check a bad idea?

+3
source share
6 answers

This is a bad idea, because checking for data sorting takes n. The whole search is about steps log(n).
If you are going to check, you can also do a linear search.

+8
source

, , , .

, .

- ? , , , , , .

, , , , , , , , .

, . , , , , .

1 . , 1024 10 , , , , .

, , , . + N + log2 N, 1024 1034 , , 512.

, , , .


. , , , , , , - , , , , , . .

+7

Yep, 0 (log n) 0 (n). , DEBUG, RELEASE.

+3

, . , .

, . , .

.

, 'n' - .

O(log(n)) , . O(n).

, n, , .

, . ,

  • 1 .
  • 2 50% .
  • 5 46%
  • 20 22% .
  • 100 7% .

. .

+1

, (O (n), , O (log (n)) .)

, . - - . , , . , .

, : , , , , , log (n) , , .

, -, . .

0

, . . .

, . - , .

, 0RPM 5000RPM, . .

0
source

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


All Articles