C++ <二分查找法>

注:二分查找法有个问题,一组数字中有多个一样的数值时,其返回的下标位置不一样是数组中第一次出现的线标位置。
运行结果:

 Enter digit numbers less than 10: -5 5 -5 33 -10 4 1 33 5
  After sort: -10 -5 -5 1 4 5 5 33 33
 Enter you want to find number: -5
 Found it, the index is: 1

 Enter digit numbers less than 10: -5 5 -5 33 -10 4 1 33 5
 After sort: -10 -5 -5 1 4 5 5 33 33
 Enter you want to find number: 5
 Found it, the index is: 6

search.cpp

#include <iostream>

const int SIZE = 10;

void fillArray(int a[], int& usedSize);
void insertSort(int a[], const int& usedSize);
void show(int a[], const int& usedSize);
bool findVal(int a[], int first, int last, int& pos, const int& find);

int main() {
    using std::cout;
    using std::cin;

    int usedSize = 0, pos, find;
    int a[SIZE];
    cout << "Enter digit numbers less than 10: ";
    fillArray(a, usedSize);

    cout << "After sort: ";
    insertSort(a, usedSize);
    show(a, usedSize);

    cout << "Enter you want to find number: ";
    cin >> find;
    if (findVal(a, 0, usedSize - 1, pos, find))
        cout << "Found it, the index is: " << pos << '\n';
    else
        cout << "Not found.\n";

    return 0;
}


void fillArray(int a[], int& usedSize) {
    using std::cin;

    char c;
    for (int i = 0; i < SIZE && (c = cin.get()) != '\n'; i++) {
        cin.putback(c);
        cin >> a[i];
        usedSize++;
    }
}

void insertSort(int a[], const int& usedSize) {
    int temp, i, j;
    for (i = 1; i < usedSize; i++) {
        temp = a[i];
        for (j = i - 1; j >= 0 && temp < a[j]; j--)
            a[j + 1] = a[j];
        a[j + 1] = temp;
    }
}

void show(int a[], const int& usedSize) {
    using std::cout;

    for (int i = 0; i < usedSize; i++) {
        cout << a[i] << ' ';
    }
    cout << '\n';
}

bool findVal(int a[], int first, int last, int& pos, const int& find) {
    int mid;
    while (first <= last) {
        mid = (first + last) / 2;
        if (a[mid] == find) {
            pos = mid;
            return true;
        }
        else if (a[mid] < find)
            first = mid + 1;
        else
            last = mid - 1;
    }

    return false;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用二分查找C++中查找元素的例子[^1]: ```cpp #include <iostream> #include <vector> using namespace std; int binarySearch(vector<int>& nums, int target) { int left = 0; int right = nums.size() - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] == target) { return mid; } else if (nums[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; } int main() { vector<int> nums = {1, 3, 5, 7, 9}; int target = 5; int index = binarySearch(nums, target); if (index != -1) { cout << "Element found at index " << index << endl; } else { cout << "Element not found" << endl; } return 0; } ``` 这个例子演示了如何使用二分查找在一个有序数组中查找指定元素。首先,我们定义了一个`binarySearch`函数,它接受一个有序数组`nums`和目标元素`target`作为参数。然后,我们使用两个指针`left`和`right`来表示查找区间的左右边界。在每一次循环中,我们计算中间元素的索引`mid`,并将其与目标元素进行比较。如果中间元素等于目标元素,则返回中间元素的索引。如果中间元素小于目标元素,则将左边界指针`left`更新为`mid + 1`,否则将右边界指针`right`更新为`mid - 1`。如果循环结束时仍然没有找到目标元素,则返回-1表示未找到。 在`main`函数中,我们定义了一个有序数组`nums`和目标元素`target`,然后调用`binarySearch`函数进行查找。如果返回的索引不等于-1,则打印出元素找到的位置,否则打印出元素未找到的提示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值