有序陣列,目的是從a陣列中找到b陣列,還有更效率的解法嗎?在找到b[0]后對比的時候再優化下?
int BanarySearchArr(const int *a, int aSize, const int *b, int bSize)
{
int start = 0;
int end = aSize - bSize + 1;
int mid = 0;
while (start < end)
{
mid = (start + end) / 2;
if (b[0] == a[mid])
{
if (b[bSize - 1] != a[mid + bSize - 1])
{
return -1;
}
else if (!memcmp(b, a + mid, bSize * sizeof(int)))
{
return mid;
}
else
{
return -1;
}
}
else if (b[0] < a[mid])
{
end = mid;
}
else if (b[0] > a[mid])
{
start = mid + 1;
}
}
return -1;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/242596.html
標籤:C++ 語言
下一篇:怎么學習音視頻的相關知識
