Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <chrono>
- #include <ctime>
- using namespace std;
- using namespace chrono;
- int countSwapped, countCheck;
- void reverseCombSort(double* arr, int size) {
- int step = size;
- bool swapped = true;
- while (step > 1 || swapped) {
- if (step > 1)
- step /= 1.2473309;
- swapped = false;
- for (int i = 0; i + step < size; ++i)
- if (arr[i + step] > arr[i]) {
- double tmp = arr[i + step];
- arr[i + step] = arr[i];
- arr[i] = tmp;
- swapped = true;
- }
- }
- }
- void output(double* arr, int size) {
- for (int i = 0; i < size - 1; i++) {
- cout << arr[i] << " ";
- }
- cout << arr[size - 1] << endl << endl;
- }
- void combSort(double* arr, int size) {
- int step = size;
- double tmp;
- bool swapped = true;
- while (step > 1 || swapped) {
- if (step > 1)
- step /= 1.2473309;
- swapped = false;
- for (int i = 0; i + step < size; ++i) {
- countCheck++;
- if (arr[i + step] < arr[i]) {
- countSwapped++;
- tmp = arr[i + step];
- arr[i + step] = arr[i];
- arr[i] = tmp;
- swapped = true;
- }
- }
- }
- }
- void quickSort(double* arr, int size) {
- double tmp;
- int i = 0;
- int j = size - 1;
- double mid = arr[size / 2];
- do {
- while (arr[i] < mid) {
- i++;
- }
- while (arr[j] > mid) {
- j--;
- }
- countCheck++;
- if (i <= j) {
- countSwapped++;
- tmp = arr[i];
- arr[i] = arr[j];
- arr[j] = tmp;
- i++;
- j--;
- }
- } while (i <= j);
- if (j > 0) {
- quickSort(arr, j + 1);
- }
- if (i < size) {
- quickSort(&arr[i], size - i);
- }
- }
- void ShellSort(double* arr, int size) {
- int step = size / 2;
- double tmp;
- while (step > 0)
- {
- for (int i = 0; i < size - step; i++)
- {
- for (int j = i; j >= 0; j--) {
- countCheck++;
- if (arr[j] > arr[j + step]) {
- countSwapped++;
- tmp = arr[j];
- arr[j] = arr[j + step];
- arr[j + step] = tmp;
- }
- else {
- break;
- }
- }
- }
- step /= 2;
- }
- }
- void createrArr(double* arr, int n) {
- for (int i = 0; i < n; i++) {
- arr[i] = (double)rand() / RAND_MAX * 50;
- }
- }
- void contrastSort(int n, int choose) {
- time_point<high_resolution_clock> start, end;
- double* mas_1 = new double[n];
- createrArr(mas_1, n);
- if (1 == choose) {
- combSort(mas_1, n);
- cout << "Best" << endl;
- } else if (2 == choose) {
- reverseCombSort(mas_1, n);
- cout << "Bed" << endl;
- }
- double* mas_2 = new double[n];
- double* mas_3 = new double[n];
- memcpy(mas_2, mas_1, sizeof(double) * n);
- memcpy(mas_3, mas_1, sizeof(double) * n);
- countSwapped = 0;
- countCheck = 0;
- start = high_resolution_clock::now();
- combSort(mas_1, n);
- end = high_resolution_clock::now();
- duration<int64_t, nano> dur_ns = (end - start);
- unsigned long long elapsed_seconds = dur_ns.count();
- cout << "comb: " << elapsed_seconds << " || swap: " << countSwapped << " || check: " << countCheck << endl;
- countSwapped = 0;
- countCheck = 0;
- start = high_resolution_clock::now();
- quickSort(mas_2, n);
- end = high_resolution_clock::now();
- dur_ns = (end - start);
- elapsed_seconds = dur_ns.count();
- cout << "quick: " << elapsed_seconds << " || swap: " << countSwapped << " || check: " << countCheck << endl;
- countSwapped = 0;
- countCheck = 0;
- start = high_resolution_clock::now();
- ShellSort(mas_3, n);
- end = high_resolution_clock::now();
- dur_ns = (end - start);
- elapsed_seconds = dur_ns.count();
- cout << "Shell: " << elapsed_seconds << " || swap: " << countSwapped << " || check: " << countCheck << endl;
- delete mas_1, mas_2, mas_3;
- }
- int main() {
- freopen("out.txt", "w", stdout);
- int lenArr[23] = {1, 2, 3, 4, 5, 10, 15, 20, 25, 30, 40, 50, 75, 100, 150, 200, 250, 300, 400, 500, 600, 800, 1000};
- for (int i = 0; i < 23; i++) {
- cout << "For " << lenArr[i] << " element" << endl;
- contrastSort(lenArr[i], 0);
- cout << endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment