生成代码工程
This commit is contained in:
parent
4aafd25acb
commit
8109e88586
|
|
@ -0,0 +1,29 @@
|
|||
cmake_minimum_required(VERSION 3.14)
|
||||
project(SortAlgorithms VERSION 1.0.0 LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
# ── MSVC 编码兼容 ──────────────────────────────────────────────
|
||||
if (MSVC)
|
||||
add_compile_options(/utf-8)
|
||||
endif()
|
||||
|
||||
# ── 头文件路径 ────────────────────────────────────────────────
|
||||
set(PROJECT_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include")
|
||||
target_include_directories(${PROJECT_NAME} PRIVATE ${PROJECT_INCLUDE_DIR})
|
||||
|
||||
# ── 主程序 ────────────────────────────────────────────────────
|
||||
add_executable(${PROJECT_NAME}
|
||||
src/main.cpp
|
||||
src/app.cpp
|
||||
)
|
||||
target_include_directories(${PROJECT_NAME} PRIVATE ${PROJECT_INCLUDE_DIR})
|
||||
|
||||
# ── 测试可执行文件 ────────────────────────────────────────────
|
||||
add_executable(${PROJECT_NAME}_test
|
||||
tests/basic_test.cpp
|
||||
src/app.cpp
|
||||
)
|
||||
target_include_directories(${PROJECT_NAME}_test PRIVATE ${PROJECT_INCLUDE_DIR})
|
||||
72
README.md
72
README.md
|
|
@ -1,3 +1,71 @@
|
|||
# lds-test
|
||||
# SortAlgorithms — 三种经典排序算法(C++17)
|
||||
|
||||
从0到1创建项目
|
||||
## 功能
|
||||
|
||||
使用纯 C++ 实现的三种排序算法,对整型数组进行**升序**排列:
|
||||
|
||||
1. **冒泡排序** (`bubbleSort`) — 相邻元素比较交换,稳定,O(n²)
|
||||
2. **快速排序** (`quickSort`) — 分治递归,不稳定,平均 O(n log n)
|
||||
3. **选择排序** (`selectionSort`) — 每轮选最小交换,不稳定,O(n²)
|
||||
|
||||
## 工程结构
|
||||
|
||||
```
|
||||
SortAlgorithms/
|
||||
├── CMakeLists.txt
|
||||
├── README.md
|
||||
├── include/
|
||||
│ └── app.hpp # 公开函数声明(Doxygen 注释)
|
||||
├── src/
|
||||
│ ├── app.cpp # 排序算法实现
|
||||
│ └── main.cpp # 命令行演示入口
|
||||
└── tests/
|
||||
└── basic_test.cpp # 基于 assert 的单元测试
|
||||
```
|
||||
|
||||
## 编译与运行
|
||||
|
||||
### 方式一:直接构建
|
||||
|
||||
```bash
|
||||
mkdir build && cd build
|
||||
cmake ..
|
||||
cmake --build .
|
||||
./SortAlgorithms # 运行演示
|
||||
./SortAlgorithms_test # 运行测试
|
||||
```
|
||||
|
||||
### 方式二:使用 CMake 预设(如有)
|
||||
|
||||
```bash
|
||||
cmake --preset default
|
||||
cmake --build build
|
||||
```
|
||||
|
||||
## 接口说明
|
||||
|
||||
所有函数均定义在命名空间 `sort` 中,见 `include/app.hpp`。
|
||||
|
||||
```cpp
|
||||
namespace sort {
|
||||
|
||||
/// @brief 冒泡排序(升序)
|
||||
void bubbleSort(int arr[], int n);
|
||||
|
||||
/// @brief 快速排序(递归入口)
|
||||
void quickSort(int arr[], int n);
|
||||
|
||||
/// @brief 快速排序递归核心
|
||||
void quickSort(int arr[], int low, int high);
|
||||
|
||||
/// @brief 选择排序(升序)
|
||||
void selectionSort(int arr[], int n);
|
||||
|
||||
} // namespace sort
|
||||
```
|
||||
|
||||
## 约束
|
||||
|
||||
- C++17 标准
|
||||
- 不使用 `std::sort` 等标准库排序函数
|
||||
- 支持空数组和单元素数组
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -0,0 +1,30 @@
|
|||
{
|
||||
"projectId": 55,
|
||||
"generationId": "codegen_ad3ea22cd7cf49deb1370f2f050ef554",
|
||||
"language": "C++",
|
||||
"status": "completed",
|
||||
"fileIds": [
|
||||
877
|
||||
],
|
||||
"outputDir": "/app/agents/ai_agents/project-files/codegen-runs/codegen_ad3ea22cd7cf49deb1370f2f050ef554",
|
||||
"relativeOutputDir": "codegen-runs/codegen_ad3ea22cd7cf49deb1370f2f050ef554",
|
||||
"generatedFiles": [
|
||||
"CMakeLists.txt",
|
||||
"README.md",
|
||||
"events.ndjson",
|
||||
"include/app.hpp",
|
||||
"src/app.cpp",
|
||||
"src/main.cpp",
|
||||
"tests/basic_test.cpp"
|
||||
],
|
||||
"analysisSummary": "{\n \"业务目标\": \"实现三种经典排序算法(冒泡排序、快速排序、选择排序),能够对整型数组进行升序排列。\",\n \"功能清单\": [\n \"冒泡排序函数:通过相邻元素比较和交换,将最大元素逐步‘冒泡’至数组末尾。\",\n \"快速排序函数:采用分治策略,选取基准元素将数组划分为左右两部分并递归排序。\",\n \"选择排序函数:每轮从未排序部分选出最小元素,与未排序首位交换位置。\"\n ],\n \"数据结构\": [\n \"int数组:用于存储待排序的整数序列。\",\n \"函数参数包含数组指针和数组长度(或使用std::vector<int>)\"\n ],\n \"接口或命令\": [\n \"void bubbleSort(int arr[], int n);\",\n \"void quickSort(int arr[], int low, int high);\",\n \"void selectionSort(int arr[], int n);\"\n ],\n \"约束\": [\n \"使用C++语言实现。\",\n \"输入为整型数组,支持空数组或单元素数组。\",\n \"排序结果为非降序(升序)。\",\n \"不允许使用标准库中的排序函数(如std::sort)。\"\n ],\n \"测试建议\": [\n \"测试空数组和单元素数组的边界情况。\",\n \"测试已有序数组、逆序数组、含重复元素数组。\",\n \"验证排序后数组是否为升序且元素完整。\",\n \"可添加断言或打印数组前后状态辅助验证。\"\n ]\n}",
|
||||
"eventLogFile": "/app/agents/ai_agents/project-files/codegen-runs/codegen_ad3ea22cd7cf49deb1370f2f050ef554/events.ndjson",
|
||||
"repoSettings": {
|
||||
"username": "root",
|
||||
"password": "pAssW0rd",
|
||||
"repoUrl": "http://47.108.255.216:3000/root/sortDemo.git",
|
||||
"branch": "main"
|
||||
},
|
||||
"repoUrl": "http://47.108.255.216:3000/root/sortDemo.git",
|
||||
"branch": "main"
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
#ifndef SORT_ALGORITHMS_APP_HPP
|
||||
#define SORT_ALGORITHMS_APP_HPP
|
||||
|
||||
/**
|
||||
* @file app.hpp
|
||||
* @brief 三种经典排序算法的函数声明
|
||||
*
|
||||
* 所有排序函数均对整型数组进行升序(非降序)排列。
|
||||
* 不允许使用标准库中的 std::sort。
|
||||
*/
|
||||
|
||||
namespace sort {
|
||||
|
||||
/**
|
||||
* @brief 冒泡排序 (Bubble Sort)
|
||||
*
|
||||
* 通过相邻元素比较和交换,将最大元素逐步“冒泡”至数组末尾。
|
||||
* 平均 / 最坏时间复杂度 O(n²),最好 O(n)(已有序时借助优化标记)。
|
||||
*
|
||||
* @param arr 待排序的整型数组
|
||||
* @param n 数组元素个数
|
||||
*/
|
||||
void bubbleSort(int arr[], int n);
|
||||
|
||||
/**
|
||||
* @brief 快速排序 (Quick Sort) — 便捷入口
|
||||
*
|
||||
* 调用递归核心版本,默认对 arr[0 .. n-1] 排序。
|
||||
* 平均时间复杂度 O(n log n),最坏 O(n²)。
|
||||
*
|
||||
* @param arr 待排序的整型数组
|
||||
* @param n 数组元素个数
|
||||
*/
|
||||
void quickSort(int arr[], int n);
|
||||
|
||||
/**
|
||||
* @brief 快速排序递归核心
|
||||
*
|
||||
* 选取基准元素(此处固定取 arr[low]),将数组划分为左右两部分,
|
||||
* 左半部分 ≤ 基准 ≤ 右半部分,然后递归排序左右子区间。
|
||||
*
|
||||
* @param arr 待排序数组
|
||||
* @param low 子区间起始下标
|
||||
* @param high 子区间结束下标
|
||||
*/
|
||||
void quickSort(int arr[], int low, int high);
|
||||
|
||||
/**
|
||||
* @brief 选择排序 (Selection Sort)
|
||||
*
|
||||
* 每轮从未排序部分选出最小元素,与未排序部分的首位交换位置。
|
||||
* 时间复杂度始终 O(n²),不稳定。
|
||||
*
|
||||
* @param arr 待排序的整型数组
|
||||
* @param n 数组元素个数
|
||||
*/
|
||||
void selectionSort(int arr[], int n);
|
||||
|
||||
} // namespace sort
|
||||
|
||||
#endif // SORT_ALGORITHMS_APP_HPP
|
||||
|
|
@ -0,0 +1,129 @@
|
|||
/**
|
||||
* @file app.cpp
|
||||
* @brief 三种经典排序算法的实现
|
||||
*/
|
||||
|
||||
#include "app.hpp"
|
||||
#include <utility> // std::swap
|
||||
|
||||
namespace sort {
|
||||
|
||||
// ============================================================================
|
||||
// 冒泡排序
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief 冒泡排序实现
|
||||
*
|
||||
* 外层循环控制已排序尾部范围,内层循环进行相邻比较并交换。
|
||||
* 引入 swapped 标记:若某一轮无交换,说明数组已有序,提前终止。
|
||||
*/
|
||||
void bubbleSort(int arr[], int n) {
|
||||
if (n <= 1) return;
|
||||
|
||||
for (int i = 0; i < n - 1; ++i) {
|
||||
bool swapped = false;
|
||||
|
||||
for (int j = 0; j < n - 1 - i; ++j) {
|
||||
if (arr[j] > arr[j + 1]) {
|
||||
std::swap(arr[j], arr[j + 1]);
|
||||
swapped = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!swapped) {
|
||||
break; // 已有序,提前退出
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 快速排序
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief 快速排序的分区函数
|
||||
*
|
||||
* 以 arr[low] 为基准,将数组分为左小右大两部分,
|
||||
* 返回基准元素最终位置。
|
||||
*
|
||||
* @param arr 待分区数组
|
||||
* @param low 左边界
|
||||
* @param high 右边界
|
||||
* @return int 基准元素最终下标
|
||||
*/
|
||||
static int partition(int arr[], int low, int high) {
|
||||
int pivot = arr[low]; // 选取第一个元素为基准
|
||||
int i = low;
|
||||
int j = high;
|
||||
|
||||
while (i < j) {
|
||||
// 从右向左找第一个小于基准的元素
|
||||
while (i < j && arr[j] >= pivot) {
|
||||
--j;
|
||||
}
|
||||
if (i < j) {
|
||||
arr[i] = arr[j];
|
||||
}
|
||||
|
||||
// 从左向右找第一个大于基准的元素
|
||||
while (i < j && arr[i] <= pivot) {
|
||||
++i;
|
||||
}
|
||||
if (i < j) {
|
||||
arr[j] = arr[i];
|
||||
}
|
||||
}
|
||||
|
||||
arr[i] = pivot;
|
||||
return i;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 快速排序递归核心
|
||||
*/
|
||||
void quickSort(int arr[], int low, int high) {
|
||||
if (low < high) {
|
||||
int pivotIndex = partition(arr, low, high);
|
||||
quickSort(arr, low, pivotIndex - 1);
|
||||
quickSort(arr, pivotIndex + 1, high);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 快速排序便捷入口
|
||||
*/
|
||||
void quickSort(int arr[], int n) {
|
||||
if (n <= 1) return;
|
||||
quickSort(arr, 0, n - 1);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 选择排序
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief 选择排序实现
|
||||
*
|
||||
* 每轮从 [i, n-1] 中选出最小值的下标 minIdx,
|
||||
* 然后将 arr[i] 与 arr[minIdx] 交换。
|
||||
*/
|
||||
void selectionSort(int arr[], int n) {
|
||||
if (n <= 1) return;
|
||||
|
||||
for (int i = 0; i < n - 1; ++i) {
|
||||
int minIdx = i;
|
||||
|
||||
for (int j = i + 1; j < n; ++j) {
|
||||
if (arr[j] < arr[minIdx]) {
|
||||
minIdx = j;
|
||||
}
|
||||
}
|
||||
|
||||
if (minIdx != i) {
|
||||
std::swap(arr[i], arr[minIdx]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sort
|
||||
|
|
@ -0,0 +1,98 @@
|
|||
/**
|
||||
* @file main.cpp
|
||||
* @brief 命令行演示入口 —— 展示三种排序算法的效果
|
||||
*/
|
||||
|
||||
#include "app.hpp"
|
||||
#include <iostream>
|
||||
#include <iterator> // std::size (C++17)
|
||||
|
||||
/**
|
||||
* @brief 打印数组元素到标准输出
|
||||
* @tparam N 数组长度(编译期常量)
|
||||
* @param arr 整型数组
|
||||
*/
|
||||
template <std::size_t N>
|
||||
static void printArray(const int (&arr)[N]) {
|
||||
std::cout << "[";
|
||||
for (std::size_t i = 0; i < N; ++i) {
|
||||
std::cout << arr[i];
|
||||
if (i + 1 < N) std::cout << ", ";
|
||||
}
|
||||
std::cout << "]" << std::endl;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief 演示冒泡排序
|
||||
*/
|
||||
static void demoBubbleSort() {
|
||||
int arr[] = {64, 34, 25, 12, 22, 11, 90};
|
||||
constexpr int n = static_cast<int>(std::size(arr));
|
||||
|
||||
std::cout << "===== 冒泡排序 (Bubble Sort) =====" << std::endl;
|
||||
std::cout << "排序前: ";
|
||||
printArray(arr);
|
||||
|
||||
sort::bubbleSort(arr, n);
|
||||
|
||||
std::cout << "排序后: ";
|
||||
printArray(arr);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief 演示快速排序
|
||||
*/
|
||||
static void demoQuickSort() {
|
||||
int arr[] = {38, 27, 43, 3, 9, 82, 10};
|
||||
constexpr int n = static_cast<int>(std::size(arr));
|
||||
|
||||
std::cout << "===== 快速排序 (Quick Sort) =====" << std::endl;
|
||||
std::cout << "排序前: ";
|
||||
printArray(arr);
|
||||
|
||||
sort::quickSort(arr, n);
|
||||
|
||||
std::cout << "排序后: ";
|
||||
printArray(arr);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief 演示选择排序
|
||||
*/
|
||||
static void demoSelectionSort() {
|
||||
int arr[] = {29, 10, 14, 37, 13, 33, 48};
|
||||
constexpr int n = static_cast<int>(std::size(arr));
|
||||
|
||||
std::cout << "===== 选择排序 (Selection Sort) =====" << std::endl;
|
||||
std::cout << "排序前: ";
|
||||
printArray(arr);
|
||||
|
||||
sort::selectionSort(arr, n);
|
||||
|
||||
std::cout << "排序后: ";
|
||||
printArray(arr);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief 主函数
|
||||
* @return 0 正常退出
|
||||
*/
|
||||
int main() {
|
||||
demoBubbleSort();
|
||||
demoQuickSort();
|
||||
demoSelectionSort();
|
||||
|
||||
std::cout << "所有排序演示完成。" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,182 @@
|
|||
/**
|
||||
* @file basic_test.cpp
|
||||
* @brief 基于标准库 assert 的排序算法单元测试
|
||||
*/
|
||||
|
||||
#include "app.hpp"
|
||||
#include <cassert>
|
||||
#include <cstring> // std::memcpy
|
||||
#include <iostream>
|
||||
#include <algorithm> // std::is_sorted (仅用于验证,不用于排序)
|
||||
|
||||
// ============================================================================
|
||||
// 辅助函数
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief 验证数组已按升序排列
|
||||
* @param arr 数组指针
|
||||
* @param n 数组长度
|
||||
* @return true 升序;false 非升序
|
||||
*/
|
||||
static bool isSorted(const int arr[], int n) {
|
||||
for (int i = 1; i < n; ++i) {
|
||||
if (arr[i - 1] > arr[i]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 复制数组(长度不超过 1024)
|
||||
*/
|
||||
static void copyArray(const int src[], int dst[], int n) {
|
||||
std::memcpy(dst, src, static_cast<std::size_t>(n) * sizeof(int));
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 测试用例
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief 测试空数组和单元素数组(边界情况)
|
||||
*/
|
||||
static void testEmptyAndSingle() {
|
||||
std::cout << "[TEST] 空数组 & 单元素数组 ... ";
|
||||
|
||||
// 空数组
|
||||
sort::bubbleSort(nullptr, 0);
|
||||
sort::quickSort(nullptr, 0);
|
||||
sort::selectionSort(nullptr, 0);
|
||||
|
||||
// 单元素
|
||||
int a1[] = {42};
|
||||
sort::bubbleSort(a1, 1);
|
||||
assert(a1[0] == 42);
|
||||
|
||||
int a2[] = {99};
|
||||
sort::quickSort(a2, 1);
|
||||
assert(a2[0] == 99);
|
||||
|
||||
int a3[] = {7};
|
||||
sort::selectionSort(a3, 1);
|
||||
assert(a3[0] == 7);
|
||||
|
||||
std::cout << "通过" << std::endl;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief 测试冒泡排序
|
||||
*/
|
||||
static void testBubbleSort() {
|
||||
std::cout << "[TEST] 冒泡排序 ... ";
|
||||
|
||||
// 普通数组
|
||||
int data[] = {64, 34, 25, 12, 22, 11, 90};
|
||||
int arr[7];
|
||||
copyArray(data, arr, 7);
|
||||
sort::bubbleSort(arr, 7);
|
||||
assert(isSorted(arr, 7));
|
||||
assert(arr[0] == 11);
|
||||
assert(arr[6] == 90);
|
||||
|
||||
// 已有序
|
||||
int sorted[] = {1, 2, 3, 4, 5};
|
||||
sort::bubbleSort(sorted, 5);
|
||||
assert(isSorted(sorted, 5));
|
||||
|
||||
// 逆序
|
||||
int reversed[] = {5, 4, 3, 2, 1};
|
||||
sort::bubbleSort(reversed, 5);
|
||||
assert(isSorted(reversed, 5));
|
||||
|
||||
// 含重复元素
|
||||
int duplicates[] = {3, 1, 2, 3, 1, 2};
|
||||
sort::bubbleSort(duplicates, 6);
|
||||
assert(isSorted(duplicates, 6));
|
||||
|
||||
std::cout << "通过" << std::endl;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief 测试快速排序
|
||||
*/
|
||||
static void testQuickSort() {
|
||||
std::cout << "[TEST] 快速排序 ... ";
|
||||
|
||||
int data[] = {38, 27, 43, 3, 9, 82, 10};
|
||||
int arr[7];
|
||||
copyArray(data, arr, 7);
|
||||
sort::quickSort(arr, 7);
|
||||
assert(isSorted(arr, 7));
|
||||
|
||||
// 已有序
|
||||
int sorted[] = {1, 2, 3, 4, 5};
|
||||
sort::quickSort(sorted, 5);
|
||||
assert(isSorted(sorted, 5));
|
||||
|
||||
// 逆序
|
||||
int reversed[] = {5, 4, 3, 2, 1};
|
||||
sort::quickSort(reversed, 5);
|
||||
assert(isSorted(reversed, 5));
|
||||
|
||||
// 含重复元素
|
||||
int duplicates[] = {3, 1, 2, 3, 1, 2};
|
||||
sort::quickSort(duplicates, 6);
|
||||
assert(isSorted(duplicates, 6));
|
||||
|
||||
std::cout << "通过" << std::endl;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief 测试选择排序
|
||||
*/
|
||||
static void testSelectionSort() {
|
||||
std::cout << "[TEST] 选择排序 ... ";
|
||||
|
||||
int data[] = {29, 10, 14, 37, 13, 33, 48};
|
||||
int arr[7];
|
||||
copyArray(data, arr, 7);
|
||||
sort::selectionSort(arr, 7);
|
||||
assert(isSorted(arr, 7));
|
||||
|
||||
// 已有序
|
||||
int sorted[] = {1, 2, 3, 4, 5};
|
||||
sort::selectionSort(sorted, 5);
|
||||
assert(isSorted(sorted, 5));
|
||||
|
||||
// 逆序
|
||||
int reversed[] = {5, 4, 3, 2, 1};
|
||||
sort::selectionSort(reversed, 5);
|
||||
assert(isSorted(reversed, 5));
|
||||
|
||||
// 含重复元素
|
||||
int duplicates[] = {3, 1, 2, 3, 1, 2};
|
||||
sort::selectionSort(duplicates, 6);
|
||||
assert(isSorted(duplicates, 6));
|
||||
|
||||
std::cout << "通过" << std::endl;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 主测试入口
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief 测试主函数
|
||||
* @return 0 全部通过;非 0 表示有测试失败
|
||||
*/
|
||||
int main() {
|
||||
testEmptyAndSingle();
|
||||
testBubbleSort();
|
||||
testQuickSort();
|
||||
testSelectionSort();
|
||||
|
||||
std::cout << "\n所有测试通过!" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue