Add problem3 implementation with sorting algorithms and update CMakeLists
This commit is contained in:
parent
7368807c68
commit
f01e47c076
@ -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)
|
||||
|
||||
7
main.cpp
7
main.cpp
@ -1,7 +1,14 @@
|
||||
#include <iostream>
|
||||
#include <bits/ostream.tcc>
|
||||
|
||||
#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();
|
||||
}
|
||||
|
||||
94
problem3.cpp
Normal file
94
problem3.cpp
Normal file
@ -0,0 +1,94 @@
|
||||
//
|
||||
// Created by grtsinry43 on 5/8/25.
|
||||
//
|
||||
|
||||
#include "problem3.h"
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <ctime>
|
||||
|
||||
int partition(std::vector<int> &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<int> &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<int> &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<int> &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<int> generate_random_array(int size) {
|
||||
std::vector<int> 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<int> arr = generate_random_array(size);
|
||||
std::vector<int> arr_copy = arr;
|
||||
std::vector<int> 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;
|
||||
}
|
||||
10
problem3.h
Normal file
10
problem3.h
Normal file
@ -0,0 +1,10 @@
|
||||
//
|
||||
// Created by grtsinry43 on 5/8/25.
|
||||
//
|
||||
|
||||
#ifndef PROBLEM3_H
|
||||
#define PROBLEM3_H
|
||||
|
||||
int problem_3();
|
||||
|
||||
#endif //PROBLEM3_H
|
||||
Loading…
x
Reference in New Issue
Block a user