Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h.>
- #include<stdlib.h>
- #include<string.h>
- #define _CRT_SECURE_NO_WARNINGS
- int cautare_liniara(int *a, int n, int x)
- {
- int i = 0;
- for (i = 0; i < n; i++)
- {
- if (a[i] == x)
- break;
- }
- return i;
- }
- int cautare_fanion(int *a, int n, int x)
- {
- int i = 0;
- a[n] = x;
- while (a[i] != x)
- {
- i = i + 1;
- }
- return i;
- }
- int cautare_binara(int a[30], int n, int x)
- {
- int S, D, m;
- S = 0;
- D = n - 1;
- do
- {
- m = (S + D) / 2;
- if (x > a[m])
- S = m + 1;
- else
- D = m - 1;
- } while (a[m] != x && S <= D);
- return m;
- }
- int cautare_binara_performanta(int a[30], int n, int x)
- {
- int S, D, m;
- S = 0;
- D = n - 1;
- do
- {
- m = (S + D) / 2;
- if (x > a[m])
- S = m + 1;
- else
- D = m;
- } while (S < D);
- return D;
- }
- int cautare_interpolare(int a[30], int n, int x)
- {
- int S, D, m;
- S = 0;
- D = n - 1;
- if (x <= a[D] && x >= a[S])
- do
- {
- m = S + (x - a[S]) * (D - S) / (a[D] - a[S]);
- if (x > a[m])
- D = m - 1;
- } while ((a[m] != x) && (S < D) && (a[S] != a[D]) && (x > a[S]) && (x < a[D]));
- return m;
- }
- int main()
- {
- int n, x, a[30], i, opt;
- FILE* f;
- fopen_s(&f, "sir.txt", "r");
- fscanf_s(f, "%d",&n);
- fscanf_s(f, "%d",&x);
- for (i = 0; i < n; i++)
- {
- fscanf_s(f, "%d", &a[i]);
- }
- fclose(f);
- do
- {
- printf("1.Cautare liniara\n");
- printf("2.Cautare fanion\n");
- printf("3.Cautare binara\n");
- printf("4.Cautare binara performanta\n");
- printf("5.Cautare prin interpolare\n");
- printf("0.Iesire\n");
- printf("Alegeti o optiune\n");
- scanf_s("%d", &opt);
- switch (opt)
- {
- case 1: cautare_liniara(a, n, x);
- break;
- case 2: cautare_fanion(a, n, x);
- break;
- case 3: cautare_binara(a, n, x);
- break;
- case 4: cautare_binara_performanta(a, n, x);
- break;
- case 5: cautare_interpolare(a, n, x);
- break;
- case 0:
- break;
- }
- } while (opt != 0);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment