Advertisement
Domy131097

[LV1] Algoritmi i strukture podataka

Mar 13th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. float random_float(float min, float max)
  6. {
  7.     if (max == min) return min;
  8.     else if (min < max) return (max - min) * ((float)rand() / RAND_MAX) + min;
  9. }
  10.  
  11. void gen_arr(float V[], int n, float dg, float gg) {
  12.     for (int i = 0; i < n; i++) {
  13.         V[i] = random_float(dg, gg);
  14.     }
  15. }
  16.  
  17. int sekv_pret(float V[], int n, float x) {
  18.     time_t t = clock();
  19.     for (int i = 0; i < n; i++) {
  20.         if (V[i] == x) {
  21.             t = clock() - t;
  22.             printf("Vrijeme trajanja sekvencijalnog pretrazivanja iznosi %dms.\n", t);
  23.             return i;
  24.         }
  25.     }
  26.     t = clock() - t;
  27.     printf("Vrijeme trajanja sekvencijalnog pretrazivanja iznosi %dms.\n", t);
  28.     return -1;
  29. }
  30.  
  31. void Zamjena(float *x, float *y) {
  32.     float temp = 0;
  33.     temp = *x;
  34.     *x = *y;
  35.     *y = temp;
  36. }
  37.  
  38. void Ispis(float V[], int n) {
  39.     printf("V[");
  40.     for (int i = 0; i < n; i++) {
  41.         if (i < n - 1) printf("%.2f, ", V[i]);
  42.         else printf("%.2f]\n", V[i]);
  43.     }
  44. }
  45. void SelectionSort(float V[], int n) {
  46.     time_t t = clock();
  47.     for (int i = 0; i < n - 1; i++) {
  48.         for (int j = i + 1; j < n; j++) {
  49.             if (V[j] < V[i]) Zamjena(&V[j], &V[i]);
  50.         }
  51.     }
  52.     t = clock() - t;
  53.     printf("Vrijeme trajanja selection sorta iznosi %dms.\n", t);
  54. }
  55.  
  56. int BinarnoPretrazivanje(float V[], int n, float x) {
  57.     int dg = 0;
  58.     int gg = n - 1;
  59.     int s = -1;
  60.     time_t t = clock();
  61.     while (dg <= gg) {
  62.         s = (dg + gg) / 2;
  63.         if (V[s] == x) {
  64.             printf("Trazeni broj se nalazi na %d. mjestu u nizu.\n", s);
  65.             t = clock() - t;
  66.             printf("Vrijeme trajanja binarnog pretrazivanja iznosi %dms.\n", t);
  67.             return s;
  68.         }
  69.         else if (V[s] > x) {
  70.             gg = s - 1;
  71.         }
  72.         else {
  73.             dg = s + 1;
  74.         }
  75.     }
  76.     t = clock() - t;
  77.     printf("Vrijeme trajanja binarnog pretrazivanja iznosi %dms.\n", t);
  78.     printf("Trazeni broj se ne nalazi u nizu.\n");
  79.     return -1;
  80. }
  81.  
  82.  
  83. int main()
  84. {
  85.     int n;
  86.     float *V = NULL;
  87.     printf("Unesite broj elemenata:\n");
  88.     scanf("%d", &n);
  89.     V = (float *)malloc(n * sizeof(float));
  90.     gen_arr(V, n, 0, 100);
  91.     //Ispis(V, n);
  92.     printf("\nSEKVENCIJALNO PRETRAZIVANJE:\n");
  93.     sekv_pret(V, n, 300);
  94.     printf("\nSORTIRANJE:\n");
  95.     SelectionSort(V, n);
  96.     //Ispis(V, n);
  97.     printf("\nBINARNO PRETRAZIVANJE\n");
  98.     BinarnoPretrazivanje(V, n, 0.13);
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement