From 7638106de6b259f03f3b9e50b6af921d7cd3c8eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=AF=E6=BA=90?= <15826362+feng-yuan0104@user.noreply.gitee.com> Date: Mon, 23 Jun 2025 14:42:38 +0000 Subject: [PATCH] =?UTF-8?q?update=20AstroLib/Src/AsMath.cpp.=202025?= =?UTF-8?q?=E6=98=A5=E5=AD=A3=E4=B8=93=E9=A1=B9=E7=8F=AD=EF=BC=8C=E8=88=AA?= =?UTF-8?q?=E5=A4=A9=E7=B3=BB=E7=BB=9F=E4=BB=BF=E7=9C=9F=E4=B8=8E=E6=8E=A8?= =?UTF-8?q?=E6=BC=94=E8=AF=BE=E7=A8=8B=EF=BC=9A=E5=86=AF=E6=BA=90=EF=BC=8C?= =?UTF-8?q?=E7=8E=8B=E6=97=AD=E3=80=82=E6=8F=90=E4=BA=A4=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E7=AE=97=E6=B3=95=E8=87=B3AsMath.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 冯源 <15826362+feng-yuan0104@user.noreply.gitee.com> --- AstroLib/Src/AsMath.cpp | 146 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 145 insertions(+), 1 deletion(-) diff --git a/AstroLib/Src/AsMath.cpp b/AstroLib/Src/AsMath.cpp index 4810c54..036af96 100644 --- a/AstroLib/Src/AsMath.cpp +++ b/AstroLib/Src/AsMath.cpp @@ -1,4 +1,4 @@ -//////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////// // // // Copyright (c) 2003-2006 // // Wang Hua // @@ -14,10 +14,154 @@ #include "AsVector.h" #include +///*********************************************************************** + /// 适用于浮点数排序 + /// @Author Wang Xu + /// @Date 2025.06.18 + ///*********************************************************************** +using namespace std; + + +#include using namespace std; +// 冒泡排序函数 - 适用于浮点数 +void bubbleSort(double arr[], int n) { + for (int i = 0; i < n - 1; i++) { + bool swapped = false; + + for (int j = 0; j < n - i - 1; j++) { + if (arr[j] > arr[j + 1]) { + swap(arr[j], arr[j + 1]); + swapped = true; + } + } + + if (!swapped) break; + } +} + +// 打印数组函数 - 适用于浮点数 +void printArray(double arr[], int size) { + for (int i = 0; i < size; i++) { + cout << arr[i] << " "; + } + cout << endl; +} + +int main() { + // 修改为浮点数数组 + double arr[] = { 64.2, 34.5, 25.7, 12.9, 22.1, 11.3, 90.4 }; + int n = sizeof(arr) / sizeof(arr[0]); + + cout << "排序前数组: "; + printArray(arr, n); + + bubbleSort(arr, n); + + cout << "排序后数组: "; + printArray(arr, n); + + return 0; +} + + + + +///*********************************************************************** + /// 适用于浮点数排序,并统计排序一万个数需要的时间 + /// @Author Feng Yuan + /// @Date 2025.06.18 + ///*********************************************************************** + +#include +#include +#include +#include +#include // 用于高精度时间测量 +#include // 用于 max 函数 +using namespace std; + +// 交换两个元素 +void swap(double& a, double& b) { +int temp = a; +a = b; +b = temp; +} + +// 分区函数,选择最后一个元素作为基准 base +int partition(vector& arr, int low, int high) { +int base = arr[high]; // 选择最后一个元素作为基准 +int i = (low - 1); // 小于基准的元素的索引 i,j 为循环指针 +for (int j = low; j <= high - 1; j++) { +// 如果当前元素小于或等于基准 +if (arr[j] <= base) { +i++; // 增加小于基准的元素的索引 +swap(arr[i], arr[j]); // 将当前元素与索引 i 处的元素交换(小于基准的数放至左区) +} +} +swap(arr[i + 1], arr[high]); // 将基准放到最终正确的位置 +return (i + 1); // 返回基准的索引 +} + +// 快速排序函数,用于递归 +void quickSort(vector& arr, int low, int high) { +if (low < high) { +// 分区操作,获取基准的索引 +int pi = partition(arr, low, high);//pi 接收分区函数的返回值(原基准,现分割值) +// 递归排序基准左边的子数组 +quickSort(arr, low, pi - 1); +// 递归排序基准右边的子数组 +quickSort(arr, pi + 1, high); +} +} + +// 生成随机数组的函数 +vector generateRandomArray(int size, double min, double max) { +vector arr(size); +srand(time(0)); // 设置随机数种子 +for (int i = 0; i < size; i++) { +arr[i] = min + ((double)rand() / RAND_MAX) * (max - min); // 生成指定范围内(min,max)的随机浮点数 +} +return arr;//生成的值返回值数组 arr +} + +int main() { +// 生成一万个随机数(1.0 到 10000.0 之间) +int size = 10000; +vector arr = generateRandomArray(size, 1.0, 10000.0); + +// 使用高精度时钟记录排序开始时间 +auto startTime = chrono::high_resolution_clock::now(); + +// 执行快速排序 +quickSort(arr, 0, size - 1); + +// 记录排序结束时间 +auto endTime = chrono::high_resolution_clock::now(); + +// 计算排序所用时间(以毫秒为单位) +auto duration = chrono::duration_cast(endTime - startTime).count(); +// 输出更精确的时间信息 +cout << "本次排序了 " << size << " 个随机数,所用时间为: " << duration << " 毫秒" << endl; +cout << "相当于 " << duration / 1000.0 << " 秒" << endl; +// 交互页面:打印排序后的数组的后 50 个数 +cout << "\n 为节省时间,只显示排序后的数组后 50 个元素:" << endl; +for (int i = max(0, size - 50); i < size; i++)//确保 i 大于等于 0 +{ +cout.precision(6); // 设置浮点数精度 +cout << fixed << arr[i]; +if (i < size - 1) cout << ", "; +if ((i - (size - 50) + 1) % 10 == 0) cout << endl; // 每 10 个数换行,清晰显示 +} +// 交互页面:等待用户输入 +cout << "\n 按回车键退出..." << endl; +cin.ignore(); // 忽略之前的输入缓冲区 +cin.get(); // 交互页面:等待用户按下回车键 +return 0; +} -- Gitee