diff --git a/myalgorithms.h b/myalgorithms.h new file mode 100644 index 0000000..d82d183 --- /dev/null +++ b/myalgorithms.h @@ -0,0 +1,13 @@ +#ifndef MYALGORITHMS_H +#define MYALGORITHMS_H + +#include + +void bubbleSort(std::vector& v); +void CocktailSort(int arr[], int n); +void insertionSort(int arr[], int n); +void selectionSort(int arr[], int n); +void shellSort(int arr[], int n); +void quickSort(std::vector& vec, int low, int high); + +#endif diff --git a/task1.cpp b/task1.cpp index 444a1a1..a542d49 100644 --- a/task1.cpp +++ b/task1.cpp @@ -1,3 +1,4 @@ +#include "myalgorithms.h" #include #include @@ -25,22 +26,3 @@ void bubbleSort(std::vector& v) } } } - -int main() -{ - std::vector v = {12, 15, 13, 4, 8}; - - // Это изменит исходную переменную - bubbleSort(v); - - // Для вывода перебираем элементы вектора - // (i - это уже значение, а не индекс) - for (int i : v) - { - std::cout << i << " "; - } - std::cout << std::endl; - - return 0; -} - diff --git a/task2.cpp b/task2.cpp index 8743e01..559c945 100644 --- a/task2.cpp +++ b/task2.cpp @@ -1,3 +1,4 @@ +#include "myalgorithms.h" #include void CocktailSort(int a[], int n) @@ -55,17 +56,3 @@ void CocktailSort(int a[], int n) start++; } } - -int main() -{ - int arr[] = {12, 11, 13, 5, 6}; - int n = sizeof(arr) / sizeof(arr[0]); - - CocktailSort(arr, n); - - for (int i = 0; i < n; i++) - { - std::cout << arr[i] << " "; - } - std::cout << std::endl; -} diff --git a/task3.cpp b/task3.cpp index 26ca298..4b97fd9 100644 --- a/task3.cpp +++ b/task3.cpp @@ -1,3 +1,4 @@ +#include "myalgorithms.h" #include void insertionSort(int arr[], int n) @@ -27,17 +28,3 @@ void insertionSort(int arr[], int n) arr[j + 1] = key; } } - -int main() -{ - int arr[] = {12, 11, 13, 5, 6}; - int n = sizeof(arr) / sizeof(arr[0]); - - insertionSort(arr, n); - - for (int i = 0; i < n; i++) - { - std::cout << arr[i] << " "; - } - std::cout << std::endl; -} diff --git a/task4.cpp b/task4.cpp index 18264e9..f62cf0e 100644 --- a/task4.cpp +++ b/task4.cpp @@ -1,3 +1,4 @@ +#include "myalgorithms.h" #include void selectionSort(int arr[], int n) @@ -27,17 +28,3 @@ void selectionSort(int arr[], int n) arr[min_idx] = temp; } } - -int main() { - int arr[] = {12, 11, 13, 5, 6}; - int n = sizeof(arr) / sizeof(arr[0]); - - selectionSort(arr, n); - - for (int i = 0; i < n; i++) { - std::cout << arr[i] << " "; - } - std::cout << std::endl; - - return 0; -} diff --git a/task5.cpp b/task5.cpp index 62ac55a..9deccbd 100644 --- a/task5.cpp +++ b/task5.cpp @@ -1,6 +1,6 @@ #include -int shellSort(int arr[], int n) +void shellSort(int arr[], int n) { // Сортировка Шелла @@ -26,19 +26,4 @@ int shellSort(int arr[], int n) arr[j] = temp; } } - return 0; -} - -int main() -{ - int arr[] = {12, 11, 13, 5, 6}; - int n = sizeof(arr) / sizeof(arr[0]); - - shellSort(arr, n); - - for (int i = 0; i < n; i++) - { - std::cout << arr[i] << " "; - } - std::cout << std::endl; } diff --git a/task6.cpp b/task6.cpp index fd1dc32..d97fc8b 100644 --- a/task6.cpp +++ b/task6.cpp @@ -1,3 +1,4 @@ +#include "myalgorithms.h" #include #include @@ -42,17 +43,3 @@ void quickSort(std::vector &vec, int low, int high) quickSort(vec, pi + 1, high); } } - -int main() -{ - std::vector vec = {12, 11, 13, 5, 6}; - quickSort(vec, 0, vec.size() - 1); - - for (int i : vec) - { - std::cout << i << " "; - } - std::cout << std::endl; - - return 0; -} diff --git a/task7.cpp b/task7.cpp new file mode 100644 index 0000000..e029079 --- /dev/null +++ b/task7.cpp @@ -0,0 +1,69 @@ +#include "myalgorithms.h" +#include +#include +#include // Для секундомера +#include // Для копирования массивов + +// Короткий и длинный C-массивы +int arr_short[] = {5, 7, 1, 2, 15, 29, 100, 1000}; +int arr_long[] = {165, 2, 23, 7, 9, 124, 4, 5000, 26, 20, 80, 55, 6, 1, 8, 10, 9, 12, 3, 6, 7, 5, 4, 4, 3, 2, 1, 100, 1000, 10000}; + +// Короткий и длинный векторы +std::vector vec_short = {5, 7, 1, 2, 15, 29, 100, 1000}; +std::vector vec_long = {165, 2, 23, 7, 9, 124, 4, 5000, 26, 20, 80, 55, 6, 1, 8, 10, 9, 12, 3, 6, 7, 5, 4, 4, 3, 2, 1, 100, 1000, 10000}; + +// Функция измерения скорости алгоритма (используется для C-массивов) +double benchmark(void (*func)(int[], int), int arr[], int n) +{ + // Поскольку все алгоритмы сортировки перезаписывают исходный массив, + // нам необходимо делать копию исходного массива и работать с ней. + // Обратите внимание, поскольку мы не пользуемся указателями и + // Память для копий массива уже заранее выделена при компиляции, + // нам не нужно очищать за собой память (удалять массивы). + // Обязательно удаляйте массивы, если используете указатели! + int arr_copy[n]; + std::copy(arr, arr + n, arr_copy); + + auto start = std::chrono::high_resolution_clock::now(); + func(arr_copy, n); + auto end = std::chrono::high_resolution_clock::now(); + + return std::chrono::duration_cast(end - start).count(); +} + +// Функция измерения скорости алгоритма (используется для векторов) +double benchmark_vector(void (*func)(std::vector&), std::vector& vec) +{ + std::vector vec_copy(vec); + std::copy(vec.begin(), vec.end(), vec_copy.begin()); + + auto start = std::chrono::high_resolution_clock::now(); + func(vec_copy); + auto end = std::chrono::high_resolution_clock::now(); + + return std::chrono::duration_cast(end - start).count(); +} + +// Функция измерения скорости алгоритма (используется для QuickSort) +double benchmark_vector(void (*func)(std::vector&, int, int), std::vector& vec) +{ + std::vector vec_copy(vec); + std::copy(vec.begin(), vec.end(), vec_copy.begin()); + + auto start = std::chrono::high_resolution_clock::now(); + func(vec_copy, 0, vec_copy.size() - 1); + auto end = std::chrono::high_resolution_clock::now(); + return std::chrono::duration_cast(end - start).count(); +} + +int main() +{ + std::cout << "Bubble sort: " << benchmark_vector(bubbleSort, vec_short) << ", " << benchmark_vector(bubbleSort, vec_long) << std::endl; + std::cout << "Cocktail sort: " << benchmark(CocktailSort, arr_short, sizeof(arr_short) / sizeof(arr_short[0])) << ", " << benchmark(CocktailSort, arr_long, sizeof(arr_long) / sizeof(arr_long[0])) << std::endl; + std::cout << "Insertion sort: " << benchmark(insertionSort, arr_short, sizeof(arr_short) / sizeof(arr_short[0])) << ", " << benchmark(insertionSort, arr_long, sizeof(arr_long) / sizeof(arr_long[0])) << std::endl; + std::cout << "Selection sort: " << benchmark(selectionSort, arr_short, sizeof(arr_short) / sizeof(arr_short[0])) << ", " << benchmark(selectionSort, arr_long, sizeof(arr_long) / sizeof(arr_long[0])) << std::endl; + std::cout << "Shell sort: " << benchmark(shellSort, arr_short, sizeof(arr_short) / sizeof(arr_short[0])) << ", " << benchmark(shellSort, arr_long, sizeof(arr_long) / sizeof(arr_long[0])) << std::endl; + std::cout << "Quick sort: " << benchmark_vector(quickSort, vec_short) << ", " << benchmark_vector(quickSort, vec_long) << std::endl; + + return 0; +}