cpp generate random array and then quick sort

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <algorithm>
#include <chrono>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <random>
#include <sstream>
#include <thread>
#include <uuid/uuid.h>
#include <vector>
 
std::string get_time_now()
{
    std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now();
    time_t raw_time = std::chrono::high_resolution_clock::to_time_t(now);
    tm tm_info = *localtime(&raw_time);
    std::stringstream ss;
    ss << std::put_time(&tm_info, "%Y%m%d%H%M%S");
    auto seconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch());
    auto mills = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
    auto micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch());
    auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch());
    ss << "_"
       << std::setw(3) << std::setfill('0') << (mills.count() - seconds.count() * 1000)
       << std::setw(3) << std::setfill('0') << (micros.count() - mills.count() * 1000)
       << std::setw(3) << std::setfill('0') << (nanos.count() - micros.count() * 1000);
    return ss.str();
}
 
std::random_device rd;
std::mt19937_64 _mt{rd()};
template <typename T>
T gen_rand(T min, T max)
{
 
    std::uniform_int_distribution<T> _uid(min, max);
    return _uid(_mt);
}
 
template <typename T>
void gen_rand_array(T *arr, T min, T max, const int &len)
{
    for (int i = 0; i < len; i++)
    {
        arr[i] = gen_rand<T>(min, max);
    }
}
 
template <typename T>
void print_t_array(T *arr, const int &len)
{
    for (int i = 0; i < len; i++)
    {
        std::cout << arr[i] << std::endl;
    }
    std::cout << std::endl;
}
 
template <typename T>
void gen_print_rand_array(T min, T max, const int &len)
{
    T *arr = new T[len];
    gen_rand_array(arr, min, max, std::cref(len));
    print_t_array(arr, std::cref(len));
    delete[] arr;
    std::cout << get_time_now() << ",finish in " << __FUNCTION__ << std::endl;
}
 
template <typename T>
void swap(T *t1, T *t2)
{
    if (t1 == t2)
    {
        return;
    }
 
    T temp = *t1;
    *t1 = *t2;
    *t2 = temp;
}
 
template <typename T>
int partition(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;
}
 
template <typename T>
void quick_sort_t(T *arr, int low, int high)
{
    if (low < high)
    {
        int pivot = partition(arr, low, high);
        quick_sort_t(arr, low, pivot - 1);
        quick_sort_t(arr, pivot + 1, high);
    }
}
 
template <typename T>
void gen_print_quick_sort_t_array(T min, T max, const int &len)
{
    T *arr = new T[len];
    gen_rand_array(arr, min, max, std::cref(len));
    print_t_array(arr, std::cref(len));
    // std::cout << "\nAfter quick sort:" << std::endl;
    quick_sort_t(arr, 0, len - 1);
    print_t_array(arr, std::cref(len));
    delete[] arr;
    // std::cout << get_time_now() << ",thread id:" << std::this_thread::get_id() << ",finish in " << __FUNCTION__ << std::endl;
}
 
int main(int args, char **argv)
{
    gen_print_quick_sort_t_array<std::uint32_t>(0, UINT32_MAX, atoi(argv[1]));
    std::cout << get_time_now() << ",finished in " << __FUNCTION__ << ",thread id:" << std::this_thread::get_id() << std::endl;
}

  Compile

1
g++-12 -std=c++2a -I. *.cpp -o h1 -luuid

  

 

posted @   FredGrit  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示