Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- void sort(int a[],int n){
- int i, j;
- for(i = 1; i < n; i++){
- int temp = a[i];
- for(j = i-1; j >= 0 && temp < a[j]; j--) a[j+1] = a[j];
- a[j+1] = temp;
- }
- }
- int search(int a[], int low, int high, int x){
- int mid;
- while(a[low]<=x && a[high]>=x){
- mid = low + ( (x - a[low]) * (high - low) ) / (a[high] - a[low]);
- if(a[mid] < x) low = mid + 1;
- else if(a[mid] > x) high = mid + 1;
- else return mid;
- }
- if(a[low] == x) return x;
- else return -1;
- }
- int main(){
- int a[20];
- int length,x;
- printf("Enter the number of numbers: ");
- scanf("%d", &length);
- printf("Enter the numbers:-\n");
- for(int i = 0; i<length; i++) scanf("%d", &a[i]);
- sort(a,length);
- for(int i = 0; i<length; i++) printf("%d\t", a[i]);
- printf("\nEnter the element to search: ");
- scanf("%d", &x);
- int result = search(a, 0, length-1, x);
- if(result != -1) printf("%d found at %d", x, result);
- else printf("Not Found!");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement