Guest User

Untitled

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