diff --git a/bucket_sort b/bucket_sort index c7f1939fac2a01f06145ce98d197b72c38d52808..3ea01678f534c55e544c4b8e21c6bc83679457b4 100755 Binary files a/bucket_sort and b/bucket_sort differ diff --git a/bucket_sort.c b/bucket_sort.c index a0d86a4722828ad764c4e4a23315a4fe8e7e4d66..f643b3ce19dfed1c1448413c147ced11dc2330b2 100644 --- a/bucket_sort.c +++ b/bucket_sort.c @@ -2,10 +2,13 @@ // Anggota : William Sentosa / 13513026 // Angela Lynn / 13513032 -#include <stdio.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> #include "mpi.h" -#define MAXELEMENT 10000 -#define MAXPROCESS 100 +#define MAXELEMENT 40000 +#define MAXPROCESS 33 +#define ELEMENT 40000 typedef struct { int tabNumber[MAXELEMENT]; @@ -86,6 +89,21 @@ void divideArray(Numbers nums, int division, ArrNumbers *arr) { } } +void insertionSort(int* array, int size) { + int i, j, temp; + + for (i = 1; i <= size - 1; i++) { + j = i; + while (j > 0 && array[j] < array[j-1]) { + temp = array[j]; + array[j] = array[j-1]; + array[j-1] = temp; + + j--; + } + } +} + int main(int argc, char *argv[]) { int numtasks, rank, dest, source, rc, count, tag=1; int length; @@ -101,10 +119,10 @@ int main(int argc, char *argv[]) { Numbers numbers; if (rank == 0) { - printf("halo from rank %d\n", rank); + clock_t start = clock(); MakeNumbers(&numbers); - for (i = 12; i >= 1; i--) { - addNumber(&numbers, i); + for (i = 0; i < ELEMENT; i++) { + addNumber(&numbers, rand()%ELEMENT+1); } MakeArrNumbers(&arrNumbers); divideArray(numbers, numtasks - 1, &arrNumbers); @@ -114,7 +132,7 @@ int main(int argc, char *argv[]) { MPI_Send(&length, 1, MPI_INT, dest, tag, MPI_COMM_WORLD); rc = MPI_Send(arrNumbers.tabNumbers[i-1].tabNumber, length, MPI_INT, dest, tag, MPI_COMM_WORLD); } - + printf("Hasil : "); for(i=1; i<numtasks; i++) { source = i; length = arrNumbers.tabNumbers[i-1].neffNumbers; @@ -124,12 +142,16 @@ int main(int argc, char *argv[]) { printf("%d ", inmsg[j]); } } + clock_t end = clock(); + float seconds = (float)(end - start) / CLOCKS_PER_SEC * 1000; + printf("\n"); + printf("Elapsed Time : %.2f ms\n", seconds); } else { - printf("halo from rank %d\n", rank); source = 0; MPI_Recv(&length, 1, MPI_INT, source, tag, MPI_COMM_WORLD, &Stat); rc = MPI_Recv(inmsg, length, MPI_INT, source, tag, MPI_COMM_WORLD, &Stat); + insertionSort(inmsg, length); dest = 0; rc = MPI_Send(inmsg, length, MPI_INT, dest, tag, MPI_COMM_WORLD);