//model/util.h template <typename T> T gen_random_num(T min, T max); void gen_uint64(const int &len); void gen_uint32(const int &len); template <typename T> int get_pivot(T *arr, int low, int high); template<typename T> void swap(T *left,T *right); template<typename T> void quick_sort(T *arr,int low,int high); template<typename T> void print_t_array(T *arr,int len); void uint32_array_quick_sort(int len); //model/util.cpp void util::uint32_array_quick_sort(int len) { std::cout<<"std::this_thread::get_id()="<<std::this_thread::get_id()<<std::endl; std::cout<<get_time_now()<<",started in "<<std::string(__FILE__)<<","<<std::string(__FUNCTION__)<<","<<std::to_string(__LINE__)<<std::endl; uint32_t *arr = new uint32_t[len]; for (int i = 0; i < len; i++) { arr[i] = gen_random_num<uint32_t>(0, UINT32_MAX); } std::cout<<"Before quick sort:"<<std::endl; print_t_array<uint32_t>(arr,len); std::cout<<std::endl<<std::endl; quick_sort<uint32_t>(arr,0,len-1); print_t_array<uint32_t>(arr,len); std::cout<<std::endl<<std::endl; std::cout<<get_time_now()<<",finished in "<<std::string(__FILE__)<<","<<std::string(__FUNCTION__)<<","<<std::to_string(__LINE__)<<std::endl; } template <typename T> void util::print_t_array(T *arr, int len) { for(int i=0;i<len;i++) { std::cout<<arr[i]<<"\t"; } std::cout<<std::endl; } template <typename T> void util::quick_sort(T *arr, int low, int high) { if (low < high) { int pivot = get_pivot(arr, low, high); quick_sort(arr, low, pivot - 1); quick_sort(arr, pivot + 1, high); } } template <typename T> void util::swap(T *left, T *right) { T temp = *left; *left = *right; *right = temp; } template <typename T> int util::get_pivot(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(&arr[i], &arr[j]); } } swap(&arr[i + 1], &arr[high]); return i + 1; } void util::gen_uint32(const int &len) { for (int i = 0; i < len; i++) { std::cout << gen_random_num<std::uint32_t>(0, UINT32_MAX) << "\t"; } std::cout << std::endl; } void util::gen_uint64(const int &len) { for (int i = 0; i < len; i++) { std::cout << i + 1 << "," << gen_random_num<std::uint64_t>(0, UINT64_MAX) << std::endl; } } template <typename T> T util::gen_random_num(T min, T max) { std::random_device rd; std::mt19937_64 mt(rd()); std::uniform_int_distribution<T> uid(min, max); return uid(mt); } //main.cpp #include "model/util.h" void gen_uint64_demo(const int& len) { util ul; ul.gen_uint64(std::cref(len)); } void gen_uint32_demo(const int&len) { util ul; ul.gen_uint32(len); } void uint32_array_quick_sort_demo(int len) { util ul; ul.uint32_array_quick_sort(len); } int main(int args,char **argv) { try { uint32_array_quick_sort_demo(std::atoi(argv[1])); } catch(const std::exception& e) { std::cerr << e.what() << '\n'; } }
标签:sort,std,arr,int,void,number,len,util,order From: https://www.cnblogs.com/Fred1987/p/17231748.html