what data structure should I use for hash lookup as well as binary search?

Posted by zebraman on Stack Overflow See other posts from Stack Overflow or by zebraman
Published on 2010-04-01T05:19:53Z Indexed on 2010/04/01 5:23 UTC
Read the original article Hit count: 374

Filed under:
|
|
|
|

I am working on a school homework. I have a list of names. I want to be able to perform binary search on these names (find all names between a lower and upper bound) for first name as well as last name, and perform keyword searches as well (this will be accomplished using hashing. For example, if I have the names

Garfield Cat
Snoopy Dog
Captain Crunch
Fat Cat

then a binary search of first names (C,H) will return Captain Crunch, Fat Cat, and Garfield Cat. A binary search of last names (Cr,D) will return Captain Crunch. A keyword search of 'cat' will return Fat Cat and Garfield Cat.

I understand binary search will only work on a sorted list, but since I am planning on searching two different criteria, I will have to sort the list by last name or first name depending on what I'm searching for. I feel like it will be too inefficient to have to resort the list each time I want to perform a new binary search. Would it just be better for me to set up and maintain two sorted lists (one for sorted by first name, one for sorted by last name)?

Also, for hashing, will I have to set up a different table of names for that as well? I understand each keyword will hash to some value determined by a hash function, and this value (or key) is a table address where the corresponding names are stored.

So I just want to know what would be the best way to solve this problem? Maintaining separate structures, or is there a way to efficiently do everything I want with just one data structure?

© Stack Overflow or respective owner

Related posts about hashing

Related posts about data