Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <ctime>
- #include <stdlib.h>
- #include <vector>
- #include <string>
- #include <algorithm>
- #include <iomanip>
- #include "mikelib.h"
- using namespace std;
- void vec_rand(int n, int x, vector<int>& vec, vector<int>& vec_custom_sort) //Randomly generates numbers and fills vector
- {
- int i = 0;
- if (x == 0) while (i < n) //Fills vector with absolutely random numbers
- {
- vec.push_back(rand());
- vec_custom_sort.push_back(vec[i]);
- i++;
- }
- else while (i < n)//Fills vector with random numbers with x interval
- {
- vec.push_back(rand() % x);
- vec_custom_sort.push_back(vec[i]);
- i++;
- }
- }
- 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
- {
- int i = 0;
- if (n <= 50)
- {
- while (i < n)
- {
- cout << temp[i] << ' ';
- i++;
- }
- }
- else
- {
- while (i < 50)
- {
- cout << temp[i] << ' ';
- i++;
- }
- }
- cout << endl;
- }
- void vec_type(int n, vector<int>& vec, vector<int>& vec_custom_sort) //Function for typing numbers into vector
- {
- int i = 0, num;
- while (i < n)
- {
- cout << "Type " << i+1 << "th element of vector: ";
- cin >> num;
- vec.push_back(num);
- vec_custom_sort.push_back(num);
- i++;
- }
- }
- int check(string temp) // Check for input
- {
- if (temp == "Yes" || temp == "yes" || temp == "y") return 1;
- else if (temp == "No" || temp == "no" || temp == "n") return 2;
- else return 0;
- }
- 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
- { //the path depending on output from previous function
- if (check(temp) == 1)
- {
- vec_rand(n, x, vec, vec_custom_sort);
- vec_print(n, vec);
- cout << endl;
- }
- else if (check(temp) == 2)
- {
- vec_type(n, vec, vec_custom_sort);
- vec_print(n, vec);
- cout << endl;
- }
- }
- void ShellsSort(unsigned N, vector<int>& temp)//Shell sorting algorithm
- {
- unsigned i, j, d;
- int t;
- for (d = N / 2; d > 0; d /= 2)
- for (i = d; i < N; i++)
- {
- t = temp[i];
- for (j = i; j >= d; j -= d)
- {
- if (t < temp[j - d])
- temp[j] = temp[j - d];
- else
- break;
- }
- temp[j] = t;
- }
- }
- int main()
- {
- vector<int> vec;
- vector<int> vec_custom_sort;
- cout << "Hello! This is program for sorting vectors.\n";//Welcome message
- string temp;
- cout << "Do you want to generate vector with random numbers? (If no, you will need to write in manually) [Yes/No]: ";
- do //Cycle for asking user to decide future path, has input check because this step is important
- {
- getline(cin, temp);
- if (check(temp) == 0) cout << "Type [Yes] or [No] without brackets: ";
- } while (check(temp) == 0);
- int n = read_uint("Type the amount of variable in vector");
- int x = 0;
- if (check(temp) == 1) {//Small "if" for additional input if user said "Yes" in previous step
- x = read_uint("Type the amount of unique integers(0 if you don't need it)");
- cout << endl << setw(70) << "Default vector (or a part of it if it's bigger than 50 integers)" << endl;
- }
- choice(n, temp, x, vec, vec_custom_sort);
- double t = clock(); //Clock function from ctime, gets current clock time and logs it into "t" integer
- sort(vec.begin(), vec.end());
- t = (clock() - (double)t)/CLOCKS_PER_SEC; //Gets current time minus previous logged time
- cout << setw(55) << "Sorted vector by library function" << endl;
- vec_print(n, vec);
- cout << "Computer sorted vector in " << t << " seconds." << endl << endl;
- double t_custom = clock();
- ShellsSort(n, vec_custom_sort);
- t_custom = (clock() - t_custom)/CLOCKS_PER_SEC;
- cout << setw(55) << "Sorted vector by custom function" << endl;
- vec_print(n, vec_custom_sort);
- cout << "Computer sorted vector in " << t_custom << " seconds." << endl << endl;
- cout << "Time difference between default sort function and custom is: " << t_custom - t << " seconds" << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement