From 7f0a49cdfe2b6ca22d3d30ba2375dc2834e89b58 Mon Sep 17 00:00:00 2001
From: ghazwan <ghazwan.sihamudin@gmail.com>
Date: Fri, 19 Feb 2016 11:38:48 +0700
Subject: [PATCH] Bucket Sort with bucketing error

---
 omp_bucket_sort.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 115 insertions(+)
 create mode 100644 omp_bucket_sort.c

diff --git a/omp_bucket_sort.c b/omp_bucket_sort.c
new file mode 100644
index 0000000..2b55b68
--- /dev/null
+++ b/omp_bucket_sort.c
@@ -0,0 +1,115 @@
+#include <stdio.h> 
+#include <stdlib.h> 
+#include <string.h>
+#include <time.h> 
+#include <math.h>
+#include <assert.h> 
+ 
+int *create_rand_nums(int num_elements) { 
+	// creating random elements for num_elements
+	int *rand_nums = (int *)malloc(sizeof(int) * (num_elements+5)); 
+	assert(rand_nums != NULL); 
+	int i; 
+	for (i = 0; i < num_elements; i++) { 
+		  rand_nums[i] = (abs((int)rand()) % num_elements); 
+	} 
+	return rand_nums; 
+}
+
+void insertionSort(int *arr, int size){
+	// sorting an array using insertion sort
+	int i,j;
+    int value;
+    for(i=1;i<size;i++)
+    {
+        value=arr[i];
+        if(value == -1) break;
+        j=i-1;
+        while(j>=0 && value<arr[j])
+        {
+            arr[j+1]=arr[j];
+            j=j-1;
+        }
+        arr[j+1]=value;
+    }
+}
+
+void *make_bucket(int *array,int *ar_result,int *num_res,int num_elements, int num_process,int total_process) {
+	int bagi = num_elements / total_process ;
+	int batas_bawah = (num_process-1)*bagi ;
+	int batas_atas = batas_bawah + bagi - 1 ;
+	if (num_process==total_process-1) batas_atas = num_elements-1 ;
+	
+	int i,num=0 ;
+	for (i=0;i<num_elements;i++) {
+		if (array[i]>=batas_bawah && array[i]<=batas_atas) {
+			ar_result[num] = array[i];
+			num++;
+		}
+	}
+	*num_res = num;
+}
+
+void print_array(int *array, int count) {
+	int i;
+	for (i=0;i<count;i++) {
+		printf("%d  ",array[i]);
+	}
+	printf("\n");
+}
+
+int *sort_array(int *array, int num_elements) {
+	int i,j ;
+	int temp_;
+	int *result = NULL ;
+	result = array ;
+	for (i=0;i<num_elements;i++) {
+		j=i;
+		while (j>0 && result[j] < result[j-1]) {
+			temp_ = result[j] ;
+			result[j] = result[j-1];
+			result[j-1 ] = temp_;
+			j--;
+		}
+	}
+	return result;
+}
+ 
+int main(int argc, char** argv) { 
+	if (argc != 3) {  // check the argument
+	fprintf(stderr, "Usage: ./omp_bucket_sort <number of elements> <number of threads>\n"); 
+	exit(1);
+	} 
+ 
+	int num_element = atoi(argv[1]); // number of elements
+	int thread_count = atoi(argv[2]); // number of threads
+	srand(time(NULL));
+
+	int *rand_nums = NULL; 
+	
+	rand_nums = create_rand_nums(num_element); // creating array with random elements 
+	assert(rand_nums != NULL); 
+	print_array(rand_nums, num_element);
+	int counter;
+
+	time_t start_t;
+	start_t  = clock(); 
+  
+	int *sorted_array = NULL;
+	int *arr =(int *)malloc(sizeof(int) * num_element); 
+#pragma omp parallel for num_threads(thread_count) shared(rand_nums)
+	for (counter = 1;counter<thread_count;counter++) {
+		
+		int size_arr ;		
+		make_bucket(rand_nums,arr,&size_arr,num_element,counter,thread_count);
+		sorted_array = sort_array(arr,size_arr);
+		print_array(sorted_array, size_arr);
+	}
+
+    time_t finish_t = clock();
+    printf("Waktu eksekusi %d data : %d process : %.5f ms\n", num_element, thread_count, (double)(finish_t-start_t)*1000/CLOCKS_PER_SEC);
+   
+	free(rand_nums);
+ 
+	return 0;
+}
-- 
GitLab