Xom9ik

Alg_Lab_4 (lll semester)

Oct 10th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.66 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 InsertionSort(int mass[], int size) // сортировка вставками
  11. {
  12.     int newElement, location;
  13.     for (int i = 1; i < size; i++)
  14.     {
  15.         newElement = mass[i];
  16.         location = i - 1;
  17.         while (location >= 0 && mass[location] > newElement)
  18.         {
  19.             mass[location + 1] = mass[location];
  20.             location = location - 1;
  21.         }
  22.         mass[location + 1] = newElement;
  23.     }
  24. }
  25. void choicesSort(int mass[], int size) // сортировка выбором
  26. {
  27.     for (int repeat_counter = 0; repeat_counter < size; repeat_counter++)
  28.     {
  29.         int temp = mass[0]; // временная переменная для хранения значения перестановки
  30.         for (int element_counter = repeat_counter + 1; element_counter < size; element_counter++)
  31.             if (mass[repeat_counter] > mass[element_counter])
  32.                 swap(mass[repeat_counter], mass[element_counter]);
  33.     }
  34. }
  35. void Merge(int mass[], int first, int last)
  36. {
  37.     int middle, start, final, j;
  38.     int *temp = new int[100];
  39.     middle = (first + last) / 2; //вычисление среднего элемента
  40.     start = first; //начало левой части
  41.     final = middle + 1; //начало правой части
  42.     for (j = first; j <= last; j++) //выполнять от начала до конца
  43.         if ((start <= middle) && ((final>last) || (mass[start]<mass[final])))
  44.         {
  45.             temp[j] = mass[start];
  46.             start++;
  47.         }
  48.         else
  49.         {
  50.             temp[j] = mass[final];
  51.             final++;
  52.         }
  53.     //возвращение результата в список
  54.     for (j = first; j <= last; j++) mass[j] = temp[j];
  55.     delete[]temp;
  56. };
  57. //рекурсивная процедура сортировки
  58. void MergeSort(int mass[], int first, int last) //сортировка слиянием
  59. {
  60.     {
  61.         if (first<last)
  62.         {
  63.             MergeSort(mass, first, (first + last) / 2); //сортировка левой части
  64.             MergeSort(mass, (first + last) / 2 + 1, last); //сортировка правой части
  65.             Merge(mass, first, last); //слияние двух частей
  66.         }
  67.     }
  68. }
  69.  
  70. int main()
  71. {
  72.     srand(time(NULL));
  73.     int option = 0;
  74.     cout << "1.Manual input" << endl;
  75.     cout << "2.Random" << endl;
  76.     cout << "3.File" << endl;
  77.     while (1)
  78.     {
  79.         cout << "Enter option: ";
  80.         cin >> option;
  81.         switch (option)
  82.         {
  83.         case 1:
  84.         {
  85.             int size, method;
  86.             cout << "Enter size: ";
  87.             cin >> size;
  88.             int *massiv = new int[size];
  89.             cout << "Enter value: " << endl;
  90.             for (int i = 0; i < size; i++)
  91.             {
  92.                 cout << "[" << i << "]= ";
  93.                 cin >> massiv[i];
  94.             }
  95.             cout << "\nInsertion(1) or Choices(2) or Merge(3): ";
  96.             cin >> method;
  97.             if (method == 1)
  98.                 InsertionSort(massiv, size);
  99.             else if (method == 2)
  100.                 choicesSort(massiv, size);
  101.             else if (method == 3)
  102.                 MergeSort(massiv, 0, size - 1);
  103.             else
  104.                 cout << "Enter 1 or 2" << endl;
  105.             cout << "\nSorting massiv: " << endl;
  106.             for (int i = 0; i < size; i++)
  107.                 cout << massiv[i] << " ";
  108.             cout << endl;
  109.             break;
  110.         }
  111.         case 2:
  112.         {
  113.             int size, method;
  114.             cout << "Enter size: ";
  115.             cin >> size;
  116.             int *massiv = new int[size];
  117.             cout << "Massiv: " << endl;
  118.             for (int i = 0; i < size; i++)
  119.             {
  120.                 massiv[i] = rand() % 100;
  121.                 cout << massiv[i] << " ";
  122.             }
  123.             cout << "\nInsertion(1) or Choices(2) or Merge(3): ";
  124.             cin >> method;
  125.             if (method == 1)
  126.                 InsertionSort(massiv, size);
  127.             else if (method == 2)
  128.                 choicesSort(massiv, size);
  129.             else if (method == 3)
  130.                 MergeSort(massiv, 0, size - 1);
  131.             else
  132.                 cout << "Enter 1 or 2" << endl;
  133.             cout << "\nSorting massiv: " << endl;
  134.             for (int i = 0; i < size; i++)
  135.                 cout << massiv[i] << " ";
  136.             cout << endl;
  137.             break;
  138.         }
  139.         case 3:
  140.         {
  141.             int size, method;
  142.             ifstream myfile("file.txt");
  143.             vector<int> vec;
  144.             cout << "Massiv: " << endl;
  145.             int id = 0;
  146.             int buf;
  147.             while (myfile >> buf)
  148.             {
  149.                 vec.push_back(buf);
  150.                 cout << vec[id] << " ";
  151.                 id++;
  152.             }
  153.             size = vec.size();
  154.             int* massiv = &vec[0];
  155.             cout << "\nInsertion(1) or Choices(2) or Merge(3): ";
  156.             cin >> method;
  157.             if (method == 1)
  158.                 InsertionSort(massiv, size);
  159.             else if (method == 2)
  160.                 choicesSort(massiv, size);
  161.             else if (method == 3)
  162.                 MergeSort(massiv, 0, size - 1);
  163.             else
  164.                 cout << "Enter 1 or 2" << endl;
  165.             ofstream output("file_out.txt");
  166.             cout << "\nSorting massiv: " << endl;
  167.             for (int i = 0; i < size; i++)
  168.                 cout << massiv[i] << " ";
  169.             cout << endl;
  170.             for (int i = 0; i < size; i++)
  171.                 output << massiv[i] << " ";
  172.             break;
  173.         }
  174.         default: cout << "Enter 1-3" << endl; break;
  175.         }
  176.     }
  177.     system("pause");
  178.     return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment