Guest User

Untitled

a guest
Apr 19th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <windows.h>
  5.  
  6. using namespace std;
  7.  
  8. // Pointer to A matrix. Matrix still does not exist.
  9. unsigned int * A;
  10.  
  11. // N length of A matrix. 0 to 65535.
  12. unsigned short N;
  13.  
  14. unsigned int timerStart;
  15. unsigned int timerEnd;
  16. unsigned int sumMillis;
  17. unsigned int sumcompCounter;
  18.  
  19. void initMatrix()
  20. {
  21.     // Initialize A matrix with random integers in the range <1, 200.000>
  22.     for(unsigned short i=0; i<N; i++)
  23.     {
  24.         A[i] = rand() % 200000 + 1;
  25.         //cout << A[i] << endl;
  26.     }
  27. }
  28.  
  29. void SelectionSort()
  30. {
  31.     unsigned short i, j, first;
  32.     unsigned int key;
  33.  
  34.     for (i= N - 1; i > 0; i--)
  35.     {
  36.         first = 0;
  37.         for (j=1; j<=i; j++)
  38.         {
  39.             if (A[j] < A[first])
  40.             {
  41.                 sumcompCounter++; // Increment the comparisons
  42.                 first = j;
  43.             }
  44.         }
  45.         key = A[first];
  46.         A[first] = A[i];
  47.         A[i] = key;
  48.     }
  49. }
  50.  
  51. void InsertionSort()
  52. {
  53.     unsigned short i,j;
  54.     unsigned int key;
  55.  
  56.     for (i=1; i<N; i++)
  57.     {
  58.         j = i;
  59.         while (j>0 && A[j]>A[j-1])
  60.         {
  61.             sumcompCounter++; // Increment the comparisons
  62.             key=A[j];
  63.             A[j]=A[j-1];
  64.             A[j-1]=key;
  65.             j--;
  66.         }
  67.     }
  68.  
  69.     for (i=0; i<N;i++)
  70.         cout << A[i] << endl;
  71.     cout << "" << endl;
  72. }
  73.  
  74. void BubbleSort()
  75. {
  76.     unsigned short i, j, flag = 1;
  77.     unsigned int temp;
  78.  
  79.     for(i = 1; (i <= N) && flag; i++)
  80.     {
  81.         flag = 0;
  82.         for (j=0; j < (N-1); j++)
  83.         {
  84.             if (A[j+1] > A[j])
  85.             {
  86.                 sumcompCounter++; // Increment the comparisons
  87.                 temp = A[j];
  88.                 A[j] = A[j+1];
  89.                 A[j+1] = temp;
  90.                 flag = 1;
  91.             }
  92.         }
  93.     }
  94.  
  95.     for(j=0; j<N; j++)
  96.         cout << A[j] << endl;
  97.     cout << "" << endl;
  98. }
  99.  
  100. int main()
  101. {
  102.     // Initialize random seed
  103.     srand ( time(NULL) );
  104.  
  105.     N = 0;
  106.     sumMillis = 0;
  107.     sumcompCounter = 0;
  108.  
  109.     short menuItem = 0;
  110.     cout << "======================" << endl;
  111.     cout << "         MENU         " << endl;
  112.     cout << "======================" << endl;
  113.     cout << "1. Selection Sort     " << endl;
  114.     cout << "2. Insertion Sort     " << endl;
  115.     cout << "3. Exit               " << endl;
  116.     cout << "4. Bubble Sort        " << endl;
  117.     cout << "5. Heap Sort          " << endl;
  118.     cout << "6. Merge Sort         " << endl;
  119.     cout << "7. Quicksort          " << endl;
  120.  
  121.     while(menuItem < 1 || menuItem > 7)
  122.     {
  123.         cout << "\nPlease enter your choice(1 to 7): ";
  124.         cin >> menuItem;
  125.     }
  126.  
  127.     if(menuItem==3)
  128.     {
  129.         cout << "Bye bye!" << endl;
  130.         return 0;
  131.     }
  132.  
  133.     // For performance reasons do not allow the user to enter a big length.
  134.     while(N<=0 || N>255)
  135.     {
  136.         cout << "Now please enter the length of A matrix(1 to 255): ";
  137.         cin >> N;
  138.     }
  139.  
  140.     // Allocate N unsigned integers
  141.     A = new unsigned int[N];
  142.  
  143.     switch (menuItem)
  144.     {
  145.      case 1:
  146.         cout << "\nSelection Sort" << endl;
  147.         cout << "==============" << endl;
  148.         for(short i=0; i<10; i++)
  149.         {
  150.             initMatrix();
  151.             timerStart = GetTickCount();
  152.             SelectionSort();
  153.             timerEnd = GetTickCount();
  154.             sumMillis += timerEnd - timerStart; // Save execution time.
  155.         }
  156.         cout << "Average execution time " << sumMillis/10 << " ms" << endl;
  157.         cout << "Average comparisons " << sumcompCounter/10 << endl;
  158.         break;
  159.      case 2:
  160.         cout << "Insertion Sort" << endl;
  161.         cout << "==============" << endl;
  162.         for(short i=0; i<10; i++)
  163.         {
  164.             initMatrix();
  165.             timerStart = GetTickCount();
  166.             InsertionSort();
  167.             timerEnd = GetTickCount();
  168.             sumMillis += timerEnd - timerStart; // Save execution time.
  169.         }
  170.         cout << "Average execution time " << sumMillis/10 << " ms" << endl;
  171.         cout << "Average comparisons " << sumcompCounter/10 << endl;
  172.         break;
  173.      case 4:
  174.         cout << "Bubble Sort" << endl;
  175.         cout << "==============" << endl;
  176.         for(short i=0; i<10; i++)
  177.         {
  178.             initMatrix();
  179.             timerStart = GetTickCount();
  180.             BubbleSort();
  181.             timerEnd = GetTickCount();
  182.             sumMillis += timerEnd - timerStart; // Save execution time.
  183.         }
  184.         cout << "Average execution time " << sumMillis/10 << " ms" << endl;
  185.         cout << "Average comparisons " << sumcompCounter/10 << endl;
  186.         break;
  187.      case 5:
  188.         cout << "Heap Sort" << endl;
  189.         cout << "==============" << endl;
  190.         break;
  191.      case 6:
  192.         cout << "Merge Sort" << endl;
  193.         cout << "==============" << endl;
  194.         break;
  195.      case 7:
  196.         cout << "Quick Sort" << endl;
  197.         cout << "==============" << endl;
  198.         break;
  199.     }
  200.  
  201.     return 0;
  202. }
Add Comment
Please, Sign In to add comment