Advertisement
Guest User

Untitled

a guest
Nov 24th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <ctime>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <vector>
  7. #include <fstream>
  8.  
  9. using namespace std;
  10.  
  11. int fp(int x, int y)
  12. {
  13.     return (x<=y);
  14. }
  15.  
  16. void sort_shell(int *tablica, int ilosc, fstream &plik1)
  17. {
  18.     int licznik=0;
  19.     int h;
  20.     int i;
  21.     int temp;
  22.     int c=1;
  23.     double czas;
  24.     const clock_t begin_time = clock();
  25.     for (h=1; h<=ilosc/9; h=ilosc/pow(2,h));
  26.     c++;
  27.  
  28.     for(; h>0; h /= 3)
  29.     {
  30.         for (i=h; i<ilosc; i++)
  31.         {
  32.             int j;
  33.             temp = tablica[i];
  34.             for (j = i-h ; j>=0; j -=h)
  35.             {
  36.                 if (temp <=tablica[j])
  37.                 {
  38.                     tablica[j+h] = tablica[j];
  39.                     licznik++;
  40.                 }
  41.                 else
  42.                     break;
  43.             }
  44.             tablica[j+h] = temp;
  45.         }
  46.     }
  47.  
  48.  
  49.     /*cout << "\nPo sortowaniu Shellem." << endl;
  50.     for(i=0; i<ilosc; i++)
  51.     {
  52.         cout << tablica[i] << "\t";
  53.     }*/
  54.  
  55.     cout.precision(30);
  56.     czas = double( clock () - begin_time ) /  CLOCKS_PER_SEC;
  57.     plik1 << fixed << czas << endl; //jesli liczbe zamian to licznik
  58.  
  59.     //cout << "\nIlosc operacji :" << licznik << "\n";
  60. }
  61.  
  62. void sort_hibbard(int x[], int n, fstream &plik2)
  63. {
  64.     int i, j,k, increment, temp,licznik=0;
  65.     long swp=0, comp=0;
  66.     int val;
  67.     double czas;
  68.     const clock_t begin_time = clock();
  69.     val=log(n+1)/log(2);
  70.     increment =pow(2,val)-1;
  71.     while (increment > 0)
  72.     {
  73.         for (i=0; i<increment; i++)
  74.         {
  75.             for(j=0; j<n; j+=increment)
  76.             {
  77.                 temp=x[j];
  78.                 for(k=j-increment; k>=0&&temp<x[k]; k-=increment)
  79.                 {
  80.                     comp++;
  81.                     swp++;
  82.                     x[k+increment]=x[k];
  83.                     licznik++;
  84.                 }
  85.                 x[k+increment]=temp;
  86.                 swp++;
  87.                 licznik++;
  88.             }
  89.         }
  90.         comp++;
  91.         val--;
  92.         if(increment!=1)
  93.             increment=pow(2,val)-1;
  94.         else
  95.             increment = 0;
  96.     }
  97.     /*cout << "\nPo sortowaniu Hibbardem." << endl;
  98.     for(i=0; i<n; i++)
  99.     {
  100.         cout << x[i] << "\t";
  101.     }*/
  102.     cout.precision(30);
  103.     czas = double( clock () - begin_time ) /  CLOCKS_PER_SEC;
  104.     plik2 << fixed << czas << endl;
  105.  
  106.  
  107.     //cout << "\nIlosc operacji :" << licznik << "\n";
  108. }
  109.  
  110.  
  111. void sort_sedgewick(int *tablica, int ilosc, fstream &plik3)
  112. {
  113.     int h = 0, i, g, t, j;
  114.     int c = 1;
  115.     int licznik = 0;
  116.     int temp;
  117.     double czas;
  118.     const clock_t begin_time = clock();
  119.     vector <int> tmp;
  120.     tmp.push_back(h);
  121.     do
  122.     {
  123.         h = (pow(4,c) + (3 * pow(2,c-1)) + 1); // funkcja z wikipedi O(N^4/3)
  124.         tmp.push_back(h);
  125.         if(h < ilosc) c++;
  126.     }while(h < ilosc);
  127.     for(g=ilosc/c;g>0;g/=c)
  128.     {
  129.         for(i=ilosc-g-1;i>=0;i--)
  130.         {
  131.             t = tablica[i];
  132.             for(j=i+g;(j<ilosc)&&!fp(t,tablica[j]);j+=g)
  133.             {
  134.                 tablica[j-g] = tablica[j];
  135.                 licznik++;
  136.             }
  137.             tablica[j-g] = t;
  138.             licznik++;
  139.         }
  140.     }
  141.     /*cout << "\nPo sortowaniu SEDGEWICKiem." << endl;
  142.     for(i=0;i<ilosc;i++)
  143.     {
  144.         cout << tablica[i] << "\t";
  145.     }*/
  146.     cout.precision(30);
  147.     czas = double( clock () - begin_time ) /  CLOCKS_PER_SEC;
  148.     plik3 << fixed << czas << endl;
  149.  
  150.  
  151.     //cout << "\nIlosc operacji :" << licznik << "\n";
  152. }
  153.  
  154. /*void sort_pratt(int *pratt, int n)
  155. {
  156.     int i, last2ind = 0, last3ind = 0;
  157.     printf("Po sortowaniu: \n");
  158.     pratt[0] = 1;
  159.     for (i=1; i < n; ++i)
  160.     {
  161.         if (pratt[last2ind]*2 < pratt[last3ind]*3)
  162.         {
  163.             pratt[i] = pratt[last2ind]*2;
  164.             last2ind++;
  165.         }
  166.         else if (pratt[last2ind]*2 > pratt[last3ind]*3)
  167.         {
  168.             pratt[i] = pratt[last3ind]*3;
  169.             last3ind++;
  170.         }
  171.         else
  172.         {
  173.  
  174.             pratt[i] = pratt[last2ind]*2;
  175.             last2ind++;
  176.             last3ind++;
  177.         }
  178.  
  179.         printf("%d \t",pratt[i]);
  180.     }
  181.     printf("\n");
  182.  
  183. }
  184. */
  185.  
  186. int main()
  187. {
  188.     int i, n;
  189.     fstream plik1;
  190.     fstream plik2;
  191.     fstream plik3;
  192.     fstream plik4;
  193.     //cout << "Podaj rozmiar tablicy (W tablicy znajda sie liczby od 1 do 100): ";
  194.     //cin >> n;
  195.  
  196.     plik1.open( "shell.txt", std::ios::in | std::ios::out );
  197.     plik2.open( "hibbard.txt", std::ios::in | std::ios::out );
  198.     plik3.open( "sedgewick.txt", std::ios::in | std::ios::out );
  199.     plik4.open( "wielkoscn.txt", std::ios::in | std::ios::out );
  200.  
  201.     for(n=1000;n<=400000;n+=1000)
  202.     {
  203.     cout << n << "\n";
  204.     plik4 << n << "\n";
  205.     int zbior[n];
  206.     int prawy = 200;
  207.     srand(time(NULL));
  208.     //cout << "Przed sortowaniem." << endl;
  209.     for (int i = 0; i < n; i++)
  210.     {
  211.         if (i%2 == 0)
  212.         {
  213.             zbior[i] = rand()%prawy;
  214.             //cout << zbior[i] << "\t";
  215.         }
  216.         else
  217.         {
  218.             zbior[i] = rand()%prawy+200;
  219.             //cout << zbior[i] << "\t";
  220.         }
  221.     }
  222.     cout << endl;
  223.  
  224.  
  225.  
  226.     sort_shell(zbior, n,plik1);
  227.     sort_hibbard(zbior, n,plik2);
  228.     sort_sedgewick(zbior, n,plik3);
  229.     //sort_pratt(zbior,n);
  230.     }
  231.  
  232.     plik1.close();
  233.     plik2.close();
  234.     plik3.close();
  235.     plik4.close();
  236.     return 0;
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement