之前指定的方式 是硬编码生成的
为了方便使用
我们可以在弄一个 新的.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;
-
- }
-
-
肯定可以跑 不用谢

-
相关阅读:
简述JVM
收下这份Mock,极速与后端联调,提升效率不止亿点点
TQSDRPI开发板教程:UDP收发测试
自建es数据迁移阿里云方案
基于C#利用S7.net库与西门子S7-1200PLC进行通信的具体方法(利用线程循环读取)
Delphi 开发so库,Delphi 调用SO库
腾讯-广点通转化归因
Attention机制学习记录(四)之Transformer
css word-spacing test 2208100255
AI与元宇宙擦出火花:人类失去的只有枷锁,获得的是全方面的解放
-
原文地址:https://blog.csdn.net/qq_68308828/article/details/133785028