Advertisement
Xom9ik

Alg_Lab_6 (lll semester)

Oct 24th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.18 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <ctime>
  4. #include <random>
  5. #include <fstream>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. void ShellSort(int mass[], int size)
  11. {
  12.     int i, j, step;
  13.     int tmp;
  14.     for (step = size / 2; step > 0; step /= 2)
  15.     {
  16.         for (i = step; i < size; i++)
  17.         {
  18.             tmp = mass[i];
  19.             for (j = i; j >= step; j -= step)
  20.             {
  21.                 if (tmp < mass[j - step])
  22.                     mass[j] = mass[j - step];
  23.                 else
  24.                     break;
  25.             }
  26.             mass[j] = tmp;
  27.         }
  28.     }
  29. }
  30. void quickSort(int mass[], int size, int left, int right)
  31. {
  32.     int x, i, j;
  33.     i = left;
  34.     j = right;
  35.     x = mass[(left + right) / 2];
  36.     while (i <= j)
  37.     {
  38.         while (mass[i]<x)
  39.             i++;
  40.         while (x<mass[j])
  41.             j--;
  42.         if (i <= j)
  43.         {
  44.             swap(mass[i], mass[j]);
  45.             i++;
  46.             j--;
  47.         }
  48.     }
  49.     if (left<j)
  50.         quickSort(mass, size, left, j);
  51.     if (i<right)
  52.         quickSort(mass, size, i, right);
  53. }
  54. int main()
  55. {
  56.     srand(time(NULL));
  57.     int option = 0;
  58.     cout << "1.Manual input" << endl;
  59.     cout << "2.Random" << endl;
  60.     cout << "3.File" << endl;
  61.     while (1)
  62.     {
  63.         cout << "Enter option: ";
  64.         cin >> option;
  65.         switch (option)
  66.         {
  67.         case 1:
  68.         {
  69.             int size, method;
  70.             cout << "Enter size: ";
  71.             cin >> size;
  72.             int *massiv = new int[size];
  73.             cout << "Enter value: " << endl;
  74.             for (int i = 0; i < size; i++)
  75.             {
  76.                 cout << "[" << i << "]= ";
  77.                 cin >> massiv[i];
  78.             }
  79.             cout << "\nShellSort(1) or QuickSort(2): ";
  80.             cin >> method;
  81.             if (method == 1)
  82.                 ShellSort(massiv, size);
  83.             else if (method == 2)
  84.                 quickSort(massiv, size, 0, size - 1);
  85.             else
  86.                 cout << "Enter 1 or 2" << endl;
  87.             cout << "\nSorting massiv: " << endl;
  88.             for (int i = 0; i < size; i++)
  89.                 cout << massiv[i] << " ";
  90.             cout << endl;
  91.             break;
  92.         }
  93.         case 2:
  94.         {
  95.             int size, method;
  96.             cout << "Enter size: ";
  97.             cin >> size;
  98.             int *massiv = new int[size];
  99.             cout << "Massiv: " << endl;
  100.             for (int i = 0; i < size; i++)
  101.             {
  102.                 massiv[i] = rand() % 100;
  103.                 cout << massiv[i] << " ";
  104.             }
  105.             cout << "\nShellSort(1) or QuickSort(2): ";
  106.             cin >> method;
  107.             if (method == 1)
  108.                 ShellSort(massiv, size);
  109.             else if (method == 2)
  110.                 quickSort(massiv, size, 0, size - 1);
  111.             else
  112.                 cout << "Enter 1 or 2" << endl;
  113.             cout << "\nSorting massiv: " << endl;
  114.             for (int i = 0; i < size; i++)
  115.                 cout << massiv[i] << " ";
  116.             cout << endl;
  117.             break;
  118.         }
  119.         case 3:
  120.         {
  121.             int size, method;
  122.             ifstream myfile("file.txt");
  123.             vector<int> vec;
  124.             cout << "Massiv: " << endl;
  125.             int id = 0;
  126.             int buf;
  127.             while (myfile >> buf)
  128.             {
  129.                 vec.push_back(buf);
  130.                 cout << vec[id] << " ";
  131.                 id++;
  132.             }
  133.             size = vec.size();
  134.             int* massiv = &vec[0];
  135.             cout << "\nShellSort(1) or QuickSort(2): ";
  136.             cin >> method;
  137.             if (method == 1)
  138.                 ShellSort(massiv, size);
  139.             else if (method == 2)
  140.                 quickSort(massiv, size, 0, size - 1);
  141.             else
  142.                 cout << "Enter 1 or 2" << endl;
  143.             ofstream output("file_out.txt");
  144.             cout << "\nSorting massiv: " << endl;
  145.             for (int i = 0; i < size; i++)
  146.                 cout << massiv[i] << " ";
  147.             cout << endl;
  148.             for (int i = 0; i < size; i++)
  149.                 output << massiv[i] << " ";
  150.             break;
  151.         }
  152.         default: cout << "Enter 1-3" << endl; break;
  153.         }
  154.     }
  155.     system("pause");
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement