diff --git a/bucket_sort.c b/bucket_sort.c index 93e9ae9f717b59f0e44a3f651b78e1a1004a93a9..07b863218a0e3d512508a75e5a49a4097344a312 100644 --- a/bucket_sort.c +++ b/bucket_sort.c @@ -37,6 +37,22 @@ int computeMin(int *data, int num) { return min; } +void insert_sort(int *data, int size) { + int i,j,t; + for (i = 1 ; i < size; i++) { + j = i; + + while ( j > 0 && data[j] < data[j-1]) { + t = data[j]; + data[j] = data[j-1]; + data[j-1] = t; + + j--; + } + } + +} + int main(int argc, char** argv) { if (argc != 2) { @@ -65,17 +81,23 @@ int main(int argc, char** argv) { MPI_Status Stat; int source = 0; int tag = 0; - + //inisialisasi bucket + int k; + for(k = 0; k < num_elements;k++) { + bucket[k] = -1; + } + if(world_rank == 0) rand_nums = create_rand_nums(num_elements); + double time = 0.0; + time -= MPI_Wtime(); if (world_rank == 0) { - rand_nums = create_rand_nums(num_elements); min = 0; max = num_elements; //bagi lalu kirim int num_range = (max-min)/world_size; int i; - int dest; - + int dest; + for (i = 0; i < num_elements; i++) { dest = rand_nums[i]/num_range; if(dest == 0) { @@ -101,20 +123,42 @@ int main(int argc, char** argv) { } } int num_elem_bucket = firstEmpty; - MPI_Barrier(MPI_COMM_WORLD); - - + MPI_Barrier(MPI_COMM_WORLD); + //sort element in bucket + insert_sort(bucket,num_elem_bucket); + MPI_Barrier(MPI_COMM_WORLD); + //gather + int *bucket_container = (int *)malloc(sizeof(int) * num_elements*world_size); + MPI_Gather(bucket, num_elements, MPI_INT, bucket_container, num_elements, MPI_INT, 0, MPI_COMM_WORLD); + MPI_Barrier(MPI_COMM_WORLD); int *bucket_sorted = (int *)malloc(sizeof(int) * num_elements); - MPI_Gather(bucket, num_elem_bucket, MPI_INT, bucket_sorted, num_elem_bucket, MPI_INT, 0, MPI_COMM_WORLD); - MPI_Barrier(MPI_COMM_WORLD); + + if(world_rank == 0) { + + int i,j; + j =0; + for(i = 0; i < num_elements*world_size;i++) { + if(bucket_container[i] != -1) { + bucket_sorted[j] = bucket_container[i]; + j++; + printf("%d - ", bucket_container[i]); + } + } + printf("\n"); + } + MPI_Barrier(MPI_COMM_WORLD); + time += MPI_Wtime(); + if(world_rank == 0) + printf("\nTime = %f \n",time); free(bucket); free(bucket_sorted); free(rand_nums); + free(bucket_container); MPI_Finalize(); } - + \ No newline at end of file