Advertisement
majczel23000

[AiSD] 1. Wyszukiwanie

Nov 29th, 2017
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. const int N = 20;
  7. int tab[N]={};
  8.  
  9. void wypelnij()
  10. {
  11.     srand( time( NULL ) );
  12.     for(int i = 0; i< N; i++)
  13.         tab[i] = rand()%N;
  14. }
  15.  
  16. void wypisz()
  17. {
  18.     for(int i = 0; i< N; i++)
  19.         cout<<tab[i]<<" ";
  20.     cout<<endl;
  21. }
  22.  
  23. int wyszukiwanie_liniowe(int s)
  24. {
  25.     cout<<"[LINIOWE] ";
  26.     int i = 0;
  27.     while(i < N && tab[i] != s)
  28.         i += 1;
  29.     if(i >= N)
  30.     {
  31.         cout<<"Nie ma elementu: "<<s<<endl;
  32.         return -1;
  33.     }
  34.  
  35.     else
  36.     {
  37.         cout<<"Znaleziono element: "<<s<<endl;
  38.         return i;
  39.     }
  40.  
  41. }
  42.  
  43. int wyszukiwanie_binarne(int s)
  44. {
  45.     cout<<"[BINARNE] ";
  46.     //tablica musi byc posortowana - zalozenie algorytmu
  47.     for(int i= 0; i< N-1; i++)
  48.         for(int j= 0; j< N-1; j++)
  49.             if(tab[j]>tab[j+1])
  50.                 swap(tab[j], tab[j+1]);
  51.  
  52.     int lewy = 0;
  53.     int prawy = N-1;
  54.     bool znaleziono = false;
  55.     while(lewy <= prawy && !znaleziono)
  56.     {
  57.         int srodek = (lewy+prawy)/2;
  58.         if(tab[srodek] == s)
  59.         {
  60.             znaleziono = true;
  61.             cout<<"Znaleziono element: "<<s<<endl;
  62.             return srodek;
  63.         }
  64.         else
  65.             if(s < tab[srodek])
  66.                 prawy = srodek-1;
  67.             else
  68.                 lewy = srodek+1;
  69.     }
  70.     cout<<"Nie ma elementu: "<<s<<endl;
  71.     return -1;
  72. }
  73.  
  74. int wyszukiwanie_interpolacyjne(int s)
  75. {
  76.     cout<<"[INTERPOLACYJNE] ";
  77.     //tablica musi byc posortowana - zalozenie algorytmu
  78.     for(int i= 0; i< N-1; i++)
  79.         for(int j= 0; j< N-1; j++)
  80.             if(tab[j]>tab[j+1])
  81.                 swap(tab[j], tab[j+1]);
  82.  
  83.     int lewy = 0;
  84.     int prawy = N-1;
  85.     bool znaleziono = false;
  86.     while(lewy <= prawy && !znaleziono)
  87.     {
  88.         int srodek = lewy + (s - tab[lewy])/(tab[prawy] - tab[lewy])*(prawy - lewy);
  89.         if(tab[srodek] == s)
  90.         {
  91.             znaleziono = true;
  92.             cout<<"Znaleziono element: "<<s<<endl;
  93.             return srodek;
  94.         }
  95.         else
  96.             if(s < tab[srodek])
  97.                 prawy = srodek-1;
  98.             else
  99.                 lewy = srodek+1;
  100.     }
  101.     cout<<"Nie ma elementu: "<<s<<endl;
  102.     return -1;
  103. }
  104.  
  105. int main(){
  106.  
  107.     wypelnij();
  108.     wypisz();
  109.     wyszukiwanie_liniowe(5);
  110.     wyszukiwanie_interpolacyjne(5);
  111.     wyszukiwanie_binarne(5);
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement