void *studentsCount(int *Arr, int len, int score, int *lessCount, int *moreCount) { if (Arr == NULL || len<0) return NULL; if (Arr[0] == Arr[len - 1]) { if (len>1 || (len == 1 && score == Arr[0])) { *lessCount = 0; *moreCount = 0; } if (score > Arr[0]) { *lessCount = 1; *moreCount = 0; } if (score < Arr[0]) { *lessCount = 0; *moreCount = 1; } } else { int k = interpolationSearch(Arr, len, score); if (k < len) { *lessCount = k; *moreCount = len - k - 1; } else { k = k / len; *lessCount = k ; *moreCount = len - k ; } } }
int main() { int i; int comp1, comp2; int location1, location2; for (i = 0; i < MAX + 3; i++) { if (i == 0) { location1 = interpolationSearch(1, &comp1); location2 = binarySearch(1, &comp2); } else if (i == MAX + 1) { location1 = interpolationSearch(100, &comp1); location2 = interpolationSearch(100, &comp2); } else if (i == MAX + 2) { location1 = interpolationSearch(20, &comp1); location2 = interpolationSearch(20, &comp2); } else { location1 = interpolationSearch(list[i - 1], &comp1); location2 = binarySearch(list[i - 1], &comp2); } if (location1 != -1) printf("Interpolation search found element at %d\nAfter %d comparisons\n", location1 + 1, comp1); else printf("Interpolation search could not find element.\nAfter %d comparisons\n", comp1); if (location2 != -1) printf("Binary search found element at %d\nAfter %d comparisons\n", location2 + 1, comp2); else printf("Binary search could not find element.\nAfter %d comparisons\n", comp2); } return 0; }
void interpolationSearch_test(){ int n, data, A[10]={3,6,9,12,15,18,21,24,27,30}; n =10; data =20; printf("\nElement %d is at location: %d (-1 indicates not present in array)", data, interpolationSearch(A, n, data)); data =30; printf("\nElement %d is at location: %d (-1 indicates not present in array)", data, interpolationSearch(A, n, data)); }