我不明白为什么我的递归函数没有按预期运行

海登·拉布里

我正在尝试创建一个函数,该函数返回数组中给定整数的最右边位置的位置。示例:[1,2,3,4,5,6] 找到 4 的最右边位置将返回 4。 [1,2,3,4,4,4,5,6] 找到 4 的最右边位置将返回6.

我正在尝试在我的函数中实现递归调用。打印出递归调用时,我看到了正确的位置,尽管我最终无法返回该号码。

#include <stdio.h>

int RightMostBinarySearch(int *arr, int length, int find, int i, int j) {
    int middle = (i + j) / 2; //This will be floor due to integer data type
    while(i <= j){ //While the start does not excede int size of last value in array
        if(arr[middle] < find){ //If middle element is less than what is being searched for
            i = middle + 1; //Obviously the element is not found and the element is greater than middle point => make i one element to the right
        }
        else if(arr[middle] == find){ //The middle position is where the element exists in the array
            printf("%d\n", RightMostBinarySearch(arr, length, find, middle + 1, j));
            return middle + 1;
        }
        else{ //This condition will be when arr[midd] > find
            j = middle - 1; // make j 1 element left of middle because find is less than arr[middle]
        }
        middle = (i + j) / 2; //if not found i or j changes, thus middle must also change.
    }
    return -1;
}

int main(void) {
    int arr[] = { 1, 2, 4, 4, 4, 4, 4, 9, 12 }; //Sorted int array of size n
    int find = 4;
    int length = sizeof(arr) / sizeof(*arr); // Determines the length by getting the full size of memory array uses and dividing by he size of first element memory size. Full memory / element memory = num elements = length
    int i = 0;
    int j = length - 1; // Length of array is n, last element is represented n - 1
    int location = RightMostBinarySearch(arr,length, find, i, j);
    printf("The location of the element is at position: %d\n", location);
    return 0;
}
chqrlie

当函数RightMostBinarySearch递归时,你打印它的返回值,但总是 return middle + 1,这甚至可能不是与find值出现的偏移量。

您应该这样修改函数:

int RightMostBinarySearch(int *arr, int length, int find, int i, int j) {
    //While the start does not exceed int size of last value in array
    while (i <= j) {
        // This will be floor due to integer data type
        // Also avoid potential integer overflow in i+j
        // make sure the middle element is > i unless i == j
        int middle = i + (j - i + 1) / 2;
        //If middle element is less than what is being searched for
        if (arr[middle] < find) {
            //Obviously the element is not found and the element is greater than middle point => make i one element to the right
            i = middle + 1;
        } else
        if (arr[middle] == find) {
            //The middle position is where the element exists in the array
            if (middle == j) {
                /* middle is the last possible value */
                return middle;
            } else {
                return RightMostBinarySearch(arr, length, find, middle, j));
            }
        } else {
            //This condition will be when arr[midd] > find
            j = middle - 1; // make j 1 element left of middle because find is less than arr[middle]
        }
    }
    return -1;
}

请注意,无需递归函数,使用更简单的 API 即可轻松解决此问题:

#include <stdio.h>

int LeftMostBinarySearch(const int *arr, int length, int find) {
    int i = 0;
    int j = length;

    while (i < j) {
        // compute mid-point avoiding potential overflow on i+j
        int middle = i + (j - i) / 2;
        if (arr[middle] < find) {
            i = middle + 1;
        } else {
            j = middle;
        }
    }
    if (i < length && arr[i] == find)
        return i;
    else
        return -1;
}

int RightMostBinarySearch(const int *arr, int length, int find) {
    int i = 0;
    int j = length;

    while (i < j) {
        // compute mid-point avoiding potential overflow on i+j
        int middle = i + (j - i) / 2;
        if (arr[middle] <= find) {
            i = middle + 1;
        } else {
            j = middle;
        }
    }
    if (i > 0 && arr[i - 1] == find)
        return i - 1;
    else
        return -1;
}

