Блог пользователя Sirandrew2

Автор Sirandrew2, история, 4 года назад, По-английски

We need a sorted array to perform a binary search in that case the time complexity already became greater than the linear search, so isnt linear search a better option in that case?

  • Проголосовать: нравится
  • +13
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится +17 Проголосовать: не нравится

Consider having Q Queries. Binary search complexity is O(NlogN + QlogN), but normal linear search is O(QN), a pretty big difference.

»
4 года назад, # |
  Проголосовать: нравится +4 Проголосовать: не нравится

If you only have one search, then linear search would be faster. But if you're doing a bunch of searches, then you can sort the array once and do each of the searches in $$$O(\log n)$$$.