Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <ctime>
- #include <random>
- #include <fstream>
- #include <vector>
- using namespace std;
- void InsertionSort(int mass[], int size) // сортировка вставками
- {
- int newElement, location;
- for (int i = 1; i < size; i++)
- {
- newElement = mass[i];
- location = i - 1;
- while (location >= 0 && mass[location] > newElement)
- {
- mass[location + 1] = mass[location];
- location = location - 1;
- }
- mass[location + 1] = newElement;
- }
- }
- void choicesSort(int mass[], int size) // сортировка выбором
- {
- for (int repeat_counter = 0; repeat_counter < size; repeat_counter++)
- {
- int temp = mass[0]; // временная переменная для хранения значения перестановки
- for (int element_counter = repeat_counter + 1; element_counter < size; element_counter++)
- if (mass[repeat_counter] > mass[element_counter])
- swap(mass[repeat_counter], mass[element_counter]);
- }
- }
- void Merge(int mass[], int first, int last)
- {
- int middle, start, final, j;
- int *temp = new int[100];
- middle = (first + last) / 2; //вычисление среднего элемента
- start = first; //начало левой части
- final = middle + 1; //начало правой части
- for (j = first; j <= last; j++) //выполнять от начала до конца
- if ((start <= middle) && ((final>last) || (mass[start]<mass[final])))
- {
- temp[j] = mass[start];
- start++;
- }
- else
- {
- temp[j] = mass[final];
- final++;
- }
- //возвращение результата в список
- for (j = first; j <= last; j++) mass[j] = temp[j];
- delete[]temp;
- };
- //рекурсивная процедура сортировки
- void MergeSort(int mass[], int first, int last) //сортировка слиянием
- {
- {
- if (first<last)
- {
- MergeSort(mass, first, (first + last) / 2); //сортировка левой части
- MergeSort(mass, (first + last) / 2 + 1, last); //сортировка правой части
- Merge(mass, first, last); //слияние двух частей
- }
- }
- }
- int main()
- {
- srand(time(NULL));
- int option = 0;
- cout << "1.Manual input" << endl;
- cout << "2.Random" << endl;
- cout << "3.File" << endl;
- while (1)
- {
- cout << "Enter option: ";
- cin >> option;
- switch (option)
- {
- case 1:
- {
- int size, method;
- cout << "Enter size: ";
- cin >> size;
- int *massiv = new int[size];
- cout << "Enter value: " << endl;
- for (int i = 0; i < size; i++)
- {
- cout << "[" << i << "]= ";
- cin >> massiv[i];
- }
- cout << "\nInsertion(1) or Choices(2) or Merge(3): ";
- cin >> method;
- if (method == 1)
- InsertionSort(massiv, size);
- else if (method == 2)
- choicesSort(massiv, size);
- else if (method == 3)
- MergeSort(massiv, 0, size - 1);
- else
- cout << "Enter 1 or 2" << endl;
- cout << "\nSorting massiv: " << endl;
- for (int i = 0; i < size; i++)
- cout << massiv[i] << " ";
- cout << endl;
- break;
- }
- case 2:
- {
- int size, method;
- cout << "Enter size: ";
- cin >> size;
- int *massiv = new int[size];
- cout << "Massiv: " << endl;
- for (int i = 0; i < size; i++)
- {
- massiv[i] = rand() % 100;
- cout << massiv[i] << " ";
- }
- cout << "\nInsertion(1) or Choices(2) or Merge(3): ";
- cin >> method;
- if (method == 1)
- InsertionSort(massiv, size);
- else if (method == 2)
- choicesSort(massiv, size);
- else if (method == 3)
- MergeSort(massiv, 0, size - 1);
- else
- cout << "Enter 1 or 2" << endl;
- cout << "\nSorting massiv: " << endl;
- for (int i = 0; i < size; i++)
- cout << massiv[i] << " ";
- cout << endl;
- break;
- }
- case 3:
- {
- int size, method;
- ifstream myfile("file.txt");
- vector<int> vec;
- cout << "Massiv: " << endl;
- int id = 0;
- int buf;
- while (myfile >> buf)
- {
- vec.push_back(buf);
- cout << vec[id] << " ";
- id++;
- }
- size = vec.size();
- int* massiv = &vec[0];
- cout << "\nInsertion(1) or Choices(2) or Merge(3): ";
- cin >> method;
- if (method == 1)
- InsertionSort(massiv, size);
- else if (method == 2)
- choicesSort(massiv, size);
- else if (method == 3)
- MergeSort(massiv, 0, size - 1);
- else
- cout << "Enter 1 or 2" << endl;
- ofstream output("file_out.txt");
- cout << "\nSorting massiv: " << endl;
- for (int i = 0; i < size; i++)
- cout << massiv[i] << " ";
- cout << endl;
- for (int i = 0; i < size; i++)
- output << massiv[i] << " ";
- break;
- }
- default: cout << "Enter 1-3" << endl; break;
- }
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment