温故知新,最近回顾数据结构:)
这是折半查找的C++表示:
#include <iostream>
using namespace std;

int find_bin(const char *pArray, char ch)
{
if(pArray == NULL)
return -1;

int nLeft = 0;
int nRight = strlen(pArray);
int nMid;
while(nLeft <= nRight)
{
nMid = (nLeft + nRight) / 2;
if(pArray[nMid] == ch)
return nMid;
else if(ch < pArray[nMid])
nRight = nMid - 1;
else
nLeft = nMid + 1;
}
return -1;
}

int main()
{
char array[] = "abcdefgh";
int nIndex = find_bin(array, 'e');
if(nIndex != -1)
cout << "Find the character and the index is " << nIndex << endl;
else
cout << "Can't find the character" << endl;
return 0;
}
这是折半查找的C++表示:
#include <iostream>
using namespace std;
int find_bin(const char *pArray, char ch)
{
if(pArray == NULL)
return -1;
int nLeft = 0;
int nRight = strlen(pArray);
int nMid;
while(nLeft <= nRight)
{
nMid = (nLeft + nRight) / 2;
if(pArray[nMid] == ch)
return nMid;
else if(ch < pArray[nMid])
nRight = nMid - 1;
else
nLeft = nMid + 1;
}
return -1;
}
int main()
{
char array[] = "abcdefgh";
int nIndex = find_bin(array, 'e');
if(nIndex != -1)
cout << "Find the character and the index is " << nIndex << endl;
else
cout << "Can't find the character" << endl;
return 0;
}

4万+

被折叠的 条评论
为什么被折叠?



