Linkin_Park

S&A lab3 full code

Oct 29th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <string>
  5. #include <ctime>
  6. #include <algorithm>
  7. #include <windows.h>
  8. #include <cstdio>
  9.  
  10. using namespace std;
  11.  
  12. struct qtys {
  13.     int comparisons;
  14.     int assignments;
  15. };
  16.  
  17. void generateArray(int elQty, string type, ofstream &fout) {
  18.     if (type == "increasing") {
  19.         for (int i = 0; i < elQty; i++) {
  20.             fout << i + 1 << " ";
  21.         }
  22.  
  23.         fout << endl << endl;
  24.         return;
  25.     }
  26.  
  27.     if (type == "decreasing") {
  28.         for (int i = 0; i < elQty; i++) {
  29.             fout << 1000 - i << " ";
  30.         }
  31.  
  32.         fout << endl << endl;
  33.         return;
  34.     }
  35.  
  36.     if (type == "random") {
  37.         for (int i = 0; i < elQty; i++) {
  38.             fout << rand() % 100000 << " ";
  39.         }
  40.  
  41.         fout << endl << endl;
  42.         return;
  43.     }
  44. }
  45.  
  46. void bubbleSort(vector <int> &array, int passesQty, qtys &_qtys) {
  47.     for (int i = array.size() - 1, j = 0; j != passesQty; i--, j++) {
  48.         for (int k = 0; k < i; k++) {
  49.             _qtys.comparisons++;
  50.  
  51.             if (array[k + 1] < array[k]) {
  52.                 int temp = array[k + 1];
  53.                 array[k + 1] = array[k];
  54.                 array[k] = temp;
  55.  
  56.                 _qtys.assignments += 3;
  57.             }
  58.         }
  59.     }
  60. }
  61.  
  62. void sortInterval (vector <int> &array, int limit, int l, int r, qtys &_qtys) {
  63.     if (r - l + 1 > limit) {
  64.         int pivot = array[(r + l) / 2],
  65.                 i = l,
  66.                 j = r;
  67.  
  68.         do {
  69.             _qtys.comparisons += 2;
  70.  
  71.             while (array[i] < pivot) {
  72.                 _qtys.comparisons++;
  73.                 i++;
  74.             }
  75.  
  76.             while (array[j] > pivot) {
  77.                 _qtys.comparisons++;
  78.                 j--;
  79.             }
  80.  
  81.             if (i <= j) {
  82.                 int temp = array[j];
  83.                 array[j] = array[i];
  84.                 array[i] = temp;
  85.                 i++;
  86.                 j--;
  87.                 _qtys.assignments += 3;
  88.             }
  89.         } while (i <= j);
  90.  
  91.         if (l < j) {
  92.             sortInterval(array, limit, l, j, _qtys);
  93.         }
  94.  
  95.         if (r > i) {
  96.             sortInterval(array, limit, i, r, _qtys);
  97.         }
  98.     }
  99. }
  100.  
  101. void quickSort(vector <int> &array, int limit, qtys &_qtys) {
  102.     sortInterval(array, limit, 0, array.size() - 1, _qtys);
  103.     bubbleSort(array, limit, _qtys);
  104. }
  105.  
  106. int main() {
  107.     srand(time(0));
  108.     ofstream fout;
  109.     ifstream fin;
  110.  
  111.     fin.open ("input.txt", ios::in);
  112.  
  113.     string fileName;
  114.     cout << "Input fileName" << endl;
  115.     fin >> fileName;
  116.  
  117.     int arrayLength;
  118.     cout << "Input array length" << endl;
  119.     fin >> arrayLength;
  120.  
  121.     int l;
  122.     cout << "Input l" << endl;
  123.     fin >> l;
  124.  
  125.     vector <int> array(arrayLength);
  126.     fin.close();
  127.     fin.open (fileName, ios::in);
  128.  
  129.     for_each(array.begin(), array.end(), [&fin](int &item) {
  130.         fin >> item;
  131.     });
  132.  
  133.     qtys sortQtys {0, 0};
  134.  
  135.     int start_t = clock();
  136.     quickSort(array, l, sortQtys);
  137.     int end_t = clock();
  138.  
  139.     fout.open(fileName, ios::out | ios::app);
  140.  
  141.     fout << endl << endl;
  142.  
  143.     for_each(array.begin(), array.end(), [&fout](int item) {
  144.         fout << item << " ";
  145.     });
  146.  
  147.     fout << endl << endl << "Time: " << end_t - start_t << " ms" << endl;
  148.     fout << "l: " << l << endl;
  149.     fout << "Comparisons: " << sortQtys.comparisons << endl;
  150.     fout << "Assignments: " << sortQtys.assignments << endl;
  151.  
  152.  
  153.     fin.close();
  154.     fout.close();
  155.  
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment