Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- #include <algorithm>
- #include <cmath>
- #include <string>
- #include <set>
- #include <map>
- #include <iomanip>
- #include <time.h>
- #include <stdlib.h>
- using namespace std;
- //Задача: реализовать сортировки: 1) вставкой, 2) бинарной вставкой, 3)Шелла; и посчитать количество сравнений
- //и количество действий для каждой сортировки
- int ks = 0, kd = 0;
- void sortvst(vector <int> vv, int sz)
- {
- cout << "Start array: ";
- for (int i = 0; i < sz; ++i)
- cout << vv[i] << ' ';
- cout << '\n';
- ks = 0; kd = 0;
- for (int i = 1; i < sz; ++i)
- {
- int zn = vv[i], j = i - 1;
- while (j >= 0 && zn < vv[j])
- {
- ks++;
- vv[j + 1] = vv[j];
- kd++;
- j--;
- }
- vv[j + 1] = zn;
- kd++;
- }
- cout << "Result: ";
- for (int i = 0; i < sz; ++i)
- cout << vv[i] << ' ';
- cout << "\nSravnenii : " << ks << "\nDeistvii : " << kd << "\n\n";
- }
- void binsortvst(vector <int> vv, int sz)
- {
- cout << "Start array: ";
- for (int i = 0; i < sz; ++i)
- cout << vv[i] << ' ';
- cout << '\n';
- ks = 0; kd = 0;
- for (int i = 1; i < sz; ++i)
- {
- if (vv[i - 1] > vv[i])
- {
- int zn = vv[i];
- int r(i - 1), l(0);
- while (l <= r)
- {
- ks++;
- int mid = (l + r) / 2;
- kd++;
- if (vv[mid] > zn)
- r = mid - 1;
- else
- l = mid + 1;
- kd++;
- }
- for (int j = i - 1; j >= l; --j)
- {
- vv[j + 1] = vv[j];
- kd++;
- }
- vv[l] = zn;
- kd++;
- }
- }
- cout << "Result: ";
- for (int i = 0; i < sz; ++i)
- cout << vv[i] << ' ';
- cout << "\nSravnenii : " << ks << "\nDeistvii : " << kd << "\n\n";
- }
- void Shell(vector <int> vv, int sz)
- {
- ks = 0; kd = 0;
- cout << "Start array: ";
- for (int i = 0; i < sz; ++i)
- cout << vv[i] << ' ';
- cout << '\n';
- int d = sz / 3 + 1;
- while (true)
- {
- for (int i = d; i < sz; i++)
- {
- int key = vv[i];
- int j = i - d;
- while (j >= 0 && vv[j] > key)
- {
- ks++;
- vv[j + d] = vv[j];
- kd++;
- j -= d;
- }
- vv[j + d] = key;
- kd++;
- }
- if (d == 1)
- break;
- d = d / 3 + 1;
- }
- cout << "Result: ";
- for (int i = 0; i < sz; ++i)
- cout << vv[i] << ' ';
- cout << "\nSravnenii : " << ks << "\nDeistvii : " << kd << "\n\n";
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- int sz;
- cout << "Input size of array: ";
- cin >> sz;
- vector <int> v(sz);
- srand(time(NULL));
- for (int i = 0; i < sz; ++i)
- {
- v[i] = rand() % 100;
- cout << v[i] << ' ';
- }
- int ans;
- cout << "\nSelect type of sort:\n\t(1) Vstavkoi\n\t(2)Binarnoi vstavkoi\n\t(3)Shella\n";
- cin >> ans;
- while (ans)
- {
- switch (ans)
- {
- case 1: sortvst(v, sz); break;
- case 2: binsortvst(v, sz); break;
- case 3: Shell(v, sz);
- }
- cout << "Select type of sort again(if you need):\n\t(0)Exit\n\t(1) Vstavkoi\n\t(2)Binarnoi vstavkoi\n\t(3)Shella\n";
- cin >> ans;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement