Advertisement
Mike_be

HOLY SHIIEETTT (Bezus lab 3 var 12)

Oct 30th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <ctime>
  4. #include <stdlib.h>
  5. #include <vector>
  6. #include <string>
  7. #include <algorithm>
  8. #include <iomanip>
  9. #include "mikelib.h"
  10.  
  11. using namespace std;
  12.  
  13. void vec_rand(int n, int x, vector<int>& vec, vector<int>& vec_custom_sort) //Randomly generates numbers and fills vector
  14. {
  15.     int i = 0;
  16.     if (x == 0) while (i < n) //Fills vector with absolutely random numbers
  17.     {
  18.         vec.push_back(rand());
  19.         vec_custom_sort.push_back(vec[i]);
  20.         i++;
  21.     }
  22.     else while (i < n)//Fills vector with random numbers with x interval
  23.     {
  24.         vec.push_back(rand() % x);
  25.         vec_custom_sort.push_back(vec[i]);
  26.         i++;
  27.     }
  28. }
  29.  
  30. void vec_print(int n, vector<int>& temp) // Prints whole vector if it's smaller than 50 integers or prints first 50 integers if vector is longer
  31. {
  32.     int i = 0;
  33.     if (n <= 50)
  34.     {
  35.         while (i < n)
  36.         {
  37.             cout << temp[i] << ' ';
  38.             i++;
  39.         }
  40.     }
  41.     else
  42.     {
  43.         while (i < 50)
  44.         {
  45.             cout << temp[i] << ' ';
  46.             i++;
  47.         }
  48.     }
  49.     cout << endl;
  50. }
  51.  
  52. void vec_type(int n, vector<int>& vec, vector<int>& vec_custom_sort) //Function for typing numbers into vector
  53. {
  54.     int i = 0, num;
  55.     while (i < n)
  56.     {
  57.         cout << "Type " << i+1 << "th element of vector: ";
  58.         cin >> num;
  59.         vec.push_back(num);
  60.         vec_custom_sort.push_back(num);
  61.         i++;
  62.     }
  63. }
  64.  
  65. int check(string temp) // Check for input
  66. {
  67.     if (temp == "Yes" || temp == "yes" || temp == "y") return 1;
  68.     else if (temp == "No" || temp == "no" || temp == "n") return 2;
  69.     else return 0;
  70. }
  71.  
  72. void choice(int n, string temp, int x, vector<int>& vec, vector<int>& vec_custom_sort) //Compilation of several functions in one function where it also decides
  73. {                                      //the path depending on output from previous function
  74.     if (check(temp) == 1)
  75.     {
  76.         vec_rand(n, x, vec, vec_custom_sort);
  77.         vec_print(n, vec);
  78.         cout << endl;
  79.     }
  80.     else if (check(temp) == 2)
  81.     {
  82.         vec_type(n, vec, vec_custom_sort);
  83.         vec_print(n, vec);
  84.         cout << endl;
  85.     }
  86. }
  87.  
  88. void ShellsSort(unsigned N, vector<int>& temp)//Shell sorting algorithm
  89. {
  90.     unsigned i, j, d;
  91.     int t;
  92.     for (d = N / 2; d > 0; d /= 2)
  93.         for (i = d; i < N; i++)
  94.         {
  95.             t = temp[i];
  96.             for (j = i; j >= d; j -= d)
  97.             {
  98.                 if (t < temp[j - d])
  99.                     temp[j] = temp[j - d];
  100.                 else
  101.                     break;
  102.             }
  103.             temp[j] = t;
  104.         }
  105. }
  106.  
  107. int main()
  108. {
  109.     vector<int> vec;
  110.     vector<int> vec_custom_sort;
  111.     cout << "Hello! This is program for sorting vectors.\n";//Welcome message
  112.     string temp;
  113.     cout << "Do you want to generate vector with random numbers? (If no, you will need to write in manually) [Yes/No]: ";
  114.     do //Cycle for asking user to decide future path, has input check because this step is important
  115.     {
  116.         getline(cin, temp);
  117.         if (check(temp) == 0) cout << "Type [Yes] or [No] without brackets: ";
  118.     } while (check(temp) == 0);
  119.     int n = read_uint("Type the amount of variable in vector");
  120.     int x = 0;
  121.     if (check(temp) == 1) {//Small "if" for additional input if user said "Yes" in previous step
  122.         x = read_uint("Type the amount of unique integers(0 if you don't need it)");
  123.         cout << endl << setw(70) << "Default vector (or a part of it if it's bigger than 50 integers)" << endl;
  124.     }
  125.     choice(n, temp, x, vec, vec_custom_sort);
  126.     double t = clock(); //Clock function from ctime, gets current clock time and logs it into "t" integer
  127.     sort(vec.begin(), vec.end());
  128.     t = (clock() - (double)t)/CLOCKS_PER_SEC; //Gets current time minus previous logged time
  129.     cout << setw(55) << "Sorted vector by library function" << endl;
  130.     vec_print(n, vec);
  131.     cout << "Computer sorted vector in " << t << " seconds." << endl << endl;
  132.     double t_custom = clock();
  133.     ShellsSort(n, vec_custom_sort);
  134.     t_custom = (clock() - t_custom)/CLOCKS_PER_SEC;
  135.     cout << setw(55) << "Sorted vector by custom function" << endl;
  136.     vec_print(n, vec_custom_sort);
  137.     cout << "Computer sorted vector in " << t_custom << " seconds." << endl << endl;
  138.     cout << "Time difference between default sort function and custom is: " << t_custom - t << " seconds" << endl;
  139.     system("pause");
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement