Patey

Untitled

Feb 25th, 2021
757
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<string.h>
  4.  
  5. void cautare_liniara(int a[20], int n,int aux)
  6. {
  7.     int i,k=0;
  8.     for (i = 0; i < n; i++)
  9.     {
  10.         if (a[i] == aux)
  11.         {
  12.             k = 1;
  13.             printf("ELementul se gaseste pe pozitia %d\n", i);
  14.         }
  15.     }
  16.     if (k == 0)
  17.         printf("Element inexistent in multime\n");
  18. }
  19.  
  20. void cautare_binara(int s, int d,int a[20],int aux)
  21. {
  22.     int m;
  23.     if (s > d)
  24.         printf("Element inexistent in multime\n");
  25.     else
  26.     {
  27.         m = (s + d) / 2;
  28.         if (aux == a[m])
  29.             printf("Element gasit\n");
  30.         else if (aux > a[m])
  31.             cautare_binara(m + 1, d, a,aux);
  32.         else
  33.             cautare_binara(s, m - 1, a,aux);
  34.     }
  35. }
  36.  
  37. int main()
  38. {
  39.     enum{iesire, prima, adoua}opt;
  40.     int a[20],aux, n,i;
  41.     printf("Dati n=");
  42.     scanf("%d", &n);
  43.     for (i = 0; i < n; i++)
  44.     {
  45.         printf("a[%d]=", i);
  46.         scanf("%d", &a[i]);
  47.     }
  48.     do {
  49.         printf("\n0.Iesire\n");
  50.         printf("1.Liniara\n");
  51.         printf("2.Binara\n");
  52.         printf("Dati elementul cautat ");
  53.         scanf("%d", &aux);
  54.         printf("opt= ");
  55.         scanf("%d", &opt);
  56.         switch (opt)
  57.         {
  58.         case iesire:exit(0);
  59.             break;
  60.         case prima:cautare_liniara(a, n,aux);
  61.             break;
  62.         case adoua:cautare_binara(0, n - 1, a,aux);
  63.             break;
  64.         default:
  65.             break;
  66.         }
  67.     } while (1);
  68.  
  69. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×