C sorting explanation and trouble -
i'm having trouble solving problem. i'm supposed sort search array (through binary search) i'm having trouble in how size of array think.
int binarysearch( int arr[], int value) { int low = 0; int high =sizeof(arr)/sizeof(int); int mid = (low+high)/2; printf("%d\n",high); while (low <= high && arr[mid] != value) { if( arr[mid] < value) { low = mid+1; } else { high = mid-1; } mid = (low+high)/2; } if (low > high) { mid= -1; } return mid; } int main() { bubblesort( array, 10); int pos = binarysearch(array, 3); printf("the sorted array is: "); printarray( array, 10); printf("\n"); printf("now lets number 3\n"); printf("the number located in space %d\n", pos); printarray( array, 10); } but keep getting is:
./search 2 \\this wanted see getting size of array, 2??? sorted array is: 0 1 2 3 4 5 6 7 8 9 lets number 3 number located in space -1 0 1 2 3 4 5 6 7 8 9
using sizeof on array parameter function won't work expect.
when array passed function, decays pointer first element of array.
so this:
int binarysearch( int arr[], int value) is same this:
int binarysearch( int *arr, int value) and sizeof(arr) same calling sizeof(int *).
you need pass size of array separate parameter function.
Comments
Post a Comment