Sort out Books - Search related question
I've recently been asked a question in technical interview that goes like this.
You have to find a specific book in a library, and the moment you went inside, you saw that all the books are scattered all around the library, i.e. books are not placed in organized way inside 开发者_JAVA百科library. How will you proceed to look for that specific book? There is no specification about the book except that you know the Name of the Book
.
I know this is related to some search algorithm, but what algorithm can be used for searching book in this case?
Perform a linear search starting at the first book and searching each book until you come across the first occurrence of the the book you want (there may be multiple copies of course).
If you are the sole searcher and there are many books, then sorting them before looking for the book would seem like a hugely inefficient waste of time - unless you intend to search for more books in the future.
You could always ask the librarian to tell you where the book is on their system or you could enlist some friends to help you search and split the problem up and work in parallel.
EDIT There is also a quantum algorithm called Grovers algorithm (if you're in to that sort of thing) that is faster than a linear search for an unsorted database but I don't know too much about it to be honest.
In that case you either
- sort the books and search then with a search algorithm (eg. binary search). This is a good method if you might have to search another book sometimes
- search the book by going through the whole list of books, you can stop once you've found it
精彩评论