This is my code.
65 int BinarySearch(int key, int array[], int min, int max)
66 {
67 int mid;
68
69 if (max < min)
70 {
71 return -1;
72 }
73 else
74 {
75 mid = ( max + min ) / 2;
76 if (array[mid] < key)
77 {
78 BinarySearch(key, array, mid + 1, max);
79 }
80 else if (array[mid] > key)
81 {
82 BinarySearch(key, array, min, mid - 1);
83 }
84 else
85 {
86 return 0;
87 }
88 }
89 return 1;
90 }
And when I use it to search for a number that is not in the array the code eventually reaches the "return -1;" line inside the "if (max < min)" condition. At that point I expected this function to stop and go back to the calling function. But instead, GDB shows that it keeps going.
75 if (array[mid] < key)
(gdb) n
77 BinarySearch(key, array, mid + 1, max);
(gdb) n
Breakpoint 2, BinarySearch (key=6, array=0x7ffffffbdd60, min=5, max=4) at helpers.c:68
68 if (max < min)
(gdb) n
70 return -1;
(gdb) n
89 }(gdb) n
BinarySearch (key=6, array=0x7ffffffbdd60, min=4, max=4) at helpers.c:78
78 }
(gdb) n
88 return 1;
(gdb) n
89 }(gdb) n
BinarySearch (key=6, array=0x7ffffffbdd60, min=3, max=4) at helpers.c:78
78 }
I must be overlooking something simple. Thanks.