首页 > 编程语言 >C++ get random via random_device, mt19937_64,uniform_int_distribution, quick sort

C++ get random via random_device, mt19937_64,uniform_int_distribution, quick sort

时间:2022-11-06 18:59:56浏览次数:48  
标签:sort arr via min int max random char include

#include <chrono>
#include <ctime>
#include <fstream>
#include <iostream>
#include <random>
#include <sstream>
#include <thread>
#include <unistd.h>
#include <uuid/uuid.h>

using namespace std;

char *dtValue=(char*)malloc(20);
char *uuidValue=(char*)malloc(40);

char* getTimeNow()
{
    time_t rawTime=time(nullptr);
    struct tm tmInfo=*localtime(&rawTime);
    strftime(dtValue,20,"%Y%m%d%H%M%S",&tmInfo);
    return dtValue;
}

char *getUuid()
{
    uuid_t newUUID;
    uuid_generate(newUUID);
    uuid_unparse(newUUID,uuidValue);
    return uuidValue;
}



random_device rd;
mt19937_64 mt(rd());

template<typename T>
T getRandom(T min,T max)
{
    uniform_int_distribution<T> uid(min,max);
    return uid(mt);
}



template<typename T>
void swap(T *left,T *right)
{
    T temp=*left;
    *left=*right;
    *right=temp;
}


  template<typename T>   int partitionAsc(T *arr,int low,int high)   {       T pivot=arr[high];     int i=low-1;     for(int j=low;j<=high;j++)     {       if(arr[j]<pivot)       {         i=i+1;         swap<T>(&arr[i],&arr[j]);       }       }     swap<T>(&arr[i+1],&arr[high]);   return i+1;   }
 
template<typename T>
void quickSortAsc(T* arr,int low,int high)
{
    if(low<high)
    {
        int pivot=partitionAsc(arr,low,high);
        quickSortAsc(arr,low,pivot-1);
        quickSortAsc(arr,pivot+1,high);
    }
} 

template<typename T>
void getTArray(T *arr,T min,T max,int len)
{
    for(int i=0;i<len;i++)
    {
        arr[i]=getRandom<T>(min,max);
    }
}

template<typename T>
void printTArray(T *arr,int len)
{
    for(int i=0;i<len;i++)
    {
        cout<<arr[i]<<"\t";
    }
    cout<<endl<<endl<<endl;
}

template<typename T>
void arrayTDemo(T min,T max,int len)
{
    T *arr=new T[len];
    getTArray(arr,min,max,len);
    cout<<"Before quick sort:"<<endl;
    printTArray(arr,len);
    quickSortAsc(arr,0,len-1);
    cout<<"After quick sort:"<<endl;
    printTArray(arr,len);
    delete []arr;
    cout<<getTimeNow()<<","<<__FUNCTION__<<","<<__LINE__<<endl;
}

int main(int args,char **argv)
{
    arrayTDemo<uint32_t>(0,UINT32_MAX,atoi(argv[1]));
}
g++ -g -std=c++2a -I. *.cpp -o h1 -luuid
./h1 100000

 

 

标签:sort,arr,via,min,int,max,random,char,include
From: https://www.cnblogs.com/Fred1987/p/16863349.html

相关文章

  • 33. Search in Rotated Sorted Array
    Supposeanarraysortedinascendingorderisrotatedatsomepivotunknowntoyoubeforehand.(i.e., 0124567 mightbecome 4567012).Youaregiv......
  • JAVA8-Lambda-(sorted+Comparator)排序
    使用场景:排队的时候按照个子大小排队使用API排序和MySql中的升序降序规则一样。在排序时需要注意的是降序需要用到reversed();publicstaticvoidmain(String[]......
  • Hive Order By,Sort by,Distribute By,Cluster By 排序区别
    OrderByOrderBy:全局排序,只有一个Reducer,就算提前设置好n个reducerorderby也是只执行一个reducer,因为全局排序,排序的仅仅是一个表罢了。orderby对于大规模数据集......
  • R语言随机森林RandomForest、逻辑回归Logisitc预测心脏病数据和可视化分析|附代码数据
    全文链接:http://tecdat.cn/?p=22596本报告是对心脏研究的机器学习/数据科学调查分析。更具体地说,我们的目标是在心脏研究的数据集上建立一些预测模型,并建立探索性和建模方......
  • 【100个 Unity实用技能】| C# 中 Sort() 对List中的数据排序的几种方法 整理总结
    Unity小科普老规矩,先介绍一下Unity的科普小知识:Unity是实时3D互动内容创作和运营平台。包括游戏开发、美术、建筑、汽车设计、影视在内的所有创作者,借助Unity将创意......
  • Solution-P7650 [BalticOI 2007 Day 1] Ranklist Sorting(DP)
    容易发现一条性质:每个人最多只会被移动一次。说明人只有两种:移动的和不移动的。考虑枚举所有不移动的人,并最优化其它人的移动顺序。最开始第\(i\)个人的起点为\(i\),终......
  • Collections-sort
    importjava.util.ArrayList;importjava.util.Collections;importjava.util.List;/**1.Collections.sort(list);只能对List排序,注意:list中的*元素类型必须具备可......
  • s-sort命令
    对文本操作进行排序,以行为单位,依次根据ascii值进行比较,默认的排序方式为升序sort[-bcfMnrtk][源文件][-o输出文件]补充说明:sort可针对文本文件的内容,以行为单位来排......
  • Random-获取随机数
    之前也用过Random的函数,今天了解一些python中random的函数。importrandomprint(random.random)print(random.random())这用于生成一个0到1的随机符点数:0<=n<1.0看结......
  • 排序之希尔排序(shell sort)
    前言本篇博客是在伍迷兄的博客基础上进行的,其​​博客地址​​点击就可以进去,里面好博客很多,我的排序算法都来自于此;一些数据结构方面的概念我就不多阐述了,伍迷兄的博客......