昆仑山:眼中无形心中有穴之穴人合一

夫君子之行,静以修身,俭以养德;非澹泊无以明志,非宁静无以致远。夫学须静也,才须学也;非学无以广才,非志无以成学。怠慢则不能励精,险躁则不能冶性。年与时驰,意与岁去,遂成枯落,多不接世。悲守穷庐,将复何及!

 

算法积累

随机生成数据

//
// 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编辑  收藏  举报

导航