Task 7
This commit is contained in:
13
myalgorithms.h
Normal file
13
myalgorithms.h
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#ifndef MYALGORITHMS_H
|
||||||
|
#define MYALGORITHMS_H
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void bubbleSort(std::vector<int>& 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<int>& vec, int low, int high);
|
||||||
|
|
||||||
|
#endif
|
||||||
20
task1.cpp
20
task1.cpp
@@ -1,3 +1,4 @@
|
|||||||
|
#include "myalgorithms.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -25,22 +26,3 @@ void bubbleSort(std::vector<int>& v)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
std::vector<int> v = {12, 15, 13, 4, 8};
|
|
||||||
|
|
||||||
// Это изменит исходную переменную
|
|
||||||
bubbleSort(v);
|
|
||||||
|
|
||||||
// Для вывода перебираем элементы вектора
|
|
||||||
// (i - это уже значение, а не индекс)
|
|
||||||
for (int i : v)
|
|
||||||
{
|
|
||||||
std::cout << i << " ";
|
|
||||||
}
|
|
||||||
std::cout << std::endl;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
15
task2.cpp
15
task2.cpp
@@ -1,3 +1,4 @@
|
|||||||
|
#include "myalgorithms.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void CocktailSort(int a[], int n)
|
void CocktailSort(int a[], int n)
|
||||||
@@ -55,17 +56,3 @@ void CocktailSort(int a[], int n)
|
|||||||
start++;
|
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;
|
|
||||||
}
|
|
||||||
|
|||||||
15
task3.cpp
15
task3.cpp
@@ -1,3 +1,4 @@
|
|||||||
|
#include "myalgorithms.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void insertionSort(int arr[], int n)
|
void insertionSort(int arr[], int n)
|
||||||
@@ -27,17 +28,3 @@ void insertionSort(int arr[], int n)
|
|||||||
arr[j + 1] = key;
|
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;
|
|
||||||
}
|
|
||||||
|
|||||||
15
task4.cpp
15
task4.cpp
@@ -1,3 +1,4 @@
|
|||||||
|
#include "myalgorithms.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void selectionSort(int arr[], int n)
|
void selectionSort(int arr[], int n)
|
||||||
@@ -27,17 +28,3 @@ void selectionSort(int arr[], int n)
|
|||||||
arr[min_idx] = temp;
|
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;
|
|
||||||
}
|
|
||||||
|
|||||||
17
task5.cpp
17
task5.cpp
@@ -1,6 +1,6 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
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;
|
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;
|
|
||||||
}
|
}
|
||||||
|
|||||||
15
task6.cpp
15
task6.cpp
@@ -1,3 +1,4 @@
|
|||||||
|
#include "myalgorithms.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -42,17 +43,3 @@ void quickSort(std::vector<int> &vec, int low, int high)
|
|||||||
quickSort(vec, pi + 1, high);
|
quickSort(vec, pi + 1, high);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
std::vector<int> 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;
|
|
||||||
}
|
|
||||||
|
|||||||
69
task7.cpp
Normal file
69
task7.cpp
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
#include "myalgorithms.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <chrono> // Для секундомера
|
||||||
|
#include <algorithm> // Для копирования массивов
|
||||||
|
|
||||||
|
// Короткий и длинный 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<int> vec_short = {5, 7, 1, 2, 15, 29, 100, 1000};
|
||||||
|
std::vector<int> 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<std::chrono::nanoseconds>(end - start).count();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Функция измерения скорости алгоритма (используется для векторов)
|
||||||
|
double benchmark_vector(void (*func)(std::vector<int>&), std::vector<int>& vec)
|
||||||
|
{
|
||||||
|
std::vector<int> 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<std::chrono::nanoseconds>(end - start).count();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Функция измерения скорости алгоритма (используется для QuickSort)
|
||||||
|
double benchmark_vector(void (*func)(std::vector<int>&, int, int), std::vector<int>& vec)
|
||||||
|
{
|
||||||
|
std::vector<int> 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<std::chrono::nanoseconds>(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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user