日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

多线程生成随机数组+双线程归并排序(C++实现)

發布時間:2025/4/16 c/c++ 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 多线程生成随机数组+双线程归并排序(C++实现) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

算法概述

  • 動態數組生成
  • 多線程隨機數組生成
  • 雙線程歸并排序

代碼

#include <iostream> #include <thread> #include <ctime> #include <vector> #include <mutex> using namespace std;int* arrays; int N; int T; // begin_index: where the thread begins to put number in // end_index: where the thread stops putting number in void run_test(int begin_index, int end_index) {srand((unsigned)time(NULL) + begin_index);for (int i = begin_index; i<end_index; i++)arrays[i] = rand() % N; }void Merge(int begin_index, int mid_index, int end_index);void M_SORT_f(int begin_index, int end_index) {if (begin_index >= end_index - 1) return;int mid = (end_index + begin_index) / 2;M_SORT_f(begin_index, mid);M_SORT_f(mid, end_index);Merge(begin_index, mid, end_index); }void M_SORT(int begin_index, int end_index) {M_SORT_f(begin_index, end_index); }void Merge(int begin_index, int mid_index, int end_index) {if (begin_index >= end_index - 1) return;int *temp = new int[end_index - begin_index];int i = begin_index, j = mid_index, k = 0;while (i < mid_index && j < end_index) {if (arrays[i] < arrays[j])temp[k++] = arrays[i++];elsetemp[k++] = arrays[j++];}while (i < mid_index) temp[k++] = arrays[i++];while (j < end_index) temp[k++] = arrays[j++];for (int t = begin_index; t < end_index; ++t) arrays[t] = temp[t - begin_index];delete[] temp; }int main() {N = 100000;T = 10;_ASSERT(N >= T);cout << "Array Building.\n";arrays = new int[N];cout << "Array Build Finished.\n";// Ignore thread creation, join action, vector<thread> for nowvector<thread> vt;// create T threads and push them into a vector// each thread will be running the function "run_test"for (int i = 0; i<T; i++)vt.push_back(thread(run_test, i*N / T, (i + 1)*N / T));for (int i = 0; i<T; i++)vt[i].join();vt.clear();T = 2;for (int i = 0; i<T; i++)vt.push_back(thread(M_SORT, i*N / T, (i + 1)*N / T));for (int i = 0; i<T; i++)vt[i].join();Merge(0, N / T, N);for (int i = 0; i < N; ++i) cout << arrays[i] << " ";cout << endl;delete[] arrays;system("pause");return 0; }

也可以是下面的代碼

#include <iostream> #include <thread> #include <ctime> #include <vector> #include <mutex> #include <ctime> using namespace std;int* arrays; int N; int T; // begin_index: where the thread begins to put number in // end_index: where the thread stops putting number in void run_test(int begin_index, int end_index) {srand((unsigned)time(NULL) + begin_index);for (int i = begin_index; i<end_index; i++)arrays[i] = rand() % N; }void Merge(int begin_index, int mid_index, int end_index);void M_SORT(int begin_index, int end_index) {if (begin_index >= end_index - 1) return;int mid = (end_index + begin_index) / 2;M_SORT(begin_index, mid);M_SORT(mid, end_index);Merge(begin_index, mid, end_index); }void Merge(int begin_index, int mid_index, int end_index) {if (begin_index >= end_index - 1) return;int *temp = new int[end_index - begin_index];int i = begin_index, j = mid_index, k = 0;while (i < mid_index && j < end_index) {if (arrays[i] < arrays[j])temp[k++] = arrays[i++];elsetemp[k++] = arrays[j++];}while (i < mid_index) temp[k++] = arrays[i++];while (j < end_index) temp[k++] = arrays[j++];for (int t = begin_index; t < end_index; ++t) arrays[t] = temp[t - begin_index];delete[] temp; }int main() {N = 1000000;T = 10;_ASSERT(N >= T);cout << "Array Building.\n";arrays = new int[N];cout << "Array Build Finished.\n";// Ignore thread creation, join action, vector<thread> for nowvector<thread> vt;// create T threads and push them into a vector// each thread will be running the function "run_test"for (int i = 0; i<T; i++)vt.push_back(thread(run_test, i*N / T, (i + 1)*N / T));for (int i = 0; i<T; i++)vt[i].join();vt.clear();T = 2;for (int i = 0; i<T; i++)vt.push_back(thread(M_SORT, i*N / T, (i + 1)*N / T));for (int i = 0; i<T; i++)vt[i].join();int st, et;st = clock();Merge(0, N / T, N);et = clock();/*for (int i = 0; i < N; ++i) cout << arrays[i] << " ";cout << endl; */cout << "time: " << (et - st) / 1000.0 << " second(s)\n";delete[] arrays;system("pause");return 0; }

總結

以上是生活随笔為你收集整理的多线程生成随机数组+双线程归并排序(C++实现)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。