Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <stdlib.h>
- #include <ctime>
- using namespace std;
- int i, j, n, d, count;
- void Shell(int* x, int n) {
- unsigned long int M = 0, C = 0;
- d = n;
- d = d / 2;
- while (d > 0) {
- for (i = 0; i < n - d; i++) {
- j = i;
- C++;
- while (j >= 0 && x[j] > x[j + d]) {
- C++;
- swap(x[j], x[j + d]);
- j--;
- M++;
- }
- }
- d = d / 2;
- }
- if (n != 10) {
- cout << "C = " << C << " M = " << M << "\n";
- cout << "M + C = " << C + M << endl;
- }
- }
- void BadSort(int* x, int n) {
- unsigned int C = 0, M = 0;
- d = n;
- d = d / d;
- while (d > 0) {
- for (i = 0; i < n - d; i++) {
- j = i;
- C++;
- while (j >= 0 && x[j] > x[j + d]) {
- C++;
- int count = x[j];
- x[j] = x[j + d];
- x[j + d] = count;
- M++;
- j--;
- }
- }
- d = d / 2;
- }
- if (n != 10) {
- cout << "C = " << C << " M = " << M << "\n";
- cout << "M + C = " << C + M << endl;
- }
- }
- void test(int* x, int n) {
- for (int i = 0; i < 10; i++) {
- cin >> x[i];
- }
- }
- void fillarr(int* x, int* a, int n) {
- for (int i = 0; i < n; i++) x[i] = rand();
- for (int i = 0; i < n; i++) a[i] = x[i];
- }
- int main() {
- setlocale(0, "rus");
- const int t = 10, n1 = 60000, n2 = 70000, n3 = 80000, n4 = 90000, n5 = 100000;
- srand(time(0));
- int x[t], x1[n1], x2[n2], x3[n3], x4[n4], x5[n5], a1[n1], a2[n2], a3[n3], a4[n4], a5[n5];
- cout << "Заполните тестовый массив:\n";
- test(x, t);
- cout << "Введённый массив:\n";
- for (int i = 0; i < 10; i++) {
- cout << x[i] << " ";
- }
- cout << endl;
- Shell(x, 10);
- cout << "Отсортированный массив:\n";
- for (int i = 0; i < 10; i++) {
- cout << x[i] << " ";
- }
- cout << endl;
- fillarr(x1, a1, n1);// заполнение массивов для проведения работы
- fillarr(x2, a2, n2);
- fillarr(x3, a3, n3);
- fillarr(x4, a4, n4);
- fillarr(x5, a5, n5);// конец заполнения массивов для работы
- cout << "--------------------------\nn1=60000\nСредний случай\n";
- double t1 = clock();
- Shell(x1, n1);
- double t2 = clock();
- double t3 = t2 - t1;
- cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
- cout << "Наилучший случай\n";
- t1 = clock();
- BadSort(x1, n1);
- t2 = clock();
- t3 = t2 - t1;
- cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
- cout << "Наихудший случай\n";
- t1 = clock();
- BadSort(a1, n1);
- t2 = clock();
- t3 = t2 - t1;
- cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
- cout << "--------------------------\nn2=70000\nСредний случай\n";
- t1 = clock();
- Shell(x2, n2);
- t2 = clock();
- t3 = t2 - t1;
- cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
- cout << "Наилучший случай\n";
- t1 = clock();
- BadSort(x2, n2);
- t2 = clock();
- t3 = t2 - t1;
- cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
- cout << "Наихудший случай\n";
- t1 = clock();
- BadSort(a2, n2);
- t2 = clock();
- t3 = t2 - t1;
- cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
- cout << "--------------------------\nn3=80000\nСредний случай\n";
- t1 = clock();
- Shell(x3, n3);
- t2 = clock();
- t3 = t2 - t1;
- cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
- cout << "Наилучший случай\n";
- t1 = clock();
- BadSort(x3, n3);
- t2 = clock();
- t3 = t2 - t1;
- cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
- cout << "Наихудший случай\n";
- t1 = clock();
- BadSort(a3, n3);
- t2 = clock();
- t3 = t2 - t1;
- cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
- cout << "--------------------------\nn4=90000\nСредний случай\n";
- t1 = clock();
- Shell(x4, n4);
- t2 = clock();
- t3 = t2 - t1;
- cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
- cout << "Наилучший случай\n";
- t1 = clock();
- BadSort(x4, n4);
- t2 = clock();
- t3 = t2 - t1;
- cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
- cout << "Наихудший случай\n";
- t1 = clock();
- BadSort(a4, n4);
- t2 = clock();
- t3 = t2 - t1;
- cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
- cout << "--------------------------\nn5=100000\nСредний случай\n";
- t1 = clock();
- Shell(x5, n5);
- t2 = clock();
- t3 = t2 - t1;
- cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
- cout << "Наилучший случай\n";
- t1 = clock();
- BadSort(x5, n5);
- t2 = clock();
- t3 = t2 - t1;
- cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
- cout << "Наихудший случай\n";
- t1 = clock();
- BadSort(a5, n5);
- t2 = clock();
- t3 = t2 - t1;
- cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment