Sort books - search by topic

Recently, I was asked a question in a technical interview that looks like this.

You need to find a specific book in the library, and as soon as you go inside, you notice that all the books are scattered throughout the library, that is, books are not organized in an organized manner inside the library. How do you start looking for this particular book? There is no specification for the book, except that you know Name of the Book .

I know that this is due to some search algorithm, but which algorithm can be used to search for a book in this case?

+4
source share
2 answers

Perform a linear search starting from the first book and search each book until you find the first occurrence of the book you want (maybe, of course, several copies).

If you are the only search engine and there are many books, sorting them before searching for a book will seem like a very inefficient waste of time - if you are not going to search for more books in the future.

You can always ask the librarian to tell you where the book is in their system, or you can enlist friends to help you find and share the problem and work in parallel.

EDIT There is also a quantum algorithm called the Grovers algorithm (if you believe it), which is faster than a linear search for unsorted but I don’t know too much about it, to be honest.

+2
source

In this case, you either

  • sort books and then search using a search algorithm (e.g. binary search). This is a good method if you sometimes have to look for another book.
  • Find a book by looking at the entire list of books, you can stop as soon as you find it.
0
source

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


All Articles