C中的Bsearch功能无法正常工作

时间:2022-08-25 19:35:24

Basically I'm creating a program that will output the number if it is found in an given array or output -1 if not found. (Sorted Array.)

基本上我正在创建一个程序,如果在给定数组中找到该数字将输出该数字,如果未找到则输出-1。 (排序数组。)

#include <stdio.h>
#include <stdlib.h>

int cmp(const void*a,const void *b){
    if(*(int*)a-*(int*)b>0) return 1;
    if(*(int*)a-*(int*)b<0) return -1;
    return 0;
}

int main() {
    int n; scanf("%d",&n);
    int a[n];

    for(int i =0;i<n;i++) 
        scanf("%d",a+i);

    for(int i =0;i<n;i++){
        int *item;
        item = (int*)bsearch(&i,a,n,sizeof(int),cmp);
        if(item!=NULL) printf("%d ",i);
        else printf("-1 "); 
    }

    return 0;
}

INPUT : 10

输入:10

-1 -1 6 1 9 3 2 -1 4 -1

-1 -1 6 1 9 3 2 -1 4 -1

OUTPUT : -1 1 2 3 4 -1 6 -1 -1 9

输出:-1 1 2 3 4 -1 6 -1 -1 9

My OUTPUT : -1 -1 -1 3 4 -1 -1 -1 -1 -1

我的输出:-1 -1 -1 3 4 -1 -1 -1 -1 -1

1 个解决方案

#1


0  

https://www.tutorialspoint.com/c_standard_library/c_function_bsearch.htm

The C library function void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *)) function searches an array of nitems objects, the initial member of which is pointed to by base, for a member that matches the object pointed to, by key. The size of each member of the array is specified by size.

C库函数void * bsearch(const void * key,const void * base,size_t nitems,size_t size,int(* compar)(const void *,const void *))函数搜索nitems对象的数组,初始成员对于与按键指向的对象匹配的成员,由base指向。数组的每个成员的大小由size指定。

The contents of the array should be in ascending sorted order according to the comparison function referenced by compar.

根据compar引用的比较函数,数组的内容应按升序排序。

The whole point of binary searching is that if the array has size size you start in position size/2. If this element is less than what you're looking for, then go to size/2 + size/4, and otherwise go to size/2 - size/4. If this approach should work, the array needs to be sorted.

二进制搜索的重点是,如果数组具有大小,则从位置大小/ 2开始。如果此元素小于您要查找的元素,则转到size / 2 + size / 4,否则转到size / 2 - size / 4。如果这种方法起作用,则需要对数组进行排序。

Read more about binary searching here: https://en.wikipedia.org/wiki/Binary_search_algorithm

在此处阅读有关二进制搜索的更多信息:https://en.wikipedia.org/wiki/Binary_search_algorithm

And as mentioned in comments, scanf("%d",a+i) is correct, but scanf("%d",&a[i]) is preferable.

并且如评论中所述,scanf(“%d”,a + i)是正确的,但scanf(“%d”,&a [i])是优选的。

#1


0  

https://www.tutorialspoint.com/c_standard_library/c_function_bsearch.htm

The C library function void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *)) function searches an array of nitems objects, the initial member of which is pointed to by base, for a member that matches the object pointed to, by key. The size of each member of the array is specified by size.

C库函数void * bsearch(const void * key,const void * base,size_t nitems,size_t size,int(* compar)(const void *,const void *))函数搜索nitems对象的数组,初始成员对于与按键指向的对象匹配的成员,由base指向。数组的每个成员的大小由size指定。

The contents of the array should be in ascending sorted order according to the comparison function referenced by compar.

根据compar引用的比较函数,数组的内容应按升序排序。

The whole point of binary searching is that if the array has size size you start in position size/2. If this element is less than what you're looking for, then go to size/2 + size/4, and otherwise go to size/2 - size/4. If this approach should work, the array needs to be sorted.

二进制搜索的重点是,如果数组具有大小,则从位置大小/ 2开始。如果此元素小于您要查找的元素,则转到size / 2 + size / 4,否则转到size / 2 - size / 4。如果这种方法起作用,则需要对数组进行排序。

Read more about binary searching here: https://en.wikipedia.org/wiki/Binary_search_algorithm

在此处阅读有关二进制搜索的更多信息:https://en.wikipedia.org/wiki/Binary_search_algorithm

And as mentioned in comments, scanf("%d",a+i) is correct, but scanf("%d",&a[i]) is preferable.

并且如评论中所述,scanf(“%d”,a + i)是正确的,但scanf(“%d”,&a [i])是优选的。