生成代码工程

This commit is contained in:
root 2026-06-02 01:53:58 +00:00
parent 4aafd25acb
commit 8109e88586
8 changed files with 5548 additions and 2 deletions

29
CMakeLists.txt Normal file
View File

@ -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})

View File

@ -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` 等标准库排序函数
- 支持空数组和单元素数组

4949
events.ndjson Normal file

File diff suppressed because one or more lines are too long

30
generation.json Normal file
View File

@ -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"
}

61
include/app.hpp Normal file
View File

@ -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

129
src/app.cpp Normal file
View File

@ -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

98
src/main.cpp Normal file
View File

@ -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;
}

182
tests/basic_test.cpp Normal file
View File

@ -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;
}