Skip to content
Snippets Groups Projects
Commit 4691bd20 authored by Viktor Buntoro's avatar Viktor Buntoro
Browse files

masih error

parent 729bc0e6
Branches
No related merge requests found
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
int thread_count;
int main(int argc, char* argv[]) {
int a; //jumlah elemen
int *array; //array
int *buckets; //bucket
if (argc != 2) {
fprintf(stderr, "usage: %s <number of threads>\n", argv[0]);
exit(0);
}
thread_count = strtol(argv[1], NULL, 10);
printf("masukkan jumlah elemen array");
scanf("%d", &a);
// ngisi arrray
array = malloc(sizeof(int*) * a);
//Random nilai array
srand(time(NULL));
for(int i=0; i<a; i++) {
array[i] = rand();
}
buckets = bucketSort(array, thread_count);
return 0;
}
int getSize(int *array) {
int n = 0;
while (array[n]) {
n++;
}
return n;
}
int max;
max = array[0];
for(int i=1; i<n; i++) {
if(array[i] > max) {
max = array
}
}
return max;
}
int *bucketSort(int *array, int n) { //n adalah jumlah bucket
int **buckets; //Isi bucket
int k; //rentang nilai isi bucket
int arrsize; //panjang array
int count; //Untuk menentukan nomor cell pada bucket tertentu
int *sendbuf; //bucket yang sudah disortir dan akan di-return
int *sendcounts;
int *displs;
sendcounts = malloc(sizeof(int*) * n);
displs = malloc(sizeof(int*) * n);
arrsize = getSize(array);
count = malloc(sizeof(int *) * n);
buckets = malloc(sizeof(int *) * n);
for(int i=0; i<n; i++) {
count[i] = 0;
buckets[i] = malloc(sizeof(int *) * arrsize);
}
//Menghitung rentang nilai bucket
if(((max-min+1)%n) > 0) {
k = ((max-min+1)/n) + 1;
} else {
k = ((max-min+1)/n);
}
# pragma omp parallel for schedule(static) default(none) \
shared(array, n) private(i, m) \
reduction(+: sendbuf) num_threads(thread_count)
//Mengisi array ke bucket yang sesuai
for (int i = 0; i<arrsize; i++) {
int m; //nomor bucket
m = array[i]/k;
buckets[m][count[m]] = array[i];
count[m]++;
}
for(int i=0; i<n; i++) {
sendcounts[i] = count[i];
}
displs[0] = 0;
for(int i=1; i<n; i++) {
displs[i] = displs[i-1] + count[i-1];
}
for(int i=0; i<n; i++) {
memcpy(sendbuf + displs[i], buckets[i], count[i];
free(buckets[i]);
}
free(buckets);
nextSort(sendbuf);
return sendbuf;
}
int *bucket(int **array, int i, int count) { //i adalah nomor bucket, count adalah jumlah isi bucket
int *tempBucket;
tempBucket = malloc(sizeof(int*) * count);
for(int j=0; j<count; j++) {
tempBucket[j] = array[i][count];
}
return tempBucket;
}
void nextSort(int *array) {
int n = getSize(array);
int d, t;
for (int i=1 ; i <=n-1; i++) {
d = i;
while ((d > 0) && (array[d] < array[d-1])) {
t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
}
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