Skip to content
This repository has been archived by the owner on Oct 7, 2019. It is now read-only.

Added the interpolation code in C language #176

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file added search/interpolation_search/C/a.out
Binary file not shown.
75 changes: 75 additions & 0 deletions search/interpolation_search/C/interpolation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <stdio.h>

#define MAX 200

int interpolation_search(int a[], int bottom, int top, int item)

{

int mid;

while (bottom <= top) {

mid = bottom + (top - bottom) * ((item - a[bottom]) / (a[top] - a[bottom]));

if (item == a[mid])

return mid + 1;

if (item < a[mid])

top = mid - 1;

else

bottom = mid + 1;

}

return -1;

}

/* End of interpolation_search() */



/* The main() begins */

int main()

{

int arr[MAX];

int i, num;

int item, pos;

printf("\nEnter total elements (num< %d) : ", MAX);

scanf("%d", &num);

printf("Enter %d Elements in ascending order: ", num);

for (i = 0; i < num; i++)

scanf("%d", &arr[i]);

printf("\nSearch For : ");

scanf("%d", &item);

pos = interpolation_search(&arr[0], 0, num - 1, item);

if (pos == -1)

printf("\nElement %d not found\n", item);

else

printf("\nElement %d found at position %d\n", item, pos);

return 0;

}