diff --git a/bucketsort.c b/bucketsort.c index d555182e69a86421c561c896242c023d7defc6a2..c3521b4ff536ea3e5abf3b3ab1d7d1280619ac50 100644 --- a/bucketsort.c +++ b/bucketsort.c @@ -2,55 +2,111 @@ #include <stdlib.h> #include <time.h> #include <mpi.h> +#include "mpi.h" #include <assert.h> +#include <string.h> -float *create_rand_nums(int num_elements) { - float *rand_nums = (float *)malloc(sizeof(float) * num_elements); +int *create_rand_nums(int num_elements) { + int *rand_nums = (int *)malloc(sizeof(int) * num_elements); assert(rand_nums != NULL); int i; for (i = 0; i < num_elements; i++) { - rand_nums[i] = (rand() / (float)RAND_MAX); + rand_nums[i] = rand(); } return rand_nums; } -float* bucket(float* array, int n, int batasbawah, int batasatas){ - float* hasil = (float *)malloc(sizeof(float) * n); - float max =-30000, min=30000; + +int* bucket(int* array, int n, int batasbawah, int batasatas){ + int* hasil = (int *)malloc(sizeof(int) * n); for(int i=0;i<n;i++){ if(array[i]>=batasbawah&&array[i]<=batasatas){ - hasil[i] = array[i] + hasil[i] = array[i]; } } return hasil; } -float * bucketing(float* array,int n){ - + +int** bucketing(int* array,int n, int num_elements){ + int t; + int i,jumlaharray; + int* temp; + int** hasil; + int max=-65536, min=65535; + for( i=0;i < n;i++){ + if(array[i]>max){ + max = array[i]; + } + if(array[i]<min){ + min = array[i]; + } + } + if (( hasil = malloc( num_elements*sizeof( int* ))) == NULL ) + { /* error */ } + for ( i = 0; i < num_elements; i++ ) + { + if (( hasil[i] = malloc( n )) == NULL ) + { /* error */ } + } + for(int j=0;j<num_elements;j++){ + temp = (int *)malloc(sizeof(int) * n); + jumlaharray = 0; + int ite=0; + for(i=0;i<n;i++){ + if(array[i]>=(max*j/num_elements)&&array[i]<=(max*(j+1)/num_elements)){ + temp[ite]= array[i]; + jumlaharray++; + ite++; + } + } + hasil[j] = bucket(temp,jumlaharray,max*j/num_elements,max*(j+1)/num_elements); + + } + return hasil; } -float* bucketSort(float* array, int n){ - float* hasil = (float *)malloc(sizeof(float) * n); - int i,t,c,d; +int* bucketSort(int* array, int n){ + int* hasil = (int *)malloc(sizeof(int) * n); + int t; + int i,c,d; for( i=0;i < n;i++){ hasil[i] = array[i]; } - for (c = 1 ; c <= n - 1; c++) { + for (c = 1 ; c <= n-1 ; c++) { d = c; - while ( d > 0 && hasil[d] < hasil[d-1]) { t = hasil[d]; - array[d] = hasil[d-1]; - array[d-1] = t; - d--; - } + hasil[d] = hasil[d-1]; + hasil[d-1] = t; + d--; + } } return hasil; +} + +void demoBucketSort(int num_elements_per_proc ){ + int *rand_nums = NULL; + rand_nums = create_rand_nums(num_elements_per_proc); + int **sub_avg = NULL; + sub_avg = bucketing(rand_nums, num_elements_per_proc,5); + printf("\n\n\n"); + for(int i=0;i<5;i++){ + printf("%d \n",i); + for(int k=0;k<((unsigned) strlen(sub_avg[i])) ;k++){ + if((unsigned) strlen(sub_avg[i])>0){ + printf("%d ",k); + printf("%d \n", sub_avg[i][k]); + } + } + } } int main(int argc, char** argv) { + MPI_Status Stat; if (argc != 2) { fprintf(stderr, "Usage: avg num_elements_per_proc\n"); exit(1); } int num_elements_per_proc = atoi(argv[1]); + //demo(num_elements_per_proc); srand(time(NULL)); MPI_Init(NULL, NULL); @@ -60,43 +116,38 @@ int main(int argc, char** argv) { int world_size; MPI_Comm_size(MPI_COMM_WORLD, &world_size); - float *rand_nums = NULL; + int *rand_nums = NULL; if (world_rank == 0) { rand_nums = create_rand_nums(num_elements_per_proc * world_size); } - float *sub_rand_nums = (float *)malloc(sizeof(float) * num_elements_per_proc); + int *sub_rand_nums = (int *)malloc(sizeof(int) * num_elements_per_proc); assert(sub_rand_nums != NULL); - - - MPI_Scatter(rand_nums, num_elements_per_proc, MPI_FLOAT, sub_rand_nums, - num_elements_per_proc, MPI_FLOAT, 0, MPI_COMM_WORLD); - - float *sub_avg = NULL; - float *sub_avgs = NULL; - if (world_rank == 0) { - sub_avg = bucketSort(rand_nums, num_elements_per_proc); - sub_avgs = (float *)malloc(sizeof(float) * world_size); - assert(sub_avgs != NULL); - } - - MPI_Gather(sub_avg, 1, MPI_FLOAT, sub_avgs, 1, MPI_FLOAT, 0, - MPI_COMM_WORLD); + int **sub_avg = NULL; + sub_avg = bucketing(rand_nums, num_elements_per_proc,world_size); + int* hasil = (int *)malloc(sizeof(int) * num_elements_per_proc); ; + int ite=0; + int* arraybaru=NULL; + for(int i=0;i<world_size;i++){ + MPI_Send(&sub_avg[i],1,MPI_INT,i,i,MPI_COMM_WORLD); + arraybaru = bucketSort(sub_avg[i],(unsigned) strlen(sub_avg[i])); + MPI_Recv(&arraybaru,1,MPI_INT,i,i,MPI_COMM_WORLD,&Stat); + for(int j=0;j<(unsigned) strlen(sub_avg[i]);j++){ + hasil[ite] = arraybaru[j]; + printf("%d \n",hasil[ite]); + ite++; + } + } - - if (world_rank == 0) { - for(int i=0;i<world_size;i++) - printf("elements [%d] is %f\n",i, &sub_avgs[i]); - } - if (world_rank == 0) { free(rand_nums); free(sub_avg); - free(sub_avgs); + free(arraybaru); + free(hasil); } - free(sub_rand_nums); MPI_Barrier(MPI_COMM_WORLD); MPI_Finalize(); } +