int main() {
    int arr[] = { 1, 2, 4, 4, 4, 4, 4, 9, 12 }; //Sorted int array
    int length = sizeof(arr) / sizeof(*arr); // Determines the length by getting the full size of memory array and dividing by the size of first element. Full memory / element memory = num elements = length
    int find = 4;
    int left_location = LeftMostBinarySearch(arr, length, find);
    int right_location = RightMostBinarySearch(arr, length, find);
    printf("The first element %d is at position: %d\n", find, left_location);
    printf("The last element %d is at position: %d\n", find, right_location);
    return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

不明白为什么我的函数没有被调用

来自分类Dev

Java - 递归就停止了,我不明白为什么

来自分类Dev

不明白为什么 replace() 方法没有按预期工作

来自分类Dev

我有分段错误,我不明白为什么

来自分类Dev

我不明白为什么这段代码不能运行?

来自分类Dev

(FLASK) 我不明白为什么我的 html 文件没有更新/渲染

来自分类Dev

我不明白为什么我没有MVC AJAX

来自分类Dev

我不明白为什么气球在我制作的小游戏中没有移动

来自分类Dev

我的变量没有除,我不明白为什么

来自分类Dev

不明白为什么我不能调用函数

来自分类Dev

我不明白为什么编译

来自分类Dev

我不明白为什么文件没有写到挂载的分区上

来自分类Dev

我不明白为什么这两个元素没有并排对齐?

来自分类Dev

我不明白为什么最后阶段没有任何保存或追加数据操作

来自分类Dev

Laravel 叶片和顺风没有正确混合,我不明白为什么

来自分类Dev

我不明白为什么 TypeScript 没有在这里抛出错误

来自分类Dev

我不明白为什么在这个给定的程序中 a 的值没有增加

来自分类Dev

我不明白为什么我的屏幕上没有输出这个使用 c 语言数组的简单堆栈实现

来自分类Dev

我不明白为什么我的代码没有采用输入框的值并创建新对象

来自分类Dev

我不明白为什么我的 python 代码没有以英里为单位返回答案

来自分类Dev

我不明白为什么我的波形是这样出来的

来自分类Dev

我不明白为什么我的团队失败了

来自分类Dev

我不明白为什么我的 super() 产生错误

来自分类Dev

Java / Android:我不明白为什么它比预期的要慢

来自分类Dev

我不明白为什么我的.find函数不起作用

来自分类Dev

我不明白为什么我的 JS 函数会导致错误

来自分类Dev

我不明白为什么它向我显示函数、缩进和其他错误

来自分类Dev

不明白为什么我的异步等待在我的 lambda 函数中不起作用

来自分类Dev

Typescript Map <enum,set <enum >>“此调用没有重载”,但是我不明白为什么?

Related 相关文章

  1. 1

    不明白为什么我的函数没有被调用

  2. 2

    Java - 递归就停止了,我不明白为什么

  3. 3

    不明白为什么 replace() 方法没有按预期工作

  4. 4

    我有分段错误,我不明白为什么

  5. 5

    我不明白为什么这段代码不能运行?

  6. 6

    (FLASK) 我不明白为什么我的 html 文件没有更新/渲染

  7. 7

    我不明白为什么我没有MVC AJAX

  8. 8

    我不明白为什么气球在我制作的小游戏中没有移动

  9. 9

    我的变量没有除,我不明白为什么

  10. 10

    不明白为什么我不能调用函数

  11. 11

    我不明白为什么编译

  12. 12

    我不明白为什么文件没有写到挂载的分区上

  13. 13

    我不明白为什么这两个元素没有并排对齐?

  14. 14

    我不明白为什么最后阶段没有任何保存或追加数据操作

  15. 15

    Laravel 叶片和顺风没有正确混合,我不明白为什么

  16. 16

    我不明白为什么 TypeScript 没有在这里抛出错误

  17. 17

    我不明白为什么在这个给定的程序中 a 的值没有增加

  18. 18

    我不明白为什么我的屏幕上没有输出这个使用 c 语言数组的简单堆栈实现

  19. 19

    我不明白为什么我的代码没有采用输入框的值并创建新对象

  20. 20

    我不明白为什么我的 python 代码没有以英里为单位返回答案

  21. 21

    我不明白为什么我的波形是这样出来的

  22. 22

    我不明白为什么我的团队失败了

  23. 23

    我不明白为什么我的 super() 产生错误

  24. 24

    Java / Android:我不明白为什么它比预期的要慢

  25. 25

    我不明白为什么我的.find函数不起作用

  26. 26

    我不明白为什么我的 JS 函数会导致错误

  27. 27

    我不明白为什么它向我显示函数、缩进和其他错误

  28. 28

    不明白为什么我的异步等待在我的 lambda 函数中不起作用

  29. 29

    Typescript Map <enum,set <enum >>“此调用没有重载”,但是我不明白为什么?

热门标签

归档