查找 Search
- 顺序查找 Sequential search
- 二分查找 Binary search
- 块查找 Blocking search
- 哈希查找 Hash search
- 二叉树查找 Binary search tree search
1,Binary search:适用于已经排好序的数据进行查找。时间复杂度O(logN)。
Implementation:
Binary Search
int BinarySearch(const std::vector<int>& vecInt, int val)
{
int low = 0;
int high = vecInt.size() - 1;
int mid;
while (low <= high)
{
mid = low + (high - low) / 2;
if (vecInt[mid] < val)
{
low = mid + 1;
}
else if (vecInt[mid] > val)
{
high = mid - 1;
}
else
{
return mid;
}
}
return -1;
}
// 查找val,如果vector里有多个val,返回其中最小的索引号。
int BinarySearch_min(const std::vector<int>& vecInt, int val)
{
if (vecInt.size() == 0)
{
return -1;
}
int low = 0;
int high = vecInt.size() - 1;
int mid;
while (low < high - 1) // Keypoints 1
{
mid = low + (high - low) / 2;
if (vecInt[mid] >= val) // Keypoints 2
{
high = mid;
}
else
{
low = mid + 1;
}
}
if (vecInt[low] == val) // Keypoints 3: 先判断序号更小的
{
return low;
}
else if (vecInt[high] == val)
{
return high;
}
else
{
return -1;
}
}
int BinarySearch_max(const std::vector<int>& vecInt, int val)
{
if (vecInt.size() == 0)
{
return -1;
}
int low = 0;
int high = vecInt.size() - 1;
int mid;
while (low < high - 1)
{
mid = low + (high - low) / 2;
if (vecInt[mid] <= val)
{
low = mid;
}
else
{
high = mid - 1;
}
}
if (vecInt[high] == val)
{
return high;
}
else if (vecInt[low] == val)
{
return low;
}
else
{
return -1;
}
}
2,Hash search:关键是Hash函数算法(Hash function)和碰撞的解决办法(Collision resolution)。时间复杂度O(1)。
参考:http://en.wikipedia.org/wiki/Hash_table
常用的字符串Hash函数:
Hash function
unsigned int SDBMHash(const char *str)
{
unsigned int hash = 0;
while (*str)
{
// equivalent to: hash = 65599*hash + (*str++);
hash = (*str++) + (hash << 6) + (hash << 16) - hash;
}
return (hash & 0x7FFFFFFF);
}
// RS Hash
unsigned int RSHash(const char *str)
{
unsigned int b = 378551;
unsigned int a = 63689;
unsigned int hash = 0;
while (*str)
{
hash = hash * a + (*str++);
a *= b;
}
return (hash & 0x7FFFFFFF);
}
// JS Hash
unsigned int JSHash(const char *str)
{
unsigned int hash = 1315423911;
while (*str)
{
hash ^= ((hash << 5) + (*str++) + (hash >> 2));
}
return (hash & 0x7FFFFFFF);
}
// P. J. Weinberger Hash
unsigned int PJWHash(const char *str)
{
unsigned int BitsInUnignedInt = (unsigned int)(sizeof(unsigned int) * 8);
unsigned int ThreeQuarters = (unsigned int)((BitsInUnignedInt * 3) / 4);
unsigned int OneEighth = (unsigned int)(BitsInUnignedInt / 8);
unsigned int HighBits = (unsigned int)(0xFFFFFFFF) << (BitsInUnignedInt
- OneEighth);
unsigned int hash = 0;
unsigned int test = 0;
while (*str)
{
hash = (hash << OneEighth) + (*str++);
if ((test = hash & HighBits) != 0)
{
hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits));
}
}
return (hash & 0x7FFFFFFF);
}
// ELF Hash
unsigned int ELFHash(const char *str)
{
unsigned int hash = 0;
unsigned int x = 0;
while (*str)
{
hash = (hash << 4) + (*str++);
if ((x = hash & 0xF0000000L) != 0)
{
hash ^= (x >> 24);
hash &= ~x;
}
}
return (hash & 0x7FFFFFFF);
}
// BKDR Hash
unsigned int BKDRHash(const char *str)
{
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0;
while (*str)
{
hash = hash * seed + (*str++);
}
return (hash & 0x7FFFFFFF);
}
// DJB Hash
unsigned int DJBHash(const char *str)
{
unsigned int hash = 5381;
while (*str)
{
hash += (hash << 5) + (*str++);
}
return (hash & 0x7FFFFFFF);
}
// AP Hash
unsigned int APHash(const char *str)
{
unsigned int hash = 0;
int i;
for (i=0; *str; i++)
{
if ((i & 1) == 0)
{
hash ^= ((hash << 7) ^ (*str++) ^ (hash >> 3));
}
else
{
hash ^= (~((hash << 11) ^ (*str++) ^ (hash >> 5)));
}
}
return (hash & 0x7FFFFFFF);
}
参考:
1,若干经典的字符串哈希函数:http://www.cnitblog.com/schkui/archive/2007/07/02/29320.html
2,各种字符串Hash函数比较:http://blog.csai.cn/user3/50125/archives/2009/35638.html
常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法。这些函数使用位运算使得每一个字符都对最后的函数值产生影响。另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎不可能找到碰撞。
常用字符串哈希函数有BKDRHash,APHash,DJBHash,JSHash,RSHash,SDBMHash,PJWHash,ELFHash等等。
Hash查找算法测试
test of Hash search
unsigned int Hash(const char *str, unsigned int arrayLength)
{
return (ELFHash(str) % arrayLength);
}
struct CharacterInfo
{
std::string mName; // as key
std::string mInfo;
};
struct CharacterInfoNode
{
CharacterInfo* mCharacterInfo;
CharacterInfoNode* mNext;
};
const unsigned int HASH_TABLE_SIZE = 3;
void AddRecord(std::vector<CharacterInfoNode*>& hashTable, CharacterInfo* pCharacterInfo)
{
if (!pCharacterInfo || pCharacterInfo->mName == "")
{
return;
}
CharacterInfoNode* pNode = new CharacterInfoNode();
pNode->mCharacterInfo = pCharacterInfo;
pNode->mNext = NULL;
unsigned int index = Hash(pCharacterInfo->mName.c_str(), HASH_TABLE_SIZE);
if (NULL == hashTable[index])
{
hashTable[index] = pNode;
}
else
{
std::cout << "Occur collision: " << pCharacterInfo->mName << std::endl;
CharacterInfoNode* pTail = hashTable[index];
while (pTail->mNext != NULL)
{
pTail = pTail->mNext;
}
pTail->mNext = pNode;
}
}
// NULL is returned when fail to find.
CharacterInfo* FindRecord(std::vector<CharacterInfoNode*>& hashTable, std::string& name)
{
unsigned int index = Hash(name.c_str(), HASH_TABLE_SIZE);
CharacterInfoNode* pNode = hashTable[index];
while (NULL != pNode)
{
if (pNode->mCharacterInfo->mName == name)
{
return pNode->mCharacterInfo;
}
else
{
pNode = pNode->mNext;
}
}
return NULL;
}
int main(int argc, char *argv[])
{
std::vector<CharacterInfoNode*> hashTable(HASH_TABLE_SIZE); // index is the hash value.
CharacterInfo* pCharacterInfo = NULL;
pCharacterInfo = new CharacterInfo();
pCharacterInfo->mName = "岳不群";
pCharacterInfo->mInfo = "华山派掌门人,人称君子剑。";
AddRecord(hashTable, pCharacterInfo);
pCharacterInfo = new CharacterInfo();
pCharacterInfo->mName = "张三丰";
pCharacterInfo->mInfo = "武当掌门人,太极拳创始人。";
AddRecord(hashTable, pCharacterInfo);
pCharacterInfo = new CharacterInfo();
pCharacterInfo->mName = "东方不败";
pCharacterInfo->mInfo = "第一高手,葵花宝典。";
AddRecord(hashTable, pCharacterInfo);
std::string name = "张三丰";
pCharacterInfo = FindRecord(hashTable, name);
if (pCharacterInfo != NULL)
{
std::cout << "成功找到人物【" << name << "】: " << pCharacterInfo->mInfo << std::endl;
}
else
{
std::cout << "没有找到人物【" << name << "】" << std::endl;
}
return 0;
}
测试:
1,分别在奇数、偶数个有序数组中查找存在的值。
2,分别在奇数、偶数个有序数组中查找不存在的值。
3,在空数组中查找值。
4,如果数组中有多个相等值的数,是否能返回索引最小的(或最大的)。