From 92921368912e36c46ca08d06b257fdcb97c5ea9b Mon Sep 17 00:00:00 2001 From: azharfatrr <m.azhar.faturahman@gmail.com> Date: Mon, 14 Mar 2022 16:47:10 +0700 Subject: [PATCH] rollback: returning serial merge sort in utils --- result/K01-03-TC1_parallel.txt | 2 +- result/K01-03-TC2_parallel.txt | 2 +- result/K01-03-TC3_parallel.txt | 2 +- result/K01-03-TC4_parallel.txt | 2 +- src/lib/utils.cu | 58 ++++++++++++++++++++++++++++++++++ src/lib/utils.cuh | 16 ++++++++++ 6 files changed, 78 insertions(+), 4 deletions(-) diff --git a/result/K01-03-TC1_parallel.txt b/result/K01-03-TC1_parallel.txt index a6dd3f6..6213413 100644 --- a/result/K01-03-TC1_parallel.txt +++ b/result/K01-03-TC1_parallel.txt @@ -3,4 +3,4 @@ 10114197 10323010 -Runtime: 0.162644 s +Runtime: 0.213774 s diff --git a/result/K01-03-TC2_parallel.txt b/result/K01-03-TC2_parallel.txt index b0dc098..43987c2 100644 --- a/result/K01-03-TC2_parallel.txt +++ b/result/K01-03-TC2_parallel.txt @@ -3,4 +3,4 @@ 37739803 38222937 -Runtime: 0.871782 s +Runtime: 0.919108 s diff --git a/result/K01-03-TC3_parallel.txt b/result/K01-03-TC3_parallel.txt index a2e3dcc..a3f236b 100644 --- a/result/K01-03-TC3_parallel.txt +++ b/result/K01-03-TC3_parallel.txt @@ -3,4 +3,4 @@ 23198319 23380111 -Runtime: 1.021110 s +Runtime: 1.026466 s diff --git a/result/K01-03-TC4_parallel.txt b/result/K01-03-TC4_parallel.txt index 0392bae..0e9c566 100644 --- a/result/K01-03-TC4_parallel.txt +++ b/result/K01-03-TC4_parallel.txt @@ -3,4 +3,4 @@ 51451884 51774352 -Runtime: 9.808481 s +Runtime: 10.590979 s diff --git a/src/lib/utils.cu b/src/lib/utils.cu index b0efc57..04b72a5 100644 --- a/src/lib/utils.cu +++ b/src/lib/utils.cu @@ -46,3 +46,61 @@ long get_floored_mean(int *n, int length) return sum / length; } + +/* + * Procedure merge_array + * + * Merges two subarrays of n with n[left..mid] and n[mid+1..right] + * to n itself, with n now ordered ascendingly + * */ +void merge_array(int *n, int left, int mid, int right) { + int n_left = mid - left + 1; + int n_right = right - mid; + int iter_left = 0, iter_right = 0, iter_merged = left; + int *arr_left = (int *) malloc(n_left * sizeof(int)); + int *arr_right = (int *) malloc(n_right * sizeof(int)); + + for (int i = 0; i < n_left; i++) { + arr_left[i] = n[i + left]; + } + + for (int i = 0; i < n_right; i++) { + arr_right[i] = n[i + mid + 1]; + } + + while (iter_left < n_left && iter_right < n_right) { + if (arr_left[iter_left] <= arr_right[iter_right]) { + n[iter_merged] = arr_left[iter_left++]; + } else { + n[iter_merged] = arr_right[iter_right++]; + } + iter_merged++; + } + + while (iter_left < n_left) { + n[iter_merged++] = arr_left[iter_left++]; + } + while (iter_right < n_right) { + n[iter_merged++] = arr_right[iter_right++]; + } + + free(arr_left); + free(arr_right); +} + + +/* + * Procedure merge_sort + * + * Sorts array n with merge sort algorithm + * */ +void merge_sort(int *n, int left, int right) { + if (left < right) { + int mid = left + (right - left) / 2; + + merge_sort(n, left, mid); + merge_sort(n, mid + 1, right); + + merge_array(n, left, mid, right); + } +} diff --git a/src/lib/utils.cuh b/src/lib/utils.cuh index 763d092..b0728e9 100644 --- a/src/lib/utils.cuh +++ b/src/lib/utils.cuh @@ -26,4 +26,20 @@ int get_median(int *n, int length); * */ long get_floored_mean(int *n, int length); +/* + * Procedure merge_array + * + * Merges two subarrays of n with n[left..mid] and n[mid+1..right] + * to n itself, with n now ordered ascendingly + * */ +void merge_array(int *n, int left, int mid, int right); + + +/* + * Procedure merge_sort + * + * Sorts array n with merge sort algorithm + * */ +void merge_sort(int *n, int left, int right); + #endif \ No newline at end of file -- GitLab