二分查找 Binary Search

来源:互联网 发布:软件外包学校 编辑:程序博客网 时间:2024/04/29 01:44

写在最前

其实关于二分查找,网上已经有很多很多了;
这里仅仅是把一些有特殊要求的二分查找总结一下;
简单的测试好像并没有什么错;
如果有,希望能提出;

代码

#include <iostream>#include <vector>#include <iomanip>using namespace std;vector<int> test;int binarySearchIncreaseFirstTarget(int l, int r, int target) {  // 在不下降的序列中寻找target第一次出现位置    if (test.size() == 0) return -1;    while (l < r) {        int m = l + ((r - l) >> 1);        if (test[m] < target) l = m + 1;        else r = m;    }    if (test[l] == target) return l;    else return -1;}int binarySearchIncreaseLastTarget(int l, int r, int target) {  // 在不下降的序列中寻找target最后一次出现位置    if (test.size() == 0) return -1;    while (l < r - 1) {        int m = l + ((r - l) >> 1);        if (test[m] > target) r = m - 1;        else l = m;    }    if (test[r] == target) return r;    else if (test[l] == target) return l;    else return -1;}int binarySearchIncreaseNthTarget(int l, int r, int target, int n) {  // 在不下降的序列中寻找target第N次出现位置    if (test.size() == 0) return -1;    int first = binarySearchIncreaseFirstTarget(l, r, target);    if (first == -1) return -1;    int last = binarySearchIncreaseLastTarget(l, r, target);    if (first + n - 1 > last) return -1;    return first + n - 1;}int binarySearchIncreaseTargetNum(int l, int r, int target) {  // 在不下降的序列中寻找target出现的次数    if (test.size() == 0) return 0;    int first = binarySearchIncreaseFirstTarget(l, r, target);    if (first == -1) return 0;    int last = binarySearchIncreaseLastTarget(l, r, target);    return last - first + 1;}int binarySearchIncreaseLastSmaller(int l, int r, int target) {  // 在不下降的序列中寻找恰好比target小的数出现位置,也即最后一个比target小的数出现的位置    if (test.size() == 0) return -1;    while (l < r - 1) {        int m = l + ((r - l) >> 1);        if (test[m] < target) l = m;        else r = m - 1;    }    if (test[r] < target) return r;    else if (test[l] < target) return l;    else return -1;}int binarySearchIncreaseFirstBigger(int l, int r, int target) {  // 在不下降的序列中寻找恰好比target大的数出现位置,也即第一个比target大的数出现的位置    if (test.size() == 0) return -1;    while (l < r) {        int m = l + ((r - l) >> 1);        if (test[m] <= target) l = m + 1;        else r = m;    }    if (test[r] > target) return r;    else return -1;}//注意到并没写binarySearchIncreaseFirstSmaller()这种函数,因为如果target <= test[0], 那么显然返回-1,否则返回0//注意到并没写binarySearchIncreaseLastBigger()这种函数,因为如果target >= test[test.size() - 1],那么显然返回-1,否则返回test.size() - 1int binarySearchDecreaseFirstTarget(int l, int r, int target) {  // 在不上升的序列中寻找target第一次出现位置    if (test.size() == 0) return -1;    while (l < r) {        int m = l + ((r - l) >> 1);        if (test[m] > target) l = m + 1;        else r = m;    }    if (test[l] == target) return l;    else return -1;}int binarySearchDecreaseLastTarget(int l, int r, int target) {  // 在不上升的序列中寻找target最后一次出现位置    if (test.size() == 0) return -1;    while (l < r - 1) {        int m = l + ((r - l) >> 1);        if (test[m] < target) r = m - 1;        else l = m;    }    if (test[r] == target) return r;    else if (test[l] == target) return l;    else return -1;}int binarySearchDecreaseNthTarget(int l, int r, int target, int n) {  // 在不上升的序列中寻找target第N次出现位置    if (test.size() == 0) return -1;    int first = binarySearchDecreaseFirstTarget(l, r, target);    if (first == -1) return -1;    int last = binarySearchDecreaseLastTarget(l, r, target);    if (first + n - 1 > last) return -1;    return first + n - 1;}int binarySearchDecreaseTargetNum(int l, int r, int target) {  // 在不上升的序列中寻找target出现的次数    if (test.size() == 0) return 0;    int first = binarySearchDecreaseFirstTarget(l, r, target);    if (first == -1) return 0;    int last = binarySearchDecreaseLastTarget(l, r, target);    return last - first + 1;}int binarySearchDecreaseLastBigger(int l, int r, int target) {  // 在不上升的序列中寻找恰好比target大的数出现位置,也即最后一个比target大的数出现的位置    if (test.size() == 0) return -1;    while (l < r - 1) {        int m = l + ((r - l) >> 1);        if (test[m] > target) l = m;        else r = m - 1;    }    if (test[r] > target) return r;    else if (test[l] > target) return l;    else return -1;}int binarySearchDecreaseFirstSmaller(int l, int r, int target) {  // 在不上升的序列中寻找恰好比target小的数出现位置,也即第一个比target小的数出现的位置    if (test.size() == 0) return -1;    while (l < r) {        int m = l + ((r - l) >> 1);        if (test[m] >= target) l = m + 1;        else r = m;    }    if (test[r] < target) return r;    else return -1;}//注意到并没写binarySearchDecreaseFirstBigger()这种函数,因为如果target > test[0], 那么显然返回-1,否则返回0//注意到并没写binarySearchDecreaseLastSmaller()这种函数,因为如果target <= test[test.size() - 1],那么显然返回-1,否则返回test.size() - 1void Test() {    cout << "Test for increase test:" << endl;    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////test 1 for increase vector    cout << endl << "Test 1:" << endl;  // the number of test,size() is odd    int t1[] = {1, 2, 2, 4, 4, 4, 5};    test = vector<int>(t1, t1 + 7);    cout << "The test vector: " << endl;    cout << "Index:                           ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << i;    cout << endl;    cout << "Data:                            ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << test[i];    cout << endl;    cout << "Target:                          ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << test[i];    cout << setw(4) << test[0] - 3 << setw(4) << test[test.size() - 1] + 3 << setw(4) << 3;    cout << endl;    cout << "Search result: " << endl;    cout << "binarySearchIncreaseFirstTarget: ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchIncreaseFirstTarget(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchIncreaseFirstTarget(0, test.size() - 1, test[0] - 3)          << setw(4) << binarySearchIncreaseFirstTarget(0, test.size() - 1, test[test.size() - 1] + 3)         << setw(4) << binarySearchIncreaseFirstTarget(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchIncreaseLastTarget:  ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchIncreaseLastTarget(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchIncreaseLastTarget(0, test.size() - 1, test[0] - 3)          << setw(4) << binarySearchIncreaseLastTarget(0, test.size() - 1, test[test.size() - 1] + 3)         << setw(4) << binarySearchIncreaseLastTarget(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchIncreaseLastSmall:   ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchIncreaseLastSmaller(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchIncreaseLastSmaller(0, test.size() - 1, test[0] - 3)          << setw(4) << binarySearchIncreaseLastSmaller(0, test.size() - 1, test[test.size() - 1] + 3)         << setw(4) << binarySearchIncreaseLastSmaller(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchIncreaseFirstBigger: ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchIncreaseFirstBigger(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchIncreaseFirstBigger(0, test.size() - 1, test[0] - 3)          << setw(4) << binarySearchIncreaseFirstBigger(0, test.size() - 1, test[test.size() - 1] + 3)         << setw(4) << binarySearchIncreaseFirstBigger(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchIncreaseNthTarget:   " << endl;    for (int i = 1; i <= 3; i++) {        cout << "                           N = " << i << ":";        for (int j = 0; j < test.size(); j++) cout << setw(4) << binarySearchIncreaseNthTarget(0, test.size() - 1, test[j], i);        cout << setw(4) << binarySearchIncreaseNthTarget(0, test.size() - 1, test[0] - 3, i)              << setw(4) << binarySearchIncreaseNthTarget(0, test.size() - 1, test[test.size() - 1] + 3, i)             << setw(4) << binarySearchIncreaseNthTarget(0, test.size() - 1, 3, i);        cout << endl;    }    cout << "binarySearchIncreaseTargetNum:   ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchIncreaseTargetNum(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchIncreaseTargetNum(0, test.size() - 1, test[0] - 3)          << setw(4) << binarySearchIncreaseTargetNum(0, test.size() - 1, test[test.size() - 1] + 3)         << setw(4) << binarySearchIncreaseTargetNum(0, test.size() - 1, 3);    cout << endl;    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////test 2 for increase vector    cout << endl << "Test 2:" << endl;  // the number of test,size() is even    int t2[] = {1, 2, 2, 4, 4, 4};    test = vector<int>(t2, t2 + 6);    cout << "The test vector: " << endl;    cout << "Index:                           ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << i;    cout << endl;    cout << "Data:                            ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << test[i];    cout << endl;    cout << "Target:                          ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << test[i];    cout << setw(4) << test[0] - 3 << setw(4) << test[test.size() - 1] + 3 << setw(4) << 3;    cout << endl;    cout << "Search result: " << endl;    cout << "binarySearchIncreaseFirstTarget: ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchIncreaseFirstTarget(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchIncreaseFirstTarget(0, test.size() - 1, test[0] - 3)          << setw(4) << binarySearchIncreaseFirstTarget(0, test.size() - 1, test[test.size() - 1] + 3)         << setw(4) << binarySearchIncreaseFirstTarget(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchIncreaseLastTarget:  ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchIncreaseLastTarget(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchIncreaseLastTarget(0, test.size() - 1, test[0] - 3)          << setw(4) << binarySearchIncreaseLastTarget(0, test.size() - 1, test[test.size() - 1] + 3)         << setw(4) << binarySearchIncreaseLastTarget(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchIncreaseLastSmall:   ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchIncreaseLastSmaller(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchIncreaseLastSmaller(0, test.size() - 1, test[0] - 3)          << setw(4) << binarySearchIncreaseLastSmaller(0, test.size() - 1, test[test.size() - 1] + 3)         << setw(4) << binarySearchIncreaseLastSmaller(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchIncreaseFirstBigger: ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchIncreaseFirstBigger(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchIncreaseFirstBigger(0, test.size() - 1, test[0] - 3)          << setw(4) << binarySearchIncreaseFirstBigger(0, test.size() - 1, test[test.size() - 1] + 3)         << setw(4) << binarySearchIncreaseFirstBigger(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchIncreaseNthTarget:   " << endl;    for (int i = 1; i <= 3; i++) {        cout << "                           N = " << i << ":";        for (int j = 0; j < test.size(); j++) cout << setw(4) << binarySearchIncreaseNthTarget(0, test.size() - 1, test[j], i);        cout << setw(4) << binarySearchIncreaseNthTarget(0, test.size() - 1, test[0] - 3, i)              << setw(4) << binarySearchIncreaseNthTarget(0, test.size() - 1, test[test.size() - 1] + 3, i)             << setw(4) << binarySearchIncreaseNthTarget(0, test.size() - 1, 3, i);        cout << endl;    }    cout << "binarySearchIncreaseTargetNum:   ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchIncreaseTargetNum(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchIncreaseTargetNum(0, test.size() - 1, test[0] - 3)          << setw(4) << binarySearchIncreaseTargetNum(0, test.size() - 1, test[test.size() - 1] + 3)         << setw(4) << binarySearchIncreaseTargetNum(0, test.size() - 1, 3);    cout << endl;    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////test 3 for increase vector    cout << endl << "Test 3:" << endl;  // the number of test,size() is only one    int t3[] = {1};    test = vector<int>(t3, t3 + 1);    cout << "The test vector: " << endl;    cout << "Index:                           ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << i;    cout << endl;    cout << "Data:                            ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << test[i];    cout << endl;    cout << "Target:                          ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << test[i];    cout << setw(4) << test[0] - 3 << setw(4) << test[test.size() - 1] + 3 << setw(4) << 3;    cout << endl;    cout << "Search result: " << endl;    cout << "binarySearchIncreaseFirstTarget: ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchIncreaseFirstTarget(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchIncreaseFirstTarget(0, test.size() - 1, test[0] - 3)          << setw(4) << binarySearchIncreaseFirstTarget(0, test.size() - 1, test[test.size() - 1] + 3)         << setw(4) << binarySearchIncreaseFirstTarget(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchIncreaseLastTarget:  ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchIncreaseLastTarget(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchIncreaseLastTarget(0, test.size() - 1, test[0] - 3)          << setw(4) << binarySearchIncreaseLastTarget(0, test.size() - 1, test[test.size() - 1] + 3)         << setw(4) << binarySearchIncreaseLastTarget(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchIncreaseLastSmall:   ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchIncreaseLastSmaller(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchIncreaseLastSmaller(0, test.size() - 1, test[0] - 3)          << setw(4) << binarySearchIncreaseLastSmaller(0, test.size() - 1, test[test.size() - 1] + 3)         << setw(4) << binarySearchIncreaseLastSmaller(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchIncreaseFirstBigger: ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchIncreaseFirstBigger(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchIncreaseFirstBigger(0, test.size() - 1, test[0] - 3)          << setw(4) << binarySearchIncreaseFirstBigger(0, test.size() - 1, test[test.size() - 1] + 3)         << setw(4) << binarySearchIncreaseFirstBigger(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchIncreaseNthTarget:   " << endl;    for (int i = 1; i <= 3; i++) {        cout << "                           N = " << i << ":";        for (int j = 0; j < test.size(); j++) cout << setw(4) << binarySearchIncreaseNthTarget(0, test.size() - 1, test[j], i);        cout << setw(4) << binarySearchIncreaseNthTarget(0, test.size() - 1, test[0] - 3, i)              << setw(4) << binarySearchIncreaseNthTarget(0, test.size() - 1, test[test.size() - 1] + 3, i)             << setw(4) << binarySearchIncreaseNthTarget(0, test.size() - 1, 3, i);        cout << endl;    }    cout << "binarySearchIncreaseTargetNum:   ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchIncreaseTargetNum(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchIncreaseTargetNum(0, test.size() - 1, test[0] - 3)          << setw(4) << binarySearchIncreaseTargetNum(0, test.size() - 1, test[test.size() - 1] + 3)         << setw(4) << binarySearchIncreaseTargetNum(0, test.size() - 1, 3);    cout << endl;    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////test 4 for increase vector    cout << endl << "Test 4:" << endl;  // the number of test,size() is zero    int * t4 = NULL;    test = vector<int>(t4, t4 + 0);    cout << "The test vector: " << endl;    cout << "Index:                           ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << i;    cout << endl;    cout << "Data:                            ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << test[i];    cout << endl;    cout << "Target:                          ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << test[i];    cout << setw(4) << -3 << setw(4) << 0 << setw(4) << 3;    cout << endl;    cout << "Search result: " << endl;    cout << "binarySearchIncreaseFirstTarget: ";    cout << setw(4) << binarySearchIncreaseFirstTarget(0, test.size() - 1, -3)          << setw(4) << binarySearchIncreaseFirstTarget(0, test.size() - 1, 0)         << setw(4) << binarySearchIncreaseFirstTarget(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchIncreaseLastTarget:  ";    cout << setw(4) << binarySearchIncreaseLastTarget(0, test.size() - 1, -3)          << setw(4) << binarySearchIncreaseLastTarget(0, test.size() - 1, 0)         << setw(4) << binarySearchIncreaseLastTarget(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchIncreaseLastSmall:   ";    cout << setw(4) << binarySearchIncreaseLastSmaller(0, test.size() - 1, -3)          << setw(4) << binarySearchIncreaseLastSmaller(0, test.size() - 1, 0)         << setw(4) << binarySearchIncreaseLastSmaller(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchIncreaseFirstBigger: ";    cout << setw(4) << binarySearchIncreaseFirstBigger(0, test.size() - 1, -3)          << setw(4) << binarySearchIncreaseFirstBigger(0, test.size() - 1, 0)         << setw(4) << binarySearchIncreaseFirstBigger(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchIncreaseNthTarget:   " << endl;    for (int i = 1; i <= 3; i++) {        cout << "                           N = " << i << ":";        cout << setw(4) << binarySearchIncreaseNthTarget(0, test.size() - 1, -3, i)              << setw(4) << binarySearchIncreaseNthTarget(0, test.size() - 1, 0, i)             << setw(4) << binarySearchIncreaseNthTarget(0, test.size() - 1, 3, i);        cout << endl;    }    cout << "binarySearchIncreaseTargetNum:   ";    cout << setw(4) << binarySearchIncreaseTargetNum(0, test.size() - 1, -3)          << setw(4) << binarySearchIncreaseTargetNum(0, test.size() - 1, 0)         << setw(4) << binarySearchIncreaseTargetNum(0, test.size() - 1, 3);    cout << endl;    cout << endl << "Test for decrease test:" << endl;    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////test 1 for decrease vector    cout << endl << "Test 5:" << endl;  // the number of test,size() is odd    int t5[] = {5, 4, 4, 4, 2, 2, 1};    test = vector<int>(t5, t5 + 7);    cout << "The test vector: " << endl;    cout << "Index:                           ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << i;    cout << endl;    cout << "Data:                            ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << test[i];    cout << endl;    cout << "Target:                          ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << test[i];    cout << setw(4) << test[0] + 3 << setw(4) << test[test.size() - 1] - 3 << setw(4) << 3;    cout << endl;    cout << "Search result: " << endl;    cout << "binarySearchDecreaseFirstTarget: ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchDecreaseFirstTarget(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchDecreaseFirstTarget(0, test.size() - 1, test[0] + 3)          << setw(4) << binarySearchDecreaseFirstTarget(0, test.size() - 1, test[test.size() - 1] - 3)         << setw(4) << binarySearchDecreaseFirstTarget(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchDecreaseLastTarget:  ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchDecreaseLastTarget(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchDecreaseLastTarget(0, test.size() - 1, test[0] + 3)          << setw(4) << binarySearchDecreaseLastTarget(0, test.size() - 1, test[test.size() - 1] - 3)         << setw(4) << binarySearchDecreaseLastTarget(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchDecreaseLastBigger:  ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchDecreaseLastBigger(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchDecreaseLastBigger(0, test.size() - 1, test[0] + 3)          << setw(4) << binarySearchDecreaseLastBigger(0, test.size() - 1, test[test.size() - 1] - 3)         << setw(4) << binarySearchDecreaseLastBigger(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchDecreaseFirstSmaller:";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchDecreaseFirstSmaller(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchDecreaseFirstSmaller(0, test.size() - 1, test[0] + 3)          << setw(4) << binarySearchDecreaseFirstSmaller(0, test.size() - 1, test[test.size() - 1] - 3)         << setw(4) << binarySearchDecreaseFirstSmaller(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchDecreaseNthTarget:   " << endl;    for (int i = 1; i <= 3; i++) {        cout << "                           N = " << i << ":";        for (int j = 0; j < test.size(); j++) cout << setw(4) << binarySearchDecreaseNthTarget(0, test.size() - 1, test[j], i);        cout << setw(4) << binarySearchDecreaseNthTarget(0, test.size() - 1, test[0] + 3, i)              << setw(4) << binarySearchDecreaseNthTarget(0, test.size() - 1, test[test.size() - 1] - 3, i)             << setw(4) << binarySearchDecreaseNthTarget(0, test.size() - 1, 3, i);        cout << endl;    }    cout << "binarySearchDecreaseTargetNum:   ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchDecreaseTargetNum(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchDecreaseTargetNum(0, test.size() - 1, test[0] + 3)          << setw(4) << binarySearchDecreaseTargetNum(0, test.size() - 1, test[test.size() - 1] - 3)         << setw(4) << binarySearchDecreaseTargetNum(0, test.size() - 1, 3);    cout << endl;    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////test 6 for decrease vector    cout << endl << "Test 6:" << endl;  // the number of test,size() is even    int t6[] = {4, 4, 4, 2, 2, 1};    test = vector<int>(t6, t6 + 6);    cout << "The test vector: " << endl;    cout << "Index:                           ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << i;    cout << endl;    cout << "Data:                            ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << test[i];    cout << endl;    cout << "Target:                          ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << test[i];    cout << setw(4) << test[0] + 3 << setw(4) << test[test.size() - 1] - 3 << setw(4) << 3;    cout << endl;    cout << "Search result: " << endl;    cout << "binarySearchDecreaseFirstTarget: ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchDecreaseFirstTarget(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchDecreaseFirstTarget(0, test.size() - 1, test[0] + 3)          << setw(4) << binarySearchDecreaseFirstTarget(0, test.size() - 1, test[test.size() - 1] - 3)         << setw(4) << binarySearchDecreaseFirstTarget(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchDecreaseLastTarget:  ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchDecreaseLastTarget(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchDecreaseLastTarget(0, test.size() - 1, test[0] + 3)          << setw(4) << binarySearchDecreaseLastTarget(0, test.size() - 1, test[test.size() - 1] - 3)         << setw(4) << binarySearchDecreaseLastTarget(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchDecreaseLastBigger:  ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchDecreaseLastBigger(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchDecreaseLastBigger(0, test.size() - 1, test[0] + 3)          << setw(4) << binarySearchDecreaseLastBigger(0, test.size() - 1, test[test.size() - 1] - 3)         << setw(4) << binarySearchDecreaseLastBigger(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchDecreaseFirstSmaller:";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchDecreaseFirstSmaller(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchDecreaseFirstSmaller(0, test.size() - 1, test[0] + 3)          << setw(4) << binarySearchDecreaseFirstSmaller(0, test.size() - 1, test[test.size() - 1] - 3)         << setw(4) << binarySearchDecreaseFirstSmaller(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchDecreaseNthTarget:   " << endl;    for (int i = 1; i <= 3; i++) {        cout << "                           N = " << i << ":";        for (int j = 0; j < test.size(); j++) cout << setw(4) << binarySearchDecreaseNthTarget(0, test.size() - 1, test[j], i);        cout << setw(4) << binarySearchDecreaseNthTarget(0, test.size() - 1, test[0] + 3, i)              << setw(4) << binarySearchDecreaseNthTarget(0, test.size() - 1, test[test.size() - 1] - 3, i)             << setw(4) << binarySearchDecreaseNthTarget(0, test.size() - 1, 3, i);        cout << endl;    }    cout << "binarySearchDecreaseTargetNum:   ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchDecreaseTargetNum(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchDecreaseTargetNum(0, test.size() - 1, test[0] + 3)          << setw(4) << binarySearchDecreaseTargetNum(0, test.size() - 1, test[test.size() - 1] - 3)         << setw(4) << binarySearchDecreaseTargetNum(0, test.size() - 1, 3);    cout << endl;    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////test 7 for increase vector    cout << endl << "Test 7:" << endl;  // the number of test,size() is only one    int t7[] = {1};    test = vector<int>(t7, t7 + 1);    cout << "The test vector: " << endl;    cout << "Index:                           ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << i;    cout << endl;    cout << "Data:                            ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << test[i];    cout << endl;    cout << "Target:                          ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << test[i];    cout << setw(4) << test[0] + 3 << setw(4) << test[test.size() - 1] - 3 << setw(4) << 3;    cout << endl;    cout << "Search result: " << endl;    cout << "binarySearchDecreaseFirstTarget: ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchDecreaseFirstTarget(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchDecreaseFirstTarget(0, test.size() - 1, test[0] + 3)          << setw(4) << binarySearchDecreaseFirstTarget(0, test.size() - 1, test[test.size() - 1] - 3)         << setw(4) << binarySearchDecreaseFirstTarget(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchDecreaseLastTarget:  ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchDecreaseLastTarget(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchDecreaseLastTarget(0, test.size() - 1, test[0] + 3)          << setw(4) << binarySearchDecreaseLastTarget(0, test.size() - 1, test[test.size() - 1] - 3)         << setw(4) << binarySearchDecreaseLastTarget(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchDecreaseLastBigger:  ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchDecreaseLastBigger(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchDecreaseLastBigger(0, test.size() - 1, test[0] + 3)          << setw(4) << binarySearchDecreaseLastBigger(0, test.size() - 1, test[test.size() - 1] - 3)         << setw(4) << binarySearchDecreaseLastBigger(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchDecreaseFirstSmaller:";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchDecreaseFirstSmaller(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchDecreaseFirstSmaller(0, test.size() - 1, test[0] + 3)          << setw(4) << binarySearchDecreaseFirstSmaller(0, test.size() - 1, test[test.size() - 1] - 3)         << setw(4) << binarySearchDecreaseFirstSmaller(0, test.size() - 1, 3);    cout << endl;    cout << "binarySearchDecreaseNthTarget:   " << endl;    for (int i = 1; i <= 3; i++) {        cout << "                           N = " << i << ":";        for (int j = 0; j < test.size(); j++) cout << setw(4) << binarySearchDecreaseNthTarget(0, test.size() - 1, test[j], i);        cout << setw(4) << binarySearchDecreaseNthTarget(0, test.size() - 1, test[0] + 3, i)              << setw(4) << binarySearchDecreaseNthTarget(0, test.size() - 1, test[test.size() - 1] - 3, i)             << setw(4) << binarySearchDecreaseNthTarget(0, test.size() - 1, 3, i);        cout << endl;    }    cout << "binarySearchDecreaseTargetNum:   ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << binarySearchDecreaseTargetNum(0, test.size() - 1, test[i]);    cout << setw(4) << binarySearchDecreaseTargetNum(0, test.size() - 1, test[0] + 3)          << setw(4) << binarySearchDecreaseTargetNum(0, test.size() - 1, test[test.size() - 1] - 3)         << setw(4) << binarySearchDecreaseTargetNum(0, test.size() - 1, 3);    cout << endl;    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////test 8 for increase vector    cout << endl << "Test 8:" << endl;  // the number of test,size() is zero    int * t8 = NULL;    test = vector<int>(t8, t8 + 0);    cout << "The test vector: " << endl;    cout << "Index:                           ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << i;    cout << endl;    cout << "Data:                            ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << test[i];    cout << endl;    cout << "Target:                          ";    for (int i = 0; i < test.size(); i++) cout << setw(4) << test[i];    cout << setw(4) << 3 << setw(4) << 0 << setw(4) << -3;    cout << endl;    cout << "Search result: " << endl;    cout << "binarySearchDecreaseFirstTarget: ";    cout << setw(4) << binarySearchDecreaseFirstTarget(0, test.size() - 1, 3)          << setw(4) << binarySearchDecreaseFirstTarget(0, test.size() - 1, 0)         << setw(4) << binarySearchDecreaseFirstTarget(0, test.size() - 1, -3);    cout << endl;    cout << "binarySearchDecreaseLastTarget:  ";    cout << setw(4) << binarySearchDecreaseLastTarget(0, test.size() - 1, 3)          << setw(4) << binarySearchDecreaseLastTarget(0, test.size() - 1, 0)         << setw(4) << binarySearchDecreaseLastTarget(0, test.size() - 1, -3);    cout << endl;    cout << "binarySearchDecreaseLastBigger:  ";    cout << setw(4) << binarySearchDecreaseLastBigger(0, test.size() - 1, 3)          << setw(4) << binarySearchDecreaseLastBigger(0, test.size() - 1, 0)         << setw(4) << binarySearchDecreaseLastBigger(0, test.size() - 1, -3);    cout << endl;    cout << "binarySearchDecreaseFirstSmaller:";    cout << setw(4) << binarySearchDecreaseFirstSmaller(0, test.size() - 1, 3)          << setw(4) << binarySearchDecreaseFirstSmaller(0, test.size() - 1, 0)         << setw(4) << binarySearchDecreaseFirstSmaller(0, test.size() - 1, -3);    cout << endl;    cout << "binarySearchDecreaseNthTarget:   " << endl;    for (int i = 1; i <= 3; i++) {        cout << "                           N = " << i << ":";        cout << setw(4) << binarySearchDecreaseNthTarget(0, test.size() - 1, 3, i)              << setw(4) << binarySearchDecreaseNthTarget(0, test.size() - 1, 0, i)             << setw(4) << binarySearchDecreaseNthTarget(0, test.size() - 1, -3, i);        cout << endl;    }    cout << "binarySearchDecreaseTargetNum:   ";    cout << setw(4) << binarySearchDecreaseTargetNum(0, test.size() - 1, 3)          << setw(4) << binarySearchDecreaseTargetNum(0, test.size() - 1, 0)         << setw(4) << binarySearchDecreaseTargetNum(0, test.size() - 1, -3);    cout << endl;}int main() {    Test();    getchar();    getchar();    return 0;}

测试结果

这里写图片描述

1 1
原创粉丝点击