Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <iomanip>
  4. #include <algorithm>
  5. #include <nmmintrin.h>
  6. #include <set>
  7. #include <map>
  8. #include <unordered_set>
  9. #include <unordered_map>
  10. #include <string>
  11. #include <queue>
  12. #include <cmath>
  13. #include <climits>
  14. #include <bitset>
  15. #include <random>
  16. #include <ctime>
  17. #include <chrono>
  18. #include <cstdio>
  19. #include <cstring>
  20. #include <cassert>
  21. #include <sstream>
  22. #include <winbgim.h>
  23. using namespace std;
  24. const int WID = 800, HIGH = 600;
  25. void FillInc(int n,int *a)
  26. {
  27.     for (int i = 0; i < n;++i)
  28.     {
  29.         a[i] = i;
  30.     }
  31. }
  32. void FillDec(int n,int *a)
  33. {
  34.     for (int i = n; i > 0;i-- )
  35.     {
  36.         a[n-i] = i;
  37.     }
  38. }
  39. void drawLine(int moveToX, int moveToY, int drawX, int drawY, int color, int textX, int textY, char *name)
  40. {
  41.     moveto(moveToX, moveToY);
  42.     setcolor(color);
  43.     lineto(drawX, drawY);
  44.     outtextxy(textX, textY, name);
  45. }
  46. void FillRand(int n,int *a)
  47. {
  48.     for (int i = 0;i < n;++i)
  49.     {
  50.         a[i] = rand() % n;
  51.    
  52.     }
  53.  }
  54. void CheckSum(int n, int *a)
  55. {
  56.     int x = 0;
  57.     for(int i = 0; i < n;++i)
  58.     {
  59.         x+=a[i];
  60.     }
  61.     cout << endl << x;
  62. }
  63. void swap(int *xp, int *yp)  
  64. {  
  65.     int temp = *xp;  
  66.     *xp = *yp;  
  67.     *yp = temp;  
  68. }  
  69. void ShakerSort(int m, int *a)
  70. {
  71.     int i, j, k,M = 0,C = 0;
  72.     for(i = 0; i < m;)
  73.     {
  74.       for(j = i+1; j < m; j++)
  75.       {
  76.           C++;
  77.          if(a[j] < a[j-1])
  78.          {
  79.             swap(&a[j], &a[j-1]);
  80.             M+=3;
  81.          }
  82.       }
  83.     m--;
  84.     for(k = m-1; k > i; k--)
  85.     {
  86.         ++C;
  87.         if(a[k] < a[k-1])
  88.         {
  89.             swap(&a[k], &a[k-1]);
  90.             M+=3;
  91.         }
  92.     }
  93.     i++;
  94.    }
  95.     cout << "M(fact) = " << M << " C(fact) = " << C << endl;
  96. }
  97. int MCShakerSort(int m, int *a)
  98. {
  99.     int i,sum = 0, j, k,M = 0,C = 0;
  100.     for(i = 0; i < m;)
  101.     {
  102.       for(j = i+1; j < m; j++)
  103.       {
  104.           C++;
  105.          if(a[j] < a[j-1])
  106.          {
  107.             swap(&a[j], &a[j-1]);
  108.             M+=3;
  109.          }
  110.       }
  111.     m--;
  112.     for(k = m-1; k > i; k--)
  113.     {
  114.         ++C;
  115.         if(a[k] < a[k-1])
  116.         {
  117.             swap(&a[k], &a[k-1]);
  118.             M+=3;
  119.         }
  120.     }
  121.     i++;
  122.    }
  123.     sum = M + C;
  124.     return sum;
  125. }
  126. void SSort(int n, int *a)  
  127. {  
  128.     int i, j, mina, M=0, C=0;  
  129.     for (i = 0; i < n-1; i++)  
  130.     {  
  131.         mina = i;  
  132.         for (j = i+1; j < n; j++)
  133.         {
  134.             ++C;
  135.             if (a[j] < a[mina])  
  136.             mina = j;
  137.         }
  138.     if(mina!=i)
  139.      {
  140.        M+=3;
  141.        swap(a[mina], a[i]);
  142.      }
  143.    }
  144.     cout<<endl<<"(fact)C = "<< C<<" (fact) M = "<< M;
  145.  }
  146.  void CheckMC(int n)
  147. {
  148.     int m,c;
  149.     m = pow(n,2);
  150.     c = (pow(n,2)-n)/2;
  151.     cout << "M = " << m << " C = " << c << endl;
  152. }
  153.  void ShakerCheckMC(int n)
  154. {
  155.     int m,c;
  156.     c = (pow(n,2)-n)/2;
  157.     m = 3*(pow(n,2)-n)/2;
  158.     cout << "M = " << m << " C = " << c << endl;
  159. }
  160. void RunNumber(int n,int *a)
  161. {
  162.     int x = 1;
  163.     int null = a[0];
  164.     for (int i = 1; i < n;i++)
  165.     {
  166.         if (a[i] < null)
  167.         {
  168.             x++;
  169.         }
  170.         null = a[i];
  171.     }
  172.     cout << endl << x;
  173. }
  174. void PrintMas(int n,int *a)
  175. {
  176.   for (int i = 0; i < n;++i)
  177.   {
  178.     cout << a[i]<<" ";
  179.   }
  180. }
  181. void BubbleSort(int n, int *a)
  182. {  
  183.   int i, j;
  184.   int c = 0, m = 0;  
  185.   for (i = 0; i < n-1; i++)      
  186.     for (j = 0; j < n-i-1; j++)
  187.     {
  188.      ++c;
  189.      if (a[j] > a[j+1])
  190.      {
  191.       m+=3;
  192.       swap(&a[j], &a[j+1]);
  193.     }
  194.   }
  195.   cout << endl << "M(fact) = " << m << " " <<  "C(fact) = " << c << " ";
  196. }
  197. int MCBubbleSort(int n, int *a)
  198. {  
  199.   int i, j,sum;
  200.   int c = 0, m = 0;  
  201.   for (i = 0; i < n-1; i++)      
  202.     for (j = 0; j < n-i-1; j++)
  203.     {
  204.      ++c;
  205.      if (a[j] > a[j+1])
  206.      {
  207.       m+=3;
  208.       swap(&a[j], &a[j+1]);
  209.     }
  210.  
  211.   }
  212.   sum = c + m;
  213.   return sum;
  214.  
  215. }
  216. void PrintTabl()
  217. {
  218.   int m,c,n,sum = 0;
  219.   int *a = new int[n];
  220.   n = 100;
  221.   int RandSum,IncSum,DecSum;
  222.   int SRandSum, SIncSum,SDecSum;
  223.   cout << endl << "| n |        MFact + CFact Bubble    |     MFact + CFact Shaker     |" << endl;
  224.   cout << "|   |  ybiv    |   Rand   |  vozr    |  ybiv  |   Rand   |    vozr  |" << endl;
  225.   for (int i = 1;i <= 5;++i)
  226.   {
  227.     n = i * 100;
  228.     FillRand(n,a);
  229.     RandSum = MCBubbleSort(n,a);
  230.     FillDec(n,a);
  231.     DecSum = MCBubbleSort(n,a);
  232.     FillInc(n,a);
  233.     IncSum = MCBubbleSort(n,a);
  234.     FillRand(n,a);
  235.     SRandSum = MCShakerSort(n,a);
  236.     FillDec(n,a);
  237.     SDecSum = MCShakerSort(n,a);
  238.     FillInc(n,a);
  239.     SIncSum = MCShakerSort(n,a);
  240.     cout << "|" << n << "| " << " " << setw(8)<< DecSum << "| " << setw(8) <<
  241.     RandSum << " | " << setw(8) << IncSum << " |" << setw(8)<< SDecSum << "| " << setw(8) <<
  242.     SRandSum << " | " << setw(8) << SIncSum << " |" << endl ;
  243.   }
  244. }
  245. void GraphText(int x1, int y1, int x2, int y2, int color, int textX, int textY, char *name)
  246. {
  247.     setcolor(color);
  248.     bar(x1,y1,x2,y2);
  249.     outtextxy(textX, textY, name);
  250. }
  251. int Graph()
  252. {
  253.     double y1;
  254.     int x1;
  255.     int *a = new int[x1];
  256.     double x2;
  257.     x2 = x1;
  258.     initwindow(WID, HIGH);
  259.     moveto(WID / 2, HIGH / 2);
  260.     for (x1 = -WID; x1 < WID; x2 += 0.1)
  261.     {
  262.         FillRand(x1,a);
  263.         y1 = MCBubbleSort(x1,a);
  264.         setcolor(4);
  265.         x2 = x1;
  266.         x1+=1;
  267.         lineto((WID / 2) + (x1 * 10), (HIGH / 2) - (y1 * 10));
  268.     }
  269.     for (x1 = -WID; x1 < WID; x2 += 0.1)
  270.     {
  271.         FillRand(x1,a);
  272.         y1 = MCShakerSort(x1,a);
  273.         setcolor(3);
  274.         x2 = x1;
  275.         x1+=1;
  276.         lineto((WID / 2) + (x1 * 10), (HIGH / 2) - (y1 * 10));
  277.     }
  278.     drawLine(0, HIGH / 2, WID, HIGH / 2, 15, WID - 20, HIGH / 2 + 10, "X");
  279.     drawLine(WID / 2, 0, WID / 2, HIGH, 15, WID / 2 + 10, 0, "Y");
  280.     GraphText(8, 50, 33, 75, 4, 47, 63, "Bubble Sort");
  281.     GraphText(8, 88, 33, 113, 3, 47, 99, "Shaker Sort");
  282.     getch();
  283.     closegraph();
  284.     return 0;
  285. }
  286. int prompt_menu_item()
  287. {
  288.     int variant;
  289.     cout << "Âûáåðèòå ГЇГіГ­ГЄГІ ìåíþ \n" << endl;
  290.     cout << "1. \n"
  291.          << "2. \n"
  292.          << "3. \n"
  293.          << "4. Shaker Sort\n"
  294.          << "5. \n"
  295.          << "6. \n"
  296.          << "7. " << endl;
  297.     cout << ">>> ";
  298.     cin >> variant;
  299.     return variant;
  300. }
  301. int main()
  302. {
  303.     int n;
  304.     int *a = new int[n];
  305.     setlocale(LC_ALL, "Russian");
  306. /*  cout << "Ââåäèòå ðàçìåð ìàññèâà: ";
  307.     cin >> n;
  308.     int variant = prompt_menu_item();
  309.     switch (variant)
  310.     {
  311.         case 1:
  312.         {
  313.             break;
  314.         }
  315.            
  316.         case 2:
  317.         {
  318.             break;
  319.         }  
  320.         case 3:
  321.         {
  322.             break;
  323.         }
  324.          
  325.         case 4:
  326.         {
  327.             FillRand(n,a);
  328.             PrintMas(n,a);
  329.             cout << endl;
  330.             ShakerCheckMC(n);
  331.             cout << "Sorting..." << endl;
  332.             ShakerSort(n,a);
  333.             PrintMas(n,a);
  334.             PrintTabl();
  335.             break;
  336.         }
  337.         case 5:
  338.         {
  339.             break;
  340.         }
  341.         case 6:
  342.         {
  343.             break;
  344.         }
  345.         case 7:
  346.         {
  347.            break;
  348.         }
  349.     }
  350.    */
  351.     cout << "N:=  ";
  352.     cin >> n;
  353.     FillDec(n,a);
  354.     FillRand(n,a);
  355.     PrintMas(n,a);
  356.     cout << endl;
  357.     ShakerCheckMC(n);
  358.     cout << "Sorting..." << endl;
  359.     ShakerSort(n,a);
  360.     PrintMas(n,a);
  361.     PrintTabl();
  362.     Graph();
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement