Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <locale.h>
  3. #include <stdlib.h>
  4. #include <iostream>
  5. #include <ctime>
  6.  
  7. const int size = 100000;
  8.  
  9. void sort1(int* &A);
  10. void sort2(int* &A);
  11. void sort3(int* &A);
  12. void fill(int* &A);
  13.  
  14. int main()
  15. {
  16.     char method = '1';
  17.     std::clock_t start;
  18.  
  19.    
  20.  
  21.     int* A = new int[size];
  22.     int* B = new int[size];
  23.     int* C = new int[size];
  24.  
  25.     fill(A);
  26.     fill(B);
  27.     fill(C);
  28.  
  29.     while (method != '0') {
  30.         printf("Write the nubmer of method (0 - exit):\n");
  31.         std::cin >> method;
  32.  
  33.         switch (method) {
  34.         case '1':
  35.             start = std::clock();
  36.             sort1(A);
  37.             printf("Runtime is %.3f seconds\n", ((std::clock() - start) / (double)(CLOCKS_PER_SEC)));
  38.  
  39.             start = std::clock();
  40.             sort1(B);
  41.             printf("Runtime is %.3f seconds\n", ((std::clock() - start) / (double)(CLOCKS_PER_SEC)));
  42.  
  43.             start = std::clock();
  44.             sort1(C);
  45.             printf("Runtime is %.3f seconds\n", ((std::clock() - start) / (double)(CLOCKS_PER_SEC)));
  46.  
  47.             break;
  48.         case '2':
  49.             start = std::clock();
  50.             sort2(A);
  51.             printf("Runtime is %.3f seconds\n", ((std::clock() - start) / (double)(CLOCKS_PER_SEC)));
  52.  
  53.             start = std::clock();
  54.             sort2(B);
  55.             printf("Runtime is %.3f seconds\n", ((std::clock() - start) / (double)(CLOCKS_PER_SEC)));
  56.  
  57.             start = std::clock();
  58.             sort2(C);
  59.             printf("Runtime is %.3f seconds\n", ((std::clock() - start) / (double)(CLOCKS_PER_SEC)));
  60.             break;
  61.  
  62.         case '3':
  63.             start = std::clock();
  64.             sort3(A);
  65.             printf("Runtime is %.3f seconds\n", ((std::clock() - start) / (double)(CLOCKS_PER_SEC)));
  66.  
  67.             start = std::clock();
  68.             sort3(B);
  69.             printf("Runtime is %.3f seconds\n", ((std::clock() - start) / (double)(CLOCKS_PER_SEC)));
  70.  
  71.             start = std::clock();
  72.             sort3(C);
  73.             printf("Runtime is %.3f seconds\n", ((std::clock() - start) / (double)(CLOCKS_PER_SEC)));
  74.             break;
  75.  
  76.         }
  77.     }
  78.  
  79.    
  80.  
  81.     system("PAUSE");
  82.     return 0;
  83. }
  84.  
  85. void fill(int* &A) {
  86.     for (int i = 0; i < size; i++) {
  87.         A[i] = rand() % 10;
  88.     }
  89. }
  90.  
  91. void sort1(int* &A) //вставками
  92. {
  93.     for (int i = 0, j, x; i < size; i++)
  94.     {
  95.         j = i;
  96.         x = A[i];
  97.         while (j>0 && x < A[j - 1])
  98.         {
  99.             A[j] = A[j - 1];
  100.             j--;
  101.         }
  102.         A[j] = x;
  103.     };
  104.  
  105. }
  106.  
  107.  
  108.  
  109. void sort2(int* &A) //Пузырек
  110. {
  111.    
  112.     for (int i = 0, x; i < size; i++)
  113.         for (int j = size; j >=  i; j--)
  114.         if (A[j - 1]>A[j])
  115.         {
  116.             x = A[j - 1];
  117.             A[j - 1] = A[j];
  118.             A[j] = x;
  119.         }
  120. }
  121.  
  122.  
  123. void sort3(int* &A) //Выборка
  124. {
  125.     for (int i = 0, k, x; i < size; i++)
  126.     {
  127.         k = i;
  128.         x = A[i];
  129.         for (int j = i + 1; j <= size; j++)
  130.         if (A[j]>x)
  131.         {
  132.             k = j;
  133.             x = A[k];
  134.         }
  135.         A[k] = A[i];
  136.         A[i] = x;
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement