-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_sort.cu
65 lines (51 loc) · 1.58 KB
/
merge_sort.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <cstdlib>
// Merge two sorted arrays
__device__ void merge(int* arr, int* temp, int left, int mid, int right) {
int i = left;
int j = mid + 1;
int k = left;
while (i <= mid && j <= right) {
if (arr[i] <= arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
while (i <= mid)
temp[k++] = arr[i++];
while (j <= right)
temp[k++] = arr[j++];
for (int idx = left; idx <= right; ++idx)
arr[idx] = temp[idx];
}
// Recursive merge sort
__device__ void mergeSortRecursive(int* arr, int* temp, int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSortRecursive(arr, temp, left, mid);
mergeSortRecursive(arr, temp, mid + 1, right);
merge(arr, temp, left, mid, right);
}
}
// Kernel function to start the merge sort
__global__ void mergeSort(int* arr, int* temp, int N) {
mergeSortRecursive(arr, temp, 0, N - 1);
}
int main() {
const int N = 10; // Number of elements
int arr[N] = {9, 3, 7, 1, 5, 8, 2, 4, 6, 0};
int* d_arr;
int* d_temp;
cudaMalloc(&d_arr, N * sizeof(int));
cudaMalloc(&d_temp, N * sizeof(int));
cudaMemcpy(d_arr, arr, N * sizeof(int), cudaMemcpyHostToDevice);
mergeSort<<<1, 1>>>(d_arr, d_temp, N);
cudaMemcpy(arr, d_arr, N * sizeof(int), cudaMemcpyDeviceToHost);
std::cout << "Sorted array: ";
for (int i = 0; i < N; ++i)
std::cout << arr[i] << " ";
std::cout << std::endl;
cudaFree(d_arr);
cudaFree(d_temp);
return 0;
}