您的位置:首页 > 其它

二分查找的灵活应用

2013-07-26 17:46 295 查看
二分查找

1.找任意一个出现key的下标

思路:mid位置小于key则low右移,大于key则high左移,等于则直接返回mid

2.找第一个出现key的下标

思路:mid位置小于key则low右移,大于等于key则high左移,若low不越界且该位置的值等于key,则直接返回low,否则表示该值不存在

3.找最大的小于key的下标

思路:mid位置小于key则low右移,大于等于key则high左移,若high不越界则直接返回high,否则表示该值不存在

4.找最后一个出现key的下标

思路:mid位置小于等于key则low右移,大于key则high左移,若high不越界且该位置的值等于key,则直接返回high,否则表示该值不存在

5.找最小的大于key的下标

思路:mid位置小于等于key则low右移,大于key则high左移,若low不越界则直接返回low,否则表示该值不存在

代码:

#include "stdafx.h"
#include <iostream>
using namespace std;

//二分查找
//1.找任意一个出现key的下标
//  思路:mid位置小于key则low右移,大于key则high左移,等于则直接返回mid
int BinarySearchAnyKey(int nArr[], int nLength, int key)
{
int nLow = 0;
int nHigh = nLength - 1;
int nMid = 0;

while (nLow <= nHigh)
{
nMid = nLow + ((nHigh - nLow) >> 1);
if (nArr[nMid] > key)
{
nHigh = nMid - 1;
}
else if (nArr[nMid] < key)
{
nLow = nMid + 1;
}
else
{
return nMid;
}
}

return -1;
}

//2.找第一个出现key的下标
//  思路:mid位置小于key则low右移,大于等于key则high左移,若low不越界且该位置的值等于key,则直接返回low,否则表示该值不存在
int BinarySearchFirstKey(int nArr[], int nLength, int key)
{
int nLow = 0;
int nHigh = nLength - 1;
int nMid = 0;

while (nLow <= nHigh)
{
nMid = nLow + ((nHigh - nLow) >> 1);
if (nArr[nMid] >= key)//相等向左移
{
nHigh = nMid - 1;
}
else
{
nLow = nMid + 1;
}
}

if (nLow >= 0 && nLow < nLength && nArr[nLow] == key)
{
return nLow;
}
else
{
return -1;
}
}

//3.找最大的小于key的下标
//  思路:mid位置小于key则low右移,大于等于key则high左移,若high不越界则直接返回high,否则表示该值不存在
int BinarySearchMaxIndexLessThanKey(int nArr[], int nLength, int key)
{
int nLow = 0;
int nHigh = nLength - 1;
int nMid = 0;

while (nLow <= nHigh)
{
nMid = nLow + ((nHigh - nLow) >> 1);
if (nArr[nMid] >= key)//相等向左移
{
nHigh = nMid - 1;
}
else
{
nLow = nMid + 1;
}
}

if (nHigh >= 0 && nHigh < nLength)
{
return nHigh;
}
else
{
return -1;
}
}

//4.找最后一个出现key的下标
//  思路:mid位置小于等于key则low右移,大于key则high左移,若high不越界且该位置的值等于key,则直接返回high,否则表示该值不存在
int BinarySearchLastKey(int nArr[], int nLength, int key)
{
int nLow = 0;
int nHigh = nLength - 1;
int nMid = 0;

while (nLow <= nHigh)
{
nMid = nLow + ((nHigh - nLow) >> 1);
if (nArr[nMid] > key)
{
nHigh = nMid - 1;
}
else //相等向右移
{
nLow = nMid + 1;
}
}

if (nHigh >= 0 && nHigh < nLength && nArr[nHigh] == key)
{
return nHigh;
}
else
{
return -1;
}
}

//5.找最小的大于key的下标
//  思路:mid位置小于等于key则low右移,大于key则high左移,若low不越界则直接返回low,否则表示该值不存在
int BinarySearchMinIndexMoreThanKey(int nArr[], int nLength, int key)
{
int nLow = 0;
int nHigh = nLength - 1;
int nMid = 0;

while (nLow <= nHigh)
{
nMid = nLow + ((nHigh - nLow) >> 1);
if (nArr[nMid] > key)
{
nHigh = nMid - 1;
}
else//相等向右移
{
nLow = nMid + 1;
}
}

if (nLow >= 0 && nLow < nLength)
{
return nLow;
}
else
{
return -1;
}
}

void Mix(int nArr[], int nLength, int key)
{
cout << "找到任一" << key << "的位置:" << BinarySearchAnyKey(nArr, nLength, key) << endl;
cout << "找到第一次出现" << key << "的位置:" << BinarySearchFirstKey(nArr, nLength, key) << endl;
cout << "找到最大的小于" << key << "的数的位置:" << BinarySearchMaxIndexLessThanKey(nArr, nLength, key) << endl;
cout << "找到最后一次出现" << key << "的位置:" << BinarySearchLastKey(nArr, nLength, key) << endl;
cout << "找到最小的大于" << key << "的数的位置:" << BinarySearchMinIndexMoreThanKey(nArr, nLength, key) << endl << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
int nArr[8] = {2,3,3,3,3,5,6,7};
Mix(nArr, 8, 3);
Mix(nArr, 8, 4);

system("pause");
return 0;
}
运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: