Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <string>
- #include <ctime>
- #include <algorithm>
- #include <windows.h>
- #include <cstdio>
- using namespace std;
- struct qtys {
- int comparisons;
- int assignments;
- };
- void generateArray(int elQty, string type, ofstream &fout) {
- if (type == "increasing") {
- for (int i = 0; i < elQty; i++) {
- fout << i + 1 << " ";
- }
- fout << endl << endl;
- return;
- }
- if (type == "decreasing") {
- for (int i = 0; i < elQty; i++) {
- fout << 1000 - i << " ";
- }
- fout << endl << endl;
- return;
- }
- if (type == "random") {
- for (int i = 0; i < elQty; i++) {
- fout << rand() % 100000 << " ";
- }
- fout << endl << endl;
- return;
- }
- }
- void bubbleSort(vector <int> &array, int passesQty, qtys &_qtys) {
- for (int i = array.size() - 1, j = 0; j != passesQty; i--, j++) {
- for (int k = 0; k < i; k++) {
- _qtys.comparisons++;
- if (array[k + 1] < array[k]) {
- int temp = array[k + 1];
- array[k + 1] = array[k];
- array[k] = temp;
- _qtys.assignments += 3;
- }
- }
- }
- }
- void sortInterval (vector <int> &array, int limit, int l, int r, qtys &_qtys) {
- if (r - l + 1 > limit) {
- int pivot = array[(r + l) / 2],
- i = l,
- j = r;
- do {
- _qtys.comparisons += 2;
- while (array[i] < pivot) {
- _qtys.comparisons++;
- i++;
- }
- while (array[j] > pivot) {
- _qtys.comparisons++;
- j--;
- }
- if (i <= j) {
- int temp = array[j];
- array[j] = array[i];
- array[i] = temp;
- i++;
- j--;
- _qtys.assignments += 3;
- }
- } while (i <= j);
- if (l < j) {
- sortInterval(array, limit, l, j, _qtys);
- }
- if (r > i) {
- sortInterval(array, limit, i, r, _qtys);
- }
- }
- }
- void quickSort(vector <int> &array, int limit, qtys &_qtys) {
- sortInterval(array, limit, 0, array.size() - 1, _qtys);
- bubbleSort(array, limit, _qtys);
- }
- int main() {
- srand(time(0));
- ofstream fout;
- ifstream fin;
- fin.open ("input.txt", ios::in);
- string fileName;
- cout << "Input fileName" << endl;
- fin >> fileName;
- int arrayLength;
- cout << "Input array length" << endl;
- fin >> arrayLength;
- int l;
- cout << "Input l" << endl;
- fin >> l;
- vector <int> array(arrayLength);
- fin.close();
- fin.open (fileName, ios::in);
- for_each(array.begin(), array.end(), [&fin](int &item) {
- fin >> item;
- });
- qtys sortQtys {0, 0};
- int start_t = clock();
- quickSort(array, l, sortQtys);
- int end_t = clock();
- fout.open(fileName, ios::out | ios::app);
- fout << endl << endl;
- for_each(array.begin(), array.end(), [&fout](int item) {
- fout << item << " ";
- });
- fout << endl << endl << "Time: " << end_t - start_t << " ms" << endl;
- fout << "l: " << l << endl;
- fout << "Comparisons: " << sortQtys.comparisons << endl;
- fout << "Assignments: " << sortQtys.assignments << endl;
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment