Advertisement
Jaagdish47

Searching Techniques

Nov 23rd, 2024
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.98 KB | Source Code | 0 0
  1. Searching Techniques
  2. 4B. Write a program to search the element using sequential search.
  3. Search the element 10 and 100 in the following array using sequential search
  4. technique.
  5. 73,18,10,5,21,32,74
  6.  
  7. #include <stdio.h>
  8. #include <conio.h> // Required for Turbo C functions
  9.  
  10. int main() {
  11.     int a[7] = {73, 18, 10, 5, 21, 32, 74}; // Array initialization
  12.     int n = 7, i, data, found = 0;
  13.  
  14.     clrscr(); // Clear the screen for Turbo C
  15.  
  16.     // Display the array
  17.     printf("Array A: ");
  18.     for (i = 0; i < n; i++) {
  19.         printf("%d ", a[i]);
  20.     }
  21.  
  22.     // Prompt for the element to search
  23.     printf("\nEnter the element to be searched: ");
  24.     scanf("%d", &data);
  25.  
  26.     // Search for the element in the array
  27.     for (i = 0; i < n; i++) {
  28.         if (a[i] == data) {
  29.             printf("The Element Is Present At %d Position!\n", i);
  30.             found = 1;
  31.             break; // Exit the loop as the element is found
  32.         }
  33.     }
  34.  
  35.     // If the element is not found
  36.     if (!found) {
  37.         printf("The Element Is Not Present In The Array!\n");
  38.     }
  39.  
  40.     getch(); // Wait for a key press before exiting
  41.     return 0;
  42. }
  43.  
  44. 4C. Write a program to search the element using binary search.
  45. Search the element 250 in the following array using binary search technique.
  46. 10,20,25,51,75,100,250,500
  47.  
  48. #include <stdio.h>
  49. #include <conio.h> // Required for Turbo C functions
  50.  
  51. int main() {
  52.     int a[8] = {10, 20, 25, 51, 75, 100, 250, 500}; // Sorted array
  53.     int n = 8, data, i, lower_bound, upper_bound, mid;
  54.     int found = 0; // Flag to indicate if the element is found
  55.  
  56.     clrscr(); // Clear the screen for Turbo C
  57.  
  58.     // Display the array
  59.     printf("\nArray A: ");
  60.     for (i = 0; i < n; i++) {
  61.         printf("%d ", a[i]);
  62.     }
  63.  
  64.     // Prompt for the element to search
  65.     printf("\nEnter the element to be searched: ");
  66.     scanf("%d", &data);
  67.  
  68.     lower_bound = 0;
  69.     upper_bound = n - 1;
  70.  
  71.     // Binary search algorithm
  72.     while (lower_bound <= upper_bound) {
  73.         mid = (lower_bound + upper_bound) / 2;
  74.  
  75.         // Display the current bounds and middle position
  76.         printf("\n\nStart = %d, End = %d, Mid = %d", lower_bound + 1, upper_bound + 1, mid + 1);
  77.  
  78.         if (data == a[mid]) {
  79.             printf("\n\t%d = %d", data, a[mid]);
  80.             printf("\nElement is present at position: %d", mid + 1);
  81.             found = 1; // Mark as found
  82.             break;     // Exit the loop once the element is found
  83.         } else if (data > a[mid]) {
  84.             lower_bound = mid + 1; // Move to the right half
  85.             printf("\n\t%d > %d, so move to RHS", data, a[mid]);
  86.         } else {
  87.             upper_bound = mid - 1; // Move to the left half
  88.             printf("\n\t%d < %d, so move to LHS", data, a[mid]);
  89.         }
  90.     }
  91.  
  92.     // If the element is not found
  93.     if (!found) {
  94.         printf("\nThe element is not present in the array!");
  95.     }
  96.  
  97.     getch(); // Wait for a key press before exiting
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement