diff --git a/.bucketSort.c.swp b/.bucketSort.c.swp new file mode 100644 index 0000000000000000000000000000000000000000..c7572b7d89d941f29b026d4885bcb849d9e083fe Binary files /dev/null and b/.bucketSort.c.swp differ diff --git a/bucketSort b/bucketSort new file mode 100755 index 0000000000000000000000000000000000000000..51c51fde9460404d2247ba1afa250f04127cfa71 Binary files /dev/null and b/bucketSort differ diff --git a/bucketSort.c b/bucketSort.c index 4517139b0b88ba80096bb783644c22817640c6e4..14c640e52d4f575593e828565ed8c62fb34d775a 100644 --- a/bucketSort.c +++ b/bucketSort.c @@ -1,10 +1,105 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> +#include <time.h> #include <omp.h> +#include <assert.h> + +int *create_rand_nums(int num_elements, int* counts, int* displs, int num_buckets) { + int *rand_nums = (int *)malloc(sizeof(int) * num_elements); + assert(rand_nums != NULL); + int bucket_size = RAND_MAX / num_buckets; + int i, j; + int** buckets; + buckets = (int **)malloc(sizeof(int*) * num_buckets); + for(i = 0; i < num_buckets; i++){ + counts[i] = 0; + displs[i] = 0; + buckets[i] = (int *)malloc(sizeof(int) * num_elements); + } + for (i = 0; i < num_elements; i++) { + int random = (rand() % RAND_MAX); + int bucket = random / bucket_size; + buckets[bucket][counts[bucket]] = random; + counts[bucket]++; + for(j = bucket+1; j < num_buckets; j++){ + displs[j]++; + } + } + for(i = 0; i < num_buckets; i++){ + memcpy(rand_nums + displs[i], buckets[i], counts[i]); + //printf("count %d\n", counts[i]); + free(buckets[i]); + } + free(buckets); + return rand_nums; +} -void bucketSort(); /* Thread Function */ +int get_bucket_count(int bucket, int *array, int num_elements, int num_buckets){ + int bucket_size = RAND_MAX / num_buckets; + int i=0; + int count = 0; -int main(int argc, chra *argv[]) { + for(i = 0; i < num_elements; i++){ + if((array[i]/bucket_size )== bucket) count++; + } + return count; +} + - return 0; +void sort(int *array, int n) { + int c, d, t; + for (c = 1 ; c <= n - 1; c++) { + d = c; + + while ( d > 0 && array[d] < array[d-1]) { + t = array[d]; + array[d] = array[d-1]; + array[d-1] = t; + d--; + } + //printf("saya bucket ke : %d\n",c); + } } + +int main(int argc, char** argv) { + int* sendcounts; + int* displacements; + + if (argc != 2) { + fprintf(stderr, "Usage: number of elements\n"); + exit(1); + } + + + int num_elements = atoi(argv[1]); + srand(time(NULL)); + + int thread_num = omp_get_thread_num(); + int thread_count = omp_get_num_threads(); + + int *rand_nums = NULL; + sendcounts = (int *)malloc(sizeof(int)*thread_count); + displacements = (int *)malloc(sizeof(int)*thread_count); + if (thread_num == 0) { + + rand_nums = create_rand_nums(num_elements, sendcounts, displacements, thread_count); + printf("Ukuran thread : %d\n", thread_count); + } + printf("thread number : %d\n", thread_num); + + int *sub_rand_nums = (int *)malloc(sizeof(int) * num_elements); + assert(sub_rand_nums != NULL); + #pragma omp parallel num_threads(thread_count) + printf("Jumlah thread : %d\n", get_bucket_count(thread_num, sub_rand_nums, num_elements, thread_count)); + sort(sub_rand_nums, get_bucket_count(thread_num, sub_rand_nums, num_elements, thread_count)); + + free(sub_rand_nums); + + if (thread_num == 0) { + free(rand_nums); + } + free(sendcounts); + free(displacements); +} +