diff --git a/bucketSort.c b/bucketSort.c new file mode 100644 index 0000000000000000000000000000000000000000..31789e842ae2da8b766ea5d9556afcce7c679880 --- /dev/null +++ b/bucketSort.c @@ -0,0 +1,160 @@ +#include <stdio.h> +#include <stdlib.h> +#include <mpi.h> +#include <assert.h> + + +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--; + } + } +} + +int main(int argc, char** argv) { + double starttime, endtime; + int iteration, increase, changebucket,i; + unsigned int j; + int receivedElement; + + MPI_Init(NULL, NULL); + + int world_rank, world_size; + MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); + MPI_Comm_size(MPI_COMM_WORLD, &world_size); + MPI_Status stat; + + int num_elements = atoi(argv[1]); + int *elements = malloc(sizeof(int)*num_elements); + + + if(world_rank == 0) { + + for(i = 0; i < num_elements; i++) + { + elements[i] = rand() % (num_elements+1); + } + + for(i = 0; i < num_elements; i++) + { + printf(" %d ", elements[i]); + } + + starttime = MPI_Wtime(); + + if(world_size != 1) { + + // Mencari nilai max dan min + unsigned int min = elements[0]; + unsigned int max = elements[0]; + for(i = 0;i < num_elements; i++) { + if(elements[i] < min); + min = elements[i]; + if(elements[i] > max) + max = elements[i]; + } + + + // Menghitung banyak elemen yang akan didapat proses/bucket + int *elementQtyArray = (int*)malloc(sizeof(int)*world_size); + for(i = 1; i < world_size; i++) { + elementQtyArray[i]=0; + } + + + for(i = 0; i < num_elements; i++) { + increase = max/(world_size-1); + iteration = 1; + changebucket = 0; + + for (j = increase; j<= max; j = j+increase) { + if(elements[i] <= j) { + elementQtyArray[iteration]++; + changebucket = 1; + break; + } + iteration++; + } + if(changebucket == 0) + elementQtyArray[iteration-1]++; + } + + // Mengirim banyaknya elemen yang akan didapat proses/bucket + for(i=1;i<world_size;i++) { + MPI_Send(&elementQtyArray[i], 1, MPI_INT, i, 1, MPI_COMM_WORLD); + } + + // Mengirim element + for(i = 0; i < num_elements; i++) { + iteration = 1; + changebucket = 0; + for(j = increase; j<=max; j = j+increase) { + if(elements[i] <= j) { + MPI_Send(&elements[i], 1, MPI_UNSIGNED, iteration, 2, MPI_COMM_WORLD); + changebucket = 1; + break; + } + iteration++; + } + if(changebucket == 0) { + MPI_Send(&elements[i], 1, MPI_UNSIGNED, iteration-1, 2, MPI_COMM_WORLD); + } + } + + // Mendapatkan hasil sort dari tiap bucket dan dijadikan satu array + int lastIndex = 0; + int index = 0; + for(i=1; i < world_size;i++) { + unsigned int *recvArray = (unsigned int*)malloc(sizeof(unsigned int)*elementQtyArray[i]); + MPI_Recv(&recvArray[0], elementQtyArray[i], MPI_UNSIGNED, i, 100, MPI_COMM_WORLD, &stat); + if(lastIndex==0) + lastIndex=elementQtyArray[i]; + for(j=0;j<elementQtyArray[i];j++) { + elements[index] = recvArray[j]; + index++; + } + } + } + else { + insertionSort(elements,num_elements); + } + + endtime = MPI_Wtime(); + + printf ("------------Hasil-----------\n"); + for(i = 0; i < num_elements; i++) + { + printf(" %d ", elements[i]); + } + prinf("\n"); + printf("Waktu Eksekusi : %.5f\n", endtime-starttime); + } + else { + int elementQtyUsed; + MPI_Recv(&elementQtyUsed, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &stat); + unsigned int *localArray = (unsigned int*)malloc(sizeof(unsigned int)*elementQtyUsed); // initiating a local bucket + + for(i=0;i<elementQtyUsed;i++) { + MPI_Recv(&receivedElement, 1, MPI_UNSIGNED, 0, 2, MPI_COMM_WORLD, &stat); + localArray[i] = receivedElement; + } + + insertionSort(localArray,elementQtyUsed); + + MPI_Send(localArray, elementQtyUsed, MPI_UNSIGNED, 0, 100, MPI_COMM_WORLD); + } + + + + MPI_Finalize(); + return 0; +} diff --git a/laporan.txt b/laporan.txt new file mode 100644 index 0000000000000000000000000000000000000000..d89102fa1ee3c84141aec659a9b22293762cd36c --- /dev/null +++ b/laporan.txt @@ -0,0 +1,27 @@ +N : 50.000 + M : 1 = 3.40591 + 4 = 1.58598 + 8 = 1.23196 + 16 = 1.10108 + 32 = 0.44516 + +N : 100.000 + M : 1 = 18.67849 + 4 = 5.98211 + 8 = 2.46938 + 16 = 2.03334 + 32 = 0.57870 + +N : 200.000 + M : 1 = 81.09364 + 4 = 32.51264 + 8 = 5.60959 + 16 = 3.21267 + 32 = 1.06012 + +N : 400.000 + M : 1 = 444.94194 + 4 = 114.84448 + 8 = 27.81522 + 16 = 8.26912 + 32 = 2.43067 \ No newline at end of file