算法积累
随机生成数据
//
// Created by fpp on 2018/11/29.
//
#ifndef CHECKIDEASALGORITHM_SORTTESTHELPER_H
#define CHECKIDEASALGORITHM_SORTTESTHELPER_H
#include <iostream>
#include <ctime>
#include <cassert>
using namespace std;
namespace SortTestHelper{
//生成有n个元素的随机数组,每个元素的随机范围为[rangeL,rangeR]
int*generateRandomArray(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;
}
}
#endif //CHECKIDEASALGORITHM_SORTTESTHELPER_H
**********************************************************************************************
#include <iostream>
#include<algorithm>
#include <cstring>
#include "SortTestHelper.h"
using namespace std;
template <typename T>
void selectSort(T arr[],int n){
for (int i=0; i < n; i++){
int minIndex = i;
for (int j=i+1; j < n; j++){
if (arr[minIndex] < arr[j]){
minIndex = j;
}
}
if (minIndex != i){
swap(arr[minIndex],arr[i]);
}
}
}
int main() {
int num=10;
int *p=SortTestHelper::generateRandomArray(num,0,num);
selectSort(p,num);
for (int i = 0; i < num; ++i) {
cout<<p[i]<<"\t";
}
delete[] p;
putchar(10);
getchar();
return 0;
}
posted on 2018-11-29 01:05 Indian_Mysore 阅读(77) 评论(1) 编辑 收藏 举报