Advertisement
MargaritaOwl

TarasSortTime

May 14th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <cstdlib>
  4. #include <ctime>
  5. using namespace std;
  6. const long N=100000;
  7. double A[N], B[N], C[N], D[N], E[N];
  8.  
  9. int BubleSort(long size,double arr[])
  10. {
  11.     double temp;
  12.     cout<<"Производится сортировка пузырьком"<<endl;
  13.     int t1=clock();
  14.     for(long i = 0; i < size - 1; ++i)
  15.     {            
  16.         for(long j = 0; j < size - 1; ++j)
  17.         {    
  18.             if (arr[j + 1] < arr[j])
  19.             {
  20.                 temp = arr[j + 1];
  21.                 arr[j + 1]     = arr[j];
  22.                 arr[j] = temp;
  23.             }
  24.         }
  25.     }
  26.     int t2=clock();
  27.     return t2-t1;
  28. }
  29. int selectSort(long size, double arr[])
  30. {
  31.     double tmp;
  32.     cout<<"Производится сортировка выборкой"<<endl;
  33.     int t1=clock();
  34.     for(long i = 0; i < size; ++i) // i - номер текущего шага
  35.     {
  36.         int pos = i;
  37.         tmp = arr[i];
  38.         for(long j = i + 1; j < size; ++j) // цикл выбора наименьшего элемента
  39.         {
  40.             if (arr[j] < tmp)
  41.             {
  42.                 pos = j;
  43.                 tmp = arr[j];
  44.             }
  45.         }
  46.         arr[pos] = arr[i];
  47.         arr[i] = tmp; // меняем местами наименьший с a[i]
  48.     }
  49.     int t2=clock();
  50.     return t2-t1;
  51. }
  52. int insertSort(long size, double arr[])
  53. {
  54.     double tmp;
  55.     cout<<"Производится сортировка вставкой"<<endl;
  56.     int t1=clock();
  57.     for (long i = 1, j; i < size; ++i) // цикл проходов, i - номер прохода
  58.     {
  59.         tmp = arr[i];
  60.         for (j = i - 1; j >= 0 && arr[j] > tmp; --j)
  61.             arr[j + 1] = arr[j];    
  62.         arr[j + 1] = tmp;    
  63.     }
  64.     int t2=clock();
  65.     return t2-t1;
  66. }
  67. void quickSortR(long N, double* arr) {
  68. // На входе - массив a[], a[N] - его последний элемент.
  69.  
  70.     long i = 0, j = N;      // поставить указатели на исходные места
  71.     double temp, p;
  72.     p = arr[ N>>1 ];      // центральный элемент
  73.     // процедура разделения
  74.     do {
  75.         while ( arr[i] < p ) i++;
  76.         while ( arr[j] > p ) j--;
  77.  
  78.         if (i <= j) {
  79.             temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
  80.             i++; j--;
  81.         }
  82.     } while ( i<=j );
  83.  
  84.     // рекурсивные вызовы, если есть, что сортировать
  85.     if ( j > 0 ) quickSortR(j, arr);
  86.     if ( N > i ) quickSortR(N-i,arr+i);
  87. }
  88. int increment(long inc[], long size)
  89. {
  90.     int p1, p2, p3, s;
  91.     p1 = p2 = p3 = 1;
  92.     s = -1;
  93.     do
  94.     {
  95.         if (++s % 2)
  96.         {
  97.             inc[s] = 8*p1 - 6*p2 + 1;
  98.         }
  99.         else
  100.         {
  101.             inc[s] = 9*p1 - 9*p3 + 1;
  102.             p2 *= 2;
  103.             p3 *= 2;
  104.         }
  105.     p1 *= 2;
  106.     }
  107.     while(3*inc[s] < size);  
  108.  
  109.     return s > 0 ? --s : 0;
  110. }
  111. int shellSort(long size, double arr[])
  112. {
  113.     long inc, i, j, seq[40];
  114.     int s;
  115.     cout<<"Производится сортировка Шелл"<<endl;
  116.     int t1=clock();
  117.     s = increment(seq, size); // вычисление последовательности приращений
  118.     while (s >= 0)  // сортировка вставками с инкрементами inc[]
  119.     {
  120.          inc = seq[s--];
  121.          for (i = inc; i < size; ++i)
  122.          {
  123.              double temp = arr[i];
  124.              for (j = i-inc; (j >= 0) && (arr[j] > temp); j -= inc)
  125.                 arr[j + inc] = arr[j];
  126.              arr[j] = temp;
  127.          }
  128.     }
  129.     int t2=clock();
  130.     return t2-t1;
  131. }
  132. int main()
  133. {
  134.     system("CHCP 1251>NUL");
  135.     int Time[5];
  136.     srand(time(NULL));
  137.     for(int i=0; i<N;i++)
  138.     {
  139.         A[i] = (double)rand() / (double)RAND_MAX *100 + -50;
  140.         B[i]=A[i];
  141.         C[i]=A[i];
  142.         D[i]=A[i];
  143.         E[i]=A[i];
  144.     }
  145.     cout<<"Размер масива = "<<sizeof(A)/sizeof(double)<<" байт"<<endl;
  146.     Time[0]=BubleSort(N-20000,A);
  147.     Time[0]=BubleSort(N,A);
  148.     cout<<"Затраченое время на сортировку массива= "<<(double)Time[0]/CLOCKS_PER_SEC<<endl;
  149.     Time[1]=selectSort(N-20000,B);
  150.     Time[1]=selectSort(N,B);
  151.     cout<<"Затраченое время на сортировку массива= "<<(double)Time[1]/CLOCKS_PER_SEC<<endl;
  152.     Time[2]=insertSort(N-20000,C);
  153.     Time[2]=insertSort(N,C);
  154.     cout<<"Затраченое время на сортировку массива= "<<(double)Time[2]/CLOCKS_PER_SEC<<endl;
  155.     cout<<"Производится быстрая сортировка"<<endl;
  156.     int t1=clock();
  157.     quickSortR(N-20000,D);
  158.     quickSortR(N,D);
  159.     int t2=clock();
  160.     Time[3]=t2-t1;
  161.     cout<<"Затраченое время на сортировку массива= "<<(double)Time[3]/CLOCKS_PER_SEC<<endl;
  162.     Time[4]=shellSort(N-20000,E);
  163.     Time[4]=shellSort(N,E);
  164.     cout<<"Затраченое время на сортировку массива= "<<(double)Time[4]/CLOCKS_PER_SEC<<endl;
  165.     system ("pause");
  166.     return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement