相关疑难解决方法(0)

在哨兵中使用线性搜索有什么意义?

我的目标是了解为什么与哨兵一起使用线性搜索比使用标准线性搜索更可取。

#include <stdio.h>

int linearSearch(int array[], int length) {
    int elementToSearch;
    printf("Insert the element to be searched: ");
    scanf("%d", &elementToSearch);

    for (int i = 0; i < length; i++) {
        if (array[i] == elementToSearch) {
            return i; // I found the position of the element requested
        }
    }
    return -1; // The element to be searched is not in the array
}

int main() {
    int myArray[] = {2, 4, 9, 2, 9, 10};
    int myArrayLength = 6;
    linearSearch(myArray, myArrayLength); …
Run Code Online (Sandbox Code Playgroud)

c linear-search

0
推荐指数
1
解决办法
4146
查看次数

标签 统计

c ×1

linear-search ×1