diff --git a/CMakeLists.txt b/CMakeLists.txt index eaa3cf7..eb3e90c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,4 +7,6 @@ add_executable(data_structure_homework main.cpp problem1.cpp problem1.h problem2.cpp - problem2.h) + problem2.h + problem3.cpp + problem3.h) diff --git a/main.cpp b/main.cpp index 460608d..2cde612 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,14 @@ +#include +#include + #include "problem1.h" #include "problem2.h" +#include "problem3.h" int main() { problem_1(); + std::cout << "------------------------" << std::endl; problem_2(); + std::cout << "------------------------" << std::endl; + problem_3(); } diff --git a/problem3.cpp b/problem3.cpp new file mode 100644 index 0000000..85d9c3e --- /dev/null +++ b/problem3.cpp @@ -0,0 +1,94 @@ +// +// Created by grtsinry43 on 5/8/25. +// + +#include "problem3.h" + +#include +#include +#include +#include + +int partition(std::vector &arr, int low, int high) { + int pivot = arr[high]; + int i = low - 1; + for (int j = low; j < high; j++) { + if (arr[j] <= pivot) { + i++; + std::swap(arr[i], arr[j]); + } + } + std::swap(arr[i + 1], arr[high]); + return i + 1; +} + +void quick_sort(std::vector &arr, int low, int high) { + if (low < high) { + int pivot = partition(arr, low, high); + quick_sort(arr, low, pivot - 1); + quick_sort(arr, pivot + 1, high); + } +} + +void select_sort(std::vector &arr) { + for (size_t i = 0; i < arr.size() - 1; i++) { + size_t min_index = i; + for (size_t j = i + 1; j < arr.size(); j++) { + if (arr[j] < arr[min_index]) { + min_index = j; + } + } + std::swap(arr[i], arr[min_index]); + } +} + +void insert_sort(std::vector &arr) { + for (int i = 0; i < arr.size(); ++i) { + int key = arr[i]; + int j = i - 1; + while (j >= 0 && arr[j] > key) { + arr[j + 1] = arr[j]; + j--; + } + arr[j + 1] = key; + } +} + +std::vector generate_random_array(int size) { + std::vector arr(size); + for (int i = 0; i < size; ++i) { + arr[i] = random() % 10000; // 范围给一个0-9999吧 + } + return arr; +} + +int problem_3() { + int size = 10000; + std::vector arr = generate_random_array(size); + std::vector arr_copy = arr; + std::vector arr_copy2 = arr; + // 快速排序 + clock_t start, end; + start = clock(); + quick_sort(arr, 0, arr.size() - 1); + end = clock(); + double quick_sort_time = double(end - start) / CLOCKS_PER_SEC; + std::cout << "Quick Sort Time: " << quick_sort_time << " seconds\n"; + + // 选择排序 + start = clock(); + select_sort(arr_copy); + end = clock(); + double select_sort_time = double(end - start) / CLOCKS_PER_SEC; + std::cout << "Select Sort Time: " << select_sort_time << " seconds\n"; + + // 插入排序 + start = clock(); + insert_sort(arr_copy2); + end = clock(); + double insert_sort_time = double(end - start) / CLOCKS_PER_SEC; + std::cout << "Insert Sort Time: " << insert_sort_time << " seconds\n"; + + + return 0; +} diff --git a/problem3.h b/problem3.h new file mode 100644 index 0000000..4094360 --- /dev/null +++ b/problem3.h @@ -0,0 +1,10 @@ +// +// Created by grtsinry43 on 5/8/25. +// + +#ifndef PROBLEM3_H +#define PROBLEM3_H + +int problem_3(); + +#endif //PROBLEM3_H