Posts

Binary Search Unleashed: Solving Problems with Precision

Image
BINARY SEARCH   CODE: #include<stdio.h> int main(){     int i, n, target, index = -1;     printf("Enter size of an array: ");     scanf("%d", &n);     int a[n];     printf("Enter the sorted array elements: ");     for(i = 0; i < n; i++) {         scanf("%d", &a[i]);     }     printf("The array elements are:\n");     for(i = 0; i < n; i++) {         printf("%d\n", a[i]);     }     printf("Enter the searching element: ");     scanf("%d", &target);     int first = 0, last = n - 1;     int mid;     while(first <= last) {         mid = (first + last) / 2;         if(a[mid] == target) {             index = mid;             printf("%d is found at %dth index", target, index);             return 0;         } else if(a[mid] < target) {             first = mid + 1;         } else {             last = mid - 1;         }     }     printf("%d is not found\n", target);    

Mastering Linear Search: Understanding the Basics

Image
LINEAR SEARCH CODE : #include<stdio.h> int main(){     int i,n,target;     int index=-1;     printf("Enter size of an array: ");     scanf("%d",&n);     int a[n];     printf("Enter the array elements: ");     for(i=0;i<n;i++){         scanf("%d",&a[i]);     }     printf("The array elements are:\n");     for(i=0;i<n;i++){         printf("%d\n",a[i]);     }     printf("Enter the searching element: ");     scanf("%d",&target);     if(target==a[0]){         printf("%d is found at %dth index",target,index++);     }else if(target==a[1]){         printf("%d is found at %dth index",target,index+=2);     }else if(target==a[2]){         printf("%d is found at %dth index",target,index+=3);     }else if(target==a[3]){         printf("%d is found at %dth index",target,index+=4);     }else if(target==a[4]){         printf("%d is found at %dth index"