From 0626913e5f25e4816cbff13b85cb7b70dfeaca01 Mon Sep 17 00:00:00 2001
From: 13512501 <13512501@hpc.if.itb.ac.id>
Date: Fri, 19 Feb 2016 10:43:59 +0700
Subject: [PATCH] sudah bisa sort. belum testing waktu

---
 README.md             | 21 ++++++++++
 bucket_sort_openmp.c  | 97 +++++++++++++++++++++++++++++++++++++++++++
 insertion_sort_func.c | 22 ++++++++++
 laporan.txt           |  0
 random.c              | 22 ++++++++++
 5 files changed, 162 insertions(+)
 create mode 100644 bucket_sort_openmp.c
 create mode 100644 insertion_sort_func.c
 create mode 100644 laporan.txt
 create mode 100644 random.c

diff --git a/README.md b/README.md
index e69de29..42b4a7b 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1,21 @@
+Readme Praktikum II IF3230 Sistem Paralel dan Terdistribusi Semester II 2015/2016
+13512501	Muhammad Nizami
+13513039        Ivan Andrianto
+
+Laporan:
+        laporan.txt
+
+Source code:
+	bucket_sort.c random.c insertion_sort_func.c
+
+Mengkompilasi:
+	gcc bucket_sort_openmp.c -o bucket_sort_openmp
+
+	PERHATIAN! jangan coba-coba melakukan linking dengan random.c maupun insertion_sort_func.c karena keduanya diinclude
+
+Menjalankan:
+	./bucket_sort_openmp <thread_count>
+
+	INPUT: satu integer desimal menandakan banyak bilangan
+
+
diff --git a/bucket_sort_openmp.c b/bucket_sort_openmp.c
new file mode 100644
index 0000000..6e8fd0c
--- /dev/null
+++ b/bucket_sort_openmp.c
@@ -0,0 +1,97 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <omp.h>
+#include <assert.h>
+#include <limits.h>
+#include <string.h>
+
+#include "random.c"
+
+#include "insertion_sort_func.c"
+
+void sort(int * arr, int len){
+	insertion_sort(arr,len);
+}
+
+int main(int argc, char ** argv){
+
+    if (argc<2){
+            fprintf(stderr, "bucket_sort_openmp <thread_count>\n");
+            exit(1);
+    }
+    
+        int thread_count =strtol(argv[1],NULL,10);
+    
+	int * compl_arr;
+	int i,j;
+		int num_elmts;
+
+        //input
+        scanf("%d",&num_elmts);
+
+        compl_arr = (int*) malloc(num_elmts*sizeof(int));
+
+        //random elemen
+        randomarr(compl_arr,num_elmts);
+
+        for (i=0;i<num_elmts;i++){
+            printf("%d ", compl_arr[i]);
+        }
+        printf("\n");
+
+	//bagi-bagi
+	int * num_elmts_in_buckets;
+	int * buckets_bin;
+	int ** buckets;
+
+        num_elmts_in_buckets = (int*) malloc(thread_count*sizeof(int));
+        buckets = (int**) malloc(thread_count*sizeof(int*));
+        buckets_bin = (int*) malloc(thread_count*sizeof(int));
+
+        for (i=0;i<thread_count;i++) buckets_bin[i]=num_elmts*(i+1)/thread_count;
+
+        for (i=0;i<thread_count;i++){ num_elmts_in_buckets[i]=0; buckets[i]=malloc(num_elmts*sizeof(int));}
+
+        for (i=0;i<num_elmts;i++){
+                for (j=0;j<thread_count;j++){
+                        if (compl_arr[i]<=buckets_bin[j]){
+                                buckets[j][num_elmts_in_buckets[j]]=compl_arr[i];
+                                num_elmts_in_buckets[j]++;
+                                break;
+                        }
+                }
+        }
+        
+        //buat reduksinya
+        int * bucket_offset = malloc(thread_count*sizeof(int));
+        int curbuck_offset = 0;
+        for (i=0;i<thread_count;i++){
+            bucket_offset[i]=curbuck_offset;
+            curbuck_offset+=num_elmts_in_buckets[i];
+        }
+
+#pragma omp parallel for num_threads(thread_count)
+        for (i=0;i<thread_count;i++)
+        {
+            sort(buckets[i], num_elmts_in_buckets[i]);
+            
+            //copy
+            memcpy(&compl_arr[bucket_offset[i]],buckets[i],num_elmts_in_buckets[i]*sizeof(int));
+        }
+
+        for (i=0;i<num_elmts;i++){
+            printf("%d ", compl_arr[i]);
+        }
+        printf("\n");
+        
+        free(bucket_offset);
+        free(num_elmts_in_buckets);
+        for (i=0;i<thread_count;i++)
+                free(buckets[i]);
+        free(buckets);
+        free(buckets_bin);
+        free(compl_arr);
+
+}
+
diff --git a/insertion_sort_func.c b/insertion_sort_func.c
new file mode 100644
index 0000000..337e5ac
--- /dev/null
+++ b/insertion_sort_func.c
@@ -0,0 +1,22 @@
+/* Copyrights http://www.programmingsimplified.com/c/source-code/c-program-insertion-sort */
+/* insertion sort ascending order */
+
+//insertion sort
+ 
+void insertion_sort (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--;
+    }
+  }
+}
+
diff --git a/laporan.txt b/laporan.txt
new file mode 100644
index 0000000..e69de29
diff --git a/random.c b/random.c
new file mode 100644
index 0000000..888cb1b
--- /dev/null
+++ b/random.c
@@ -0,0 +1,22 @@
+#include <stdlib.h>
+
+void swap(int * a, int * b){
+	int c = *b;
+	*b = *a;
+	*a = c;
+	
+}
+
+void randomarr(int vektor [], int n) {
+	int i;
+	for (i=0;i<n;)
+		vektor[i]=++i;
+
+	int max;
+	int r;
+	for (max=n-1;max>=0;max--){
+		r = rand()%n;
+		swap(&vektor[r],&vektor[max]);
+	}
+}
+
-- 
GitLab