Advertisement
sildren12

heszke update

Apr 16th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int szukanie_liniowe_1(int TAB[], int n, int x);
  4. int *szukanie_liniowe_2(int TAB[], int n, int x, int *ilosc);
  5. void czytaj_tab(int *t, int n);
  6. void sortuj(int *t, int n);
  7. void drukuj_tab(int *t, int n);
  8. int szukanie_binarne(int TAB[], int n, int x);
  9.  
  10.  
  11. int main()
  12. {
  13.     int n, pos;
  14.     int *t, *indeksy, ilosc, i, x;
  15.     ilosc=0;
  16.     printf("Podaj n:");
  17.     scanf("%d", &n);
  18.  
  19.     t=NULL;
  20.     t=(int*)realloc(t, n*sizeof(int));
  21.     czytaj_tab(t, n);
  22.  
  23.     printf("Podaj x:");
  24.     scanf("%d", &x);
  25.  
  26.     pos = szukanie_liniowe_1(t, n, x);
  27.     if(pos>=0)
  28.     {
  29.         printf("\nZnaleziono na pozycji: %d", pos);
  30.         indeksy = szukanie_liniowe_2(t, n, x, &ilosc);
  31.         printf("\nZnalezione pozycje w szukanie_liniowe_2:");
  32.         for(i=0; i<ilosc; i++)
  33.             printf(" %d", *(indeksy+i));
  34.         sortuj(t, n);
  35.         drukuj_tab(t, n);
  36.         pos = szukanie_binarne(t, n, x);
  37.         printf("\nZnaleziono na pozycji: %d", pos);
  38.     }
  39.     else
  40.         printf("\nBrak szukanej wartosci");
  41.     getch();
  42. }
  43.  
  44. void czytaj_tab(int *t, int n)
  45. {
  46.     int i;
  47.     t=(int*)realloc(t, n*sizeof(int));
  48.     for(i=0; i<n; i++)
  49.     {
  50.         printf("Podaj %d element: ", i+1);
  51.         scanf("%d", (t+i));
  52.     }
  53. }
  54.  
  55. void sortuj(int *t, int n)
  56. {
  57.     int i, j, temp;
  58.     for (i = 0; i<n-1; i++)
  59.     {
  60.         for (j=0; j<n-1-i; j++)
  61.         {
  62.             if (t[j] > t[j+1])
  63.             {
  64.                 temp = t[j+1];
  65.                 t[j+1] = t[j];
  66.                 t[j] = temp;
  67.             }
  68.         }
  69.     }
  70. }
  71.  
  72. int szukanie_liniowe_1(int TAB[], int n, int x)
  73. {
  74.     int i;
  75.     for (i=0; i<n; i++)
  76.     {
  77.         if (*(TAB+i)==x)
  78.             return i;
  79.     }
  80.     return -1;
  81. }
  82.  
  83. int *szukanie_liniowe_2(int TAB[], int n, int x, int *ilosc)
  84. {
  85.     int i;
  86.     int *indeksy;
  87.     *ilosc = 0;
  88.     indeksy = NULL;
  89.     for (i=0; i<n; i++)
  90.     {
  91.         if (*(TAB+i)==x)
  92.         {
  93.             indeksy = (int*)realloc(indeksy, (*ilosc+1)*sizeof(int));
  94.             *(indeksy + *ilosc)=i;
  95.             (*ilosc)++;
  96.         }
  97.     }
  98.     return indeksy;
  99. }
  100.  
  101. void drukuj_tab(int *t, int n)
  102. {
  103.     int i;
  104.     for (i=0; i<n; i++)
  105.         printf("\n%d", *(t+i));
  106. }
  107.  
  108. int szukanie_binarne(int TAB[], int n, int x)
  109. {
  110.     int left=0, right=n-1, mid;
  111.     while(left<=right)
  112.     {
  113.         mid=(left+right)/2;
  114.         if (*(TAB+mid)==x)
  115.             return mid;
  116.         else
  117.         {
  118.             if (TAB[mid]<x)
  119.                 left=mid;
  120.             else
  121.                 right=mid;
  122.         }
  123.     }
  124.     return -1;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement