diff --git a/bucketsort b/bucketsort new file mode 100755 index 0000000000000000000000000000000000000000..30c779d81839b6096c28280ea3e417c2bfde64cc Binary files /dev/null and b/bucketsort differ diff --git a/bucketsort.c b/bucketsort.c new file mode 100644 index 0000000000000000000000000000000000000000..1c403faf86355f3ab1ef7abeceb210c5705ac201 --- /dev/null +++ b/bucketsort.c @@ -0,0 +1,127 @@ +#include <stdio.h> +#include <stdlib.h> +#include <omp.h> +#include <assert.h> +#include <time.h> + +float *create_rand_nums(int num_elements) { + float *rand_nums = (float *)malloc(sizeof(float) * num_elements); + assert(rand_nums != NULL); + int i; + for (i = 0; i < num_elements; i++) { + rand_nums[i] = (rand() / (float)RAND_MAX); + } + return rand_nums; +} + +float * Sort(float * unsorted) { + int c, d; + int size = unsorted[0]; + float t; + + for (c=1;c<size; c++) { + d = c; + + while (d > 1 && unsorted[d] < unsorted[d-1]) { + t = unsorted[d]; + unsorted[d] = unsorted[d-1]; + unsorted[d-1] = t; + + d--; + } + } + + return unsorted; +} + +float * * Scatter (float * input, int size) { + int * min_limit = (int *)malloc(sizeof(int) * 5); + int * max_limit = (int *)malloc(sizeof(int) * 5); + float * * output = (float **)malloc(sizeof(float*) * 400000); + + //define the border limit + int i; + for (i = 0 ;i < 5; i++) { + min_limit[i] = 10 * i + 0 ; + max_limit[i] = 10 * i + 9 ; + } + + //define the size of every bucket, the first index of the array is the size + for (i = 0; i < 5; i++){ + output[i][0] = 0; + } + + //scatter the input into 5 bucket + int index =0; + for ( i = 0; i < size ; i++) { + if (input[i] < max_limit[0]) { + output[0][0]++; + index = output[0][0]; + output[0][index] = input[i]; + } else if ((input[i] < max_limit[1]) && (input[i] > min_limit[1])) { + output[1][0]++; + index = output[1][0]; + output[1][index] = input[i]; + } else if ((input[i] < max_limit[2]) && (input[i] > min_limit[2])) { + output[2][0]++; + index = output[2][0]; + output[2][index] = input[i]; + } else if ((input[i] < max_limit[3]) && (input[i] > min_limit[3])) { + output[3][0]++; + index = output[3][0]; + output[3][index] = input[i]; + } else if (input[i] > min_limit[41]) { + output[4][0]++; + index = output[4][0]; + output[4][index] = input[i]; + } + } + + return output; +} + +float** SortAllBucket(float* *arrayfloat, int M) { + float* *sortedarray; + int i=0; + + #pragma omp parallel for shared(sortedarray, arrayfloat) private (i) num_threads(M) + for (i=0;i<5;i++) { //Bucketnya ada 5 + sortedarray[i] = Sort(arrayfloat[i]); + } +} + +float BucketSort(float* arrayfloat, int N, int M) { + struct timeval start,end; + + gettimeofday(&start, NULL); + + float** array1 = Scatter(arrayfloat,N); + + /*float** sortedarray = SortAllBucket(array1, M);*/ + + gettimeofday(&end, NULL); + float delta = ((end.tv_sec - start.tv_sec)*1000000u + end.tv_usec - start.tv_usec) / 1.e6; + return delta; +} + +int main (int argc, char* argv[]) { + int N; //jumlah elemen array + int M; //jumlah thread + float time; + float * arrayfloat; + + if (argc != 3) { + fprintf(stderr, "Usage: %s <jumlah elemen array> <jumlah thread>", argv[0]); + exit(0); + } + N = atoi(argv[1]); + M = strtol(argv[2], NULL, 10); + + arrayfloat = create_rand_nums(N); + + time = BucketSort(arrayfloat,N,M); + + printf("Waktu yang dibutuhkan : %lf\n", time); + + return 0; +} diff --git a/omp_trap b/omp_trap new file mode 100755 index 0000000000000000000000000000000000000000..6dd9157fc98d0e2800bebc67c612c7cd6f75be1a Binary files /dev/null and b/omp_trap differ