void 降序折半查找(int* a, int aa, int aaa)
{//缘由https://bbs.csdn.net/topics/399166569int aaaa = aaa /= 2; bool k = 0;if (a[0] == aa){cout << 0, cout << ends << "查找:" << aa << endl;k = 1;return;}else if (a[aaa - 1] == aa){cout << --aaa, cout << ends << "查找:" << aa << endl;k = 1;return;}while (aaa)if (a[aaaa] == aa || a[aaaa - 1] == aa || a[aaaa + 1] == aa){if (a[aaaa] == aa)cout << aaaa;else if (a[aaaa - 1] == aa)cout << --aaaa;else cout << ++aaaa;k = 1;break;}else if (a[aaaa] > aa)cout << a[aaaa] << ends << aaa << endl, aaaa += aaa /= 2;elsecout << a[aaaa] << ends << aaa << endl, aaaa -= aaa /= 2;if (k)cout << ends << "查找:" << aa << endl; else cout << "-1" << ends << "查找:" << aa << endl;
}int a[] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }, aa = 11, aaa = 11;while(aaa)降序折半查找(a, aaa--, aa);int a[] = {17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }, aa = 17, aaa = 1;while(aaa<=aa)降序折半查找(a, aaa++, aa);
void 降序折半查找(int* a, int aa, int aaa)
{//缘由https://bbs.csdn.net/topics/399166569int aaaa = aaa /= 2; bool k = 0;if (a[0] == aa){ cout << 0, cout << ends << "查找:" << aa << endl; k = 1; return; }else if (a[aaa - 1] == aa){ cout << --aaa, cout << ends << "查找:" << aa << endl; k = 1; return; }while (aaa)if (a[aaaa] == aa){ cout << aaaa; k = 1; break; }else if (a[aaaa - 1] == aa){ cout << --aaaa; k = 1; break; }else if (a[aaaa + 1] == aa){ cout << ++aaaa; k = 1; break; }else if (a[aaaa] > aa)cout << a[aaaa] << ends << aaa << endl, aaaa += aaa /= 2;elsecout << a[aaaa] << ends << aaa << endl, aaaa -= aaa /= 2;if (k)cout << ends << "查找:" << aa << endl; else cout << -1 << ends << "查找:" << aa << endl;
}