Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Создать класс Sort, который будет содержать в себе реализацию сортировок:
- 2)выбором;
- 4)быструю;
- Сравнить их работу на различных данных: полностью отсортированные,
- отсортированные в обратном порядке, частично отсортированные данные.
- */
- #include <iostream>
- #include <Windows.h>
- #include <fstream>
- #include <ctime>
- #include <algorithm>
- #include <string>
- #include "Sort.h"
- using namespace std;
- void create_data(string, int);
- void fill_array(int*, string, int);
- void output_array(int*, int);
- bool comp(int, int);//компаратор, для функции sort для сортировки в обратном порядке
- int compare1 = 0;
- int compare2 = 0;
- int replace1 = 0;
- int replace2 = 0;
- class Sort
- {
- private:
- int* arr;
- void quick_sort(int* arr, int first, int last)
- {
- int mid;
- int f = first, l = last;
- mid = arr[(f + l) / 2]; //вычисление опорного элемента
- do
- {
- while (arr[f] < mid)
- {
- f++;
- compare2++;
- }
- while (arr[l] > mid)
- {
- l--;
- compare2++;
- }
- compare2 += 2;
- if (f <= l) //перестановка элементов
- {
- swap(arr[f], arr[l]);
- replace2++;
- f++;
- l--;
- }
- } while (f < l);
- if (first < l) quick_sort(arr, first, l);
- if (f < last) quick_sort(arr, f, last);
- }
- public:
- Sort(int* arr2)
- {
- arr = arr2;
- }
- double selection_sort(int size)
- {
- double start = clock();
- for (int i = 0; i < size - 1; i++)
- {
- int min = i;
- for (int j = i + 1; j < size; j++)
- {
- compare1++;
- if (arr[j] < arr[min])
- min = j;
- }
- if (min != i)
- {
- swap(arr[i], arr[min]);
- replace1++;
- }
- }
- double stop = clock();
- return ((stop - start) / CLK_TCK);
- }
- double quick_sort_count(int* arr, int first, int last)
- {
- double start = clock();
- quick_sort(arr, first, last);
- double stop = clock();
- return ((stop - start) / CLK_TCK);
- }
- };
- int main()
- {
- SetConsoleOutputCP(1251);
- SetConsoleCP(1251);
- int q;
- do
- {
- string Sorted_Data = "Sorted_Data.txt";
- string Reverse_Sorted_Data = "Reverse_Sorted_Data.txt";
- string Half_Sorted_Data = "Half_Sorted_Data.txt";
- int size;
- cout << "Введите размер массива:" << endl;
- cin >> size;
- create_data(Sorted_Data, size);
- create_data(Reverse_Sorted_Data, size);
- create_data(Half_Sorted_Data, size);
- int* selection_arr = new int[size];
- int* quick_arr = new int[size];
- ///////////////////////////////////////////////////////////////////////////
- fill_array(selection_arr, Sorted_Data, size);
- fill_array(quick_arr, Sorted_Data, size);
- Sort tmp(selection_arr);
- cout << "\nРабота сортировок на отсортированных данных:" << endl;
- cout << "Сортировка выбором (секунд): " << tmp.selection_sort(size) << endl;
- cout << "Быстрая сортировка (секунд): " << tmp.quick_sort_count(quick_arr, 0, size - 1) << endl;
- cout << "В сортировке выбором сравнений: " << compare1 << ", перестановок: " << replace1 << endl;
- cout << "В быстрой сортировке сравнений: " << compare2 << ", перестановок: " << replace2 << endl;
- compare1 = compare2 = replace1 = replace2 = 0;
- ///////////////////////////////////////////////////////////////////////////
- fill_array(selection_arr, Reverse_Sorted_Data, size);
- fill_array(quick_arr, Reverse_Sorted_Data, size);
- Sort tmp2(selection_arr);
- cout << "\nРабота сортировок на отсортированных в обратном порядке данных:" << endl;
- cout << "Сортировка выбором (секунд): " << tmp.selection_sort(size) << endl;
- cout << "Быстрая сортировка (секунд): " << tmp.quick_sort_count(quick_arr, 0, size - 1) << endl;
- cout << "В сортировке выбором сравнений: " << compare1 << ", перестановок: " << replace1 << endl;
- cout << "В быстрой сортировке сравнений: " << compare2 << ", перестановок: " << replace2 << endl;
- compare1 = compare2 = replace1 = replace2 = 0;
- ///////////////////////////////////////////////////////////////////////////
- fill_array(selection_arr, Half_Sorted_Data, size);
- fill_array(quick_arr, Half_Sorted_Data, size);
- Sort tmp3(selection_arr);
- cout << "\nРабота сортировок на частично отсортированных данных:" << endl;
- cout << "Сортировка выбором (секунд): " << tmp.selection_sort(size) << endl;
- cout << "Быстрая сортировка (секунд): " << tmp.quick_sort_count(quick_arr, 0, size - 1) << endl;
- cout << "В сортировке выбором сравнений: " << compare1 << ", перестановок: " << replace1 << endl;
- cout << "В быстрой сортировке сравнений: " << compare2 << ", перестановок: " << replace2 << endl;
- compare1 = compare2 = replace1 = replace2 = 0;
- cout << "\n0. Выход" << endl;
- cin >> q;
- } while (q != 0);
- }
- void create_data(string s1, int size)
- {
- int* arr = new int[size];
- srand(static_cast<int>(time(0)));
- for (int i = 0; i < size; i++)
- arr[i] = rand() % 10000;
- if (s1 == "Sorted_Data.txt")
- sort(arr, arr + size);
- else if (s1 == "Reverse_Sorted_Data.txt")
- sort(arr, arr + size, comp);
- else if (s1 == "Half_Sorted_Data.txt")
- sort(arr, arr + size / 2);
- fstream file_1;
- file_1.open(s1, ios::out);
- for (int i = 0; i < size; i++)
- {
- file_1 << arr[i];
- file_1 << ' ';
- }
- file_1.close();
- }
- void fill_array(int* arr, string s1, int size)
- {
- fstream file_1;
- file_1.open(s1, ios::in);
- for (int i = 0; i < size; i++)
- file_1 >> arr[i];
- file_1.close();
- }
- void output_array(int* arr, int size)
- {
- for (int i = 0; i < size; i++)
- {
- cout << arr[i] << '\t';
- if (!((i + 1) % 15))
- cout << endl;
- }
- cout << endl;
- }
- bool comp(int first, int second)
- {
- return first > second;
- }
Add Comment
Please, Sign In to add comment