Guest User

Untitled

a guest
Apr 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 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 timer_start;
  15. unsigned int timer_end;
  16. unsigned int millis;
  17.  
  18. void initMatrix()
  19. {
  20.     // Initialize A matrix with random integers in the range <1, 200.000>
  21.     for(unsigned short i=0; i<N; i++)
  22.     {
  23.         A[i] = rand() % 200000 + 1;
  24.         //cout << A[i] << endl;
  25.     }
  26. }
  27.  
  28. void Selection()
  29. {
  30.     unsigned short i,k,j;
  31.     unsigned int max;
  32.     for (i=0; i<N; i++)
  33.     {
  34.         k = i;
  35.         max = A[i];
  36.         for (j = i+1; j<N; j++)
  37.         {
  38.             if (A[j] > max)
  39.             {
  40.                 k = j;
  41.                 max = A[j];
  42.             }
  43.         }
  44.         A[k] = A[i];
  45.         A[i] = max;
  46.         cout << A[i] << endl;
  47.      }
  48. }
  49.  
  50. int main()
  51. {
  52.     // Initialize random seed
  53.     srand ( time(NULL) );
  54.  
  55.     N = 0;
  56.  
  57.     short menuItem = 0;
  58.     cout << "======================" << endl;
  59.     cout << "         MENU         " << endl;
  60.     cout << "======================" << endl;
  61.     cout << "1. Selection Sort     " << endl;
  62.     cout << "2. Insertion Sort     " << endl;
  63.     cout << "3. Exit               " << endl;
  64.     cout << "4. Bubble Sort        " << endl;
  65.     cout << "5. Heap Sort          " << endl;
  66.     cout << "6. Merge Sort         " << endl;
  67.     cout << "7. Quicksort          " << endl;
  68.  
  69.     while(menuItem < 1 || menuItem > 7)
  70.     {
  71.         cout << "\nPlease enter your choice(1 to 7): ";
  72.         cin >> menuItem;
  73.     }
  74.  
  75.     if(menuItem==3)
  76.     {
  77.         cout << "Bye bye!" << endl;
  78.         return 0;
  79.     }
  80.  
  81.     // For performance reasons do not allow the user to enter a big length.
  82.     while(N<=0 || N>255)
  83.     {
  84.         cout << "Now please enter the length of A matrix(1 to 255): ";
  85.         cin >> N;
  86.     }
  87.  
  88.     // Allocate N unsigned integers
  89.     A = new unsigned int[N];
  90.  
  91.     switch (menuItem)
  92.     {
  93.      case 1:
  94.         cout << "\nSelection Sort" << endl;
  95.         cout << "==============" << endl;
  96.         for(short i=0; i<10; i++)
  97.         {
  98.             initMatrix();
  99.             timer_start = GetTickCount();
  100.             Selection();
  101.             timer_end = GetTickCount();
  102.             millis = timer_end - timer_start;
  103.             cout << "Took " << millis << " ms\n" << endl;
  104.         }
  105.         break;
  106.      case 2:
  107.         cout << "Insertion Sort" << endl;
  108.         for(short i=0; i<10; i++)
  109.         {
  110.             initMatrix();
  111.         }
  112.         break;
  113.      case 4:
  114.         cout << "Bubble Sort" << endl;
  115.         break;
  116.      case 5:
  117.         cout << "Heap Sort" << endl;
  118.         break;
  119.      case 6:
  120.         cout << "Merge Sort" << endl;
  121.         break;
  122.      case 7:
  123.         cout << "Quick Sort" << endl;
  124.         break;
  125.     }
  126.  
  127.     return 0;
  128. }
Add Comment
Please, Sign In to add comment