Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Source.cpp
- #include <iostream>
- #include <Windows.h>
- #include <fstream>
- #include <ctime>
- #include <algorithm>
- #include <string>
- #include "Source1.h"
- #include "Sort.h"
- using namespace std;
- int compare1 = 0;
- int compare2 = 0;
- int compare3 = 0;
- int replace1 = 0;
- int replace2 = 0;
- int replace3 = 0;
- 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];
- int* heap_sort_arr = new int[size];
- ///////////////////////////////////////////////////////////////////////////
- fill_array(selection_arr, Sorted_Data, size);
- fill_array(quick_arr, Sorted_Data, size);
- fill_array(heap_sort_arr, Sorted_Data, size);
- Sort tmp(selection_arr);
- Sort tmp_heap(heap_sort_arr);
- cout << "\nРабота сортировок на отсортированных данных:" << endl;
- cout << "Сортировка выбором (секунд): " << tmp.selection_sort(size) << endl;
- cout << "Быстрая сортировка (секунд): " << tmp.quick_sort_count(quick_arr, 0, size - 1) << endl;
- cout << "Сортировка кучей (секунд): " << tmp_heap.heap_sort(heap_sort_arr, size) << endl;
- cout << "В сортировке выбором сравнений: " << compare1 << ", перестановок: " << replace1 << endl;
- cout << "В быстрой сортировке сравнений: " << compare2 << ", перестановок: " << replace2 << endl;
- cout << "В сортировке кучей сравнений: " << compare3 << ", перестановок: " << replace3 << endl;
- compare1 = compare2 = compare3 = replace1 = replace2 = replace3 = 0;
- ///////////////////////////////////////////////////////////////////////////
- fill_array(selection_arr, Reverse_Sorted_Data, size);
- fill_array(quick_arr, Reverse_Sorted_Data, size);
- fill_array(heap_sort_arr, Reverse_Sorted_Data, size);
- Sort tmp2(selection_arr);
- Sort tmp_heap2(heap_sort_arr);
- cout << "\nРабота сортировок на отсортированных в обратном порядке данных:" << endl;
- cout << "Сортировка выбором (секунд): " << tmp2.selection_sort(size) << endl;
- cout << "Быстрая сортировка (секунд): " << tmp2.quick_sort_count(quick_arr, 0, size - 1) << endl;
- cout << "Сортировка кучей (секунд): " << tmp_heap2.heap_sort(heap_sort_arr, size) << endl;
- cout << "В сортировке выбором сравнений: " << compare1 << ", перестановок: " << replace1 << endl;
- cout << "В быстрой сортировке сравнений: " << compare2 << ", перестановок: " << replace2 << endl;
- cout << "В сортировке кучей сравнений: " << compare3 << ", перестановок: " << replace3 << endl;
- compare1 = compare2 = compare3 = replace1 = replace2 = replace3 = 0;
- ///////////////////////////////////////////////////////////////////////////
- fill_array(selection_arr, Half_Sorted_Data, size);
- fill_array(quick_arr, Half_Sorted_Data, size);
- fill_array(heap_sort_arr, Half_Sorted_Data, size);
- Sort tmp3(selection_arr);
- Sort tmp_heap3(heap_sort_arr);
- cout << "\nРабота сортировок на частично отсортированных данных:" << endl;
- cout << "Сортировка выбором (секунд): " << tmp3.selection_sort(size) << endl;
- cout << "Быстрая сортировка (секунд): " << tmp3.quick_sort_count(quick_arr, 0, size - 1) << endl;
- cout << "Сортировка кучей (секунд): " << tmp_heap3.heap_sort(heap_sort_arr, size) << endl;
- cout << "В сортировке выбором сравнений: " << compare1 << ", перестановок: " << replace1 << endl;
- cout << "В быстрой сортировке сравнений: " << compare2 << ", перестановок: " << replace2 << endl;
- cout << "В сортировке кучей сравнений: " << compare3 << ", перестановок: " << replace3 << endl;
- compare1 = compare2 = compare3 = replace1 = replace2 = replace3 = 0;
- cout << "\n0. Выход" << endl;
- cin >> q;
- } while (q != 0);
- }
- //Source1.h
- #pragma once
- #include <iostream>
- #include <Windows.h>
- #include <fstream>
- #include <ctime>
- #include <algorithm>
- #include <string>
- using namespace std;
- void create_data(string, int);
- void fill_array(int*, string, int);
- void output_array(int*, int);
- bool comp(int, int);//компаратор, для функции sort для сортировки в обратном порядке
- //Source1.cpp
- #include "Source1.h"
- 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() % size;
- 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 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;
- }
- 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();
- }
- bool comp(int first, int second)
- {
- return first > second;
- }
- //Sort.h
- #pragma once
- #include <iostream>
- #include <ctime>
- using namespace std;
- class Sort
- {
- private:
- int* arr;
- void quick_sort(int*, int, int);
- void heapify(int*, int, int);
- public:
- Sort(int*);
- double selection_sort(int);
- double quick_sort_count(int*, int, int);
- double heap_sort(int*, int);
- };
- //Sort.cpp
- #include "Sort.h"
- extern int compare1;
- extern int compare2;
- extern int compare3;
- extern int replace1;
- extern int replace2;
- extern int replace3;
- void Sort::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);
- }
- Sort::Sort(int* arr2)
- {
- arr = arr2;
- }
- double Sort::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 Sort::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);
- }
- void Sort::heapify(int* arr, int n, int i)
- {
- int largest = i; // Инициализируем наибольший элемент как корень
- int left = 2 * i + 1; // Левый потомок
- int right = 2 * i + 2; // Правый потомок
- compare3++;
- if (left < n && arr[left] > arr[largest]) // Если левый потомок больше корня
- largest = left;
- compare3++;
- if (right < n && arr[right] > arr[largest]) // Если правый потомок больше корня
- largest = right;
- if (largest != i) // Если самый большой элемент оказался не корень
- {
- swap(arr[i], arr[largest]);
- replace3++;
- heapify(arr, n, largest); // Проверяем, удовлетворяет ли поддерево свойству кучи
- }
- }
- double Sort::heap_sort(int* arr, int n)
- {
- double start = clock();
- for (int i = n / 2 - 1; i >= 0; i--) // Построение кучи (перегруппировка массива)
- heapify(arr, n, i);
- for (int i = n - 1; i >= 0; i--) // Извлекаем элементы из кучи по одному и устанавливаем их в конец
- {
- swap(arr[0], arr[i]);
- replace3++;
- heapify(arr, i, 0); // Вызываем функцию heapify на уменьшенной куче
- }
- double stop = clock();
- return ((stop - start) / CLK_TCK);
- }
Add Comment
Please, Sign In to add comment