Advertisement
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 ShellSort(int mass[], int size)
- {
- int i, j, step;
- int tmp;
- for (step = size / 2; step > 0; step /= 2)
- {
- for (i = step; i < size; i++)
- {
- tmp = mass[i];
- for (j = i; j >= step; j -= step)
- {
- if (tmp < mass[j - step])
- mass[j] = mass[j - step];
- else
- break;
- }
- mass[j] = tmp;
- }
- }
- }
- void quickSort(int mass[], int size, int left, int right)
- {
- int x, i, j;
- i = left;
- j = right;
- x = mass[(left + right) / 2];
- while (i <= j)
- {
- while (mass[i]<x)
- i++;
- while (x<mass[j])
- j--;
- if (i <= j)
- {
- swap(mass[i], mass[j]);
- i++;
- j--;
- }
- }
- if (left<j)
- quickSort(mass, size, left, j);
- if (i<right)
- quickSort(mass, size, i, right);
- }
- 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 << "\nShellSort(1) or QuickSort(2): ";
- cin >> method;
- if (method == 1)
- ShellSort(massiv, size);
- else if (method == 2)
- quickSort(massiv, size, 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 << "\nShellSort(1) or QuickSort(2): ";
- cin >> method;
- if (method == 1)
- ShellSort(massiv, size);
- else if (method == 2)
- quickSort(massiv, size, 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 << "\nShellSort(1) or QuickSort(2): ";
- cin >> method;
- if (method == 1)
- ShellSort(massiv, size);
- else if (method == 2)
- quickSort(massiv, size, 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
Advertisement