diff --git a/bucketsort.c b/bucketsort.c new file mode 100644 index 0000000000000000000000000000000000000000..e32204c9652ca875d6c9af5062d724b65575eb2b --- /dev/null +++ b/bucketsort.c @@ -0,0 +1,136 @@ + + +/* Candy Olivia Mawalim (13513031) & Tifani Warnita (13513055) */ +/* Nama File: bucket_sort.c */ + +#include <assert.h> +#include <omp.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> + +/* Function for doing the insertion sort for each bucket */ +/* n is the size of the array */ +void insertionSort(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--; + } + } +} + +/* Function for creating random array with size n */ +/* The maximum value of each item is n */ +int *createRandomArray(int n) { + time_t t; + srand((unsigned) time(&t)); // Initialize random number generator + + int *array = (int *)malloc(sizeof(int) * n); + assert(array != NULL); + int i; + for (i = 0; i < n; i++) { + array[i] = rand() % n; + } + return array; +} + +int main(int argc, char* argv[]) { + int thread_count = strtol(argv[1], NULL, 10); + int num_element_array = strtol(argv[2], NULL, 10); + int my_rank, i; + + // Create the random array + int *array = NULL; + array = createRandomArray(num_element_array); + + /* printf("Array Awal : ["); + for (i=0; i<num_element_array-1; i++) { + printf("%d, ", array[i]); + } + printf("%d]\n", array[i]); */ + + int num_buckets = thread_count; + int num_element_in_bucket[num_buckets]; + + for (i=0; i<num_buckets; i++) { + num_element_in_bucket[i] = 0; + } + + // Timer (start) + double starttime = omp_get_wtime(); + + int range = (num_element_array-0)/num_buckets; + + int j; + // Find number of element in each bucket +# pragma omp parallel for num_threads(thread_count) default(none) shared(array, range, num_element_array, num_buckets, num_element_in_bucket, j) + for(j=0; j<num_element_array; j++) { + int position = array[j] / range; + if (position >= num_buckets) // Biar ga kelebihan + position = num_buckets - 1; + + # pragma omp critical + { + num_element_in_bucket[position] = num_element_in_bucket[position] + 1; + } + } +# pragma omp barrier + + int array_result[num_element_array]; + + + + // Sorting +# pragma omp parallel num_threads(thread_count) default(none) shared(array, array_result, num_element_array, num_element_in_bucket, num_buckets, range) private(my_rank) + { + my_rank = omp_get_thread_num(); + + // Create local array + int local_array[num_element_in_bucket[my_rank]]; + int j, k = 0; + + for(j=0; j<num_element_array; j++) { + int position = array[j] / range; + if (position == my_rank || (my_rank==(num_buckets-1) && position >= num_buckets)) { + local_array[k] = array[j]; + k++; + } + } + + // Sorting + insertionSort(local_array, num_element_in_bucket[my_rank]); + + // First index to put + int l = 0; + for(j=0; j<my_rank; j++) { + l += num_element_in_bucket[j]; + } + + for(j=0; j<num_element_in_bucket[my_rank]; j++) { + array_result[l] = local_array[j]; + l++; + } + } +# pragma omp barrier + + double endtime = omp_get_wtime(); + + /* printf("Array Akhir: ["); + for(i=0; i<num_element_array-1; i++) { + printf("%d, ", array_result[i]); + } + printf("%d]\n", array_result[i]); */ + + printf("Waktu : %lf sekon\n", endtime-starttime); + + return 0; +} + +