Skip to content
Snippets Groups Projects
Commit a8cfec04 authored by 13513057's avatar 13513057
Browse files

sort

parent 729bc0e6
Branches
No related merge requests found
sort.cpp 0 → 100644
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <omp.h>
#include <vector>
using namespace std;
int n, m[400100];
int thread_count;
vector<int> sub[600];
void bucketSort(void) {
int my_rank = omp_get_thread_num();
int size = sub[my_rank].size();
for (int i = 0; i < size; i++) {
for (int j = i+1; j < size; j++) {
if (sub[my_rank][i] > sub[my_rank][j]) {
int tmp = sub[my_rank][i];
sub[my_rank][i] = sub[my_rank][j];
sub[my_rank][j] = tmp;
}
}
}
}
int getSection(int x) {
int width = n / thread_count;
if (x) {
x--;
}
return x/width;
}
int main(int argc, char *argv[]) {
thread_count = strtol(argv[1], NULL, 10);
scanf("%d", &n);
for (int i = 0; i < n; i++) {
m[i] = rand() % n;
}
time_t startTime, endTime;
time(&startTime);
for (int i = 0; i < n; i++) {
sub[getSection(m[i])].push_back(m[i]);
}
#pragma omp parallel num_threads(thread_count)
bucketSort();
#pragma omp barrier
vector<int> result;
for (int i = 0; i < thread_count; i++) {
int size = sub[i].size();
for (int j = 0; j < size; j++) {
result.push_back(sub[i][j]);
}
}
time(&endTime);
double diff = difftime(endTime, startTime);
printf("%lf\n", diff);
printf("\n");
return 0;
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment