之前指定的方式 是硬编码生成的
为了方便使用
我们可以在弄一个 新的.h文件
- #ifndef SORT_HELPER_H
- #define SORT_HELPER_H
- //解决ide.h文件的多重引用的问题
- #include
- #include
- #include
- #include
- using namespace std;
- namespace SortTestHelper{
- //生成n个元素的随机数组,每个元素的随机范围为【rangeL, rangeR】
- int* generateRandomArr(int n, int rangeL, int rangeR){
-
- assert( rangeL <= rangeR );
- int *arr = new int[n];
- //设置随机种子
- srand(time(NULL));
- for(int i = 0; i < n ;i ++)
- arr[i] = rand()%(rangeR - rangeL + 1) + rangeL;
-
- return arr;
-
- }
-
- template<typename T >
- void printarr(T arr[], int n){
- for( int i = 0 ; i < n ; i++)
- cout<
" "; - cout<
- return;
- }
- }
-
-
-
-
-
-
- #endif //SORT_HELPER_H
都是细节 不用多说 快上车
- #include
- #include
- #include "Student.h"
- #include "sorttesthelper.h"
- using namespace std;
-
- template<typename T >
-
- void selectionSort( T arr[], int n){
- for(int i = 0 ; i < n ; i++){
-
- //寻找【i,n之间的最小值】
- int minIndex = i;
- for( int j = i + 1 ; j < n ; j++)
- if(arr[j] < arr[minIndex] )
- minIndex = j;
-
- swap( arr[i] , arr[minIndex]);
-
- }
- }
-
-
- int main()
- {
- int a[5] = {5,62,3,58,44};
- selectionSort( a, 5 );
- for( int i = 0 ; i < 5 ; i++)
- cout<" ";
-
- cout<
-
- float b[4] = {4.4,2.3,5.63};
- selectionSort( b , 3);
- for( int i = 0 ; i < 3 ; i++)
- cout<" ";
- cout<
-
- string c[2] = {"z","b"};
- selectionSort( c , 2);
- for( int i = 0 ; i < 2 ; i++)
- cout<
" "; - cout<
-
- Student d[3] = {{"D",90} , {"C",89} , { "B", 114}};
- selectionSort( d , 3);
- for( int i = 0 ; i < 3 ; i++)
- cout<
- cout<
-
-
- int n = 1000;
- int *arr = SortTestHelper :: generateRandomArr(n, 0, n) ;
- selectionSort( arr, n );
- SortTestHelper :: printarr(arr, n);
-
- delete[] arr;
-
- return 0;
-
- }
-
-
肯定可以跑 不用谢

-
相关阅读:
云计算概念、技术与架构Thomas Erl-第8章上 特殊云机制[week7]
从 WinDbg 角度理解 .NET7 的AOT玩法
消息队列实现进程间通信
零代码编程:用ChatGPT批量转换多个视频文件夹到音频并自动移动文件夹
C语言入门Day_28 结语
15.Redis系列之使用Redisson分布式锁实现秒杀
HTML+CSS+JS网页设计期末课程大作业 html+css+javascript+jquery化妆品电商网站4页面
五.Redis_事务秒杀案例
Android Camera性能分析 - 第26讲 DequeueBuffer Latency
支持JDK19虚拟线程的web框架,之四:看源码,了解quarkus如何支持虚拟线程
-
原文地址:https://blog.csdn.net/qq_68308828/article/details/133785028