Changes

Jump to: navigation, search

The Bean Counters

961 bytes added, 21:33, 2 April 2018
bubble sort
=== Source Code ===
===== bubble sort =====
template <typename T>
__global__ void bubbleSort_kernel(T *d_a, const int size) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
__shared__ bool done;
do {
done = true;
if (idx < size - 1 && idx % 2 && d_a[idx] > d_a[idx + 1]) {
// * if idx + 1 less than size
// * if idx is odd count (1,3,5,7...)
// * if d_a[idx] > d_a[idx + 1]
// Then swap
//T temp = d_a[idx];
//d_a[idx] = d_a[idx + 1];
//d_a[idx + 1] = temp;
swap(d_a[idx], d_a[idx + 1]);
done = false;
}
// Wait every thread stop
__syncthreads();
 
if (idx < size - 1 && (idx + 1) % 2 && d_a[idx] > d_a[idx + 1]) {
// * if idx + 1 less than size
// * if idx is even count (0,2,4,6...)
// * if d_a[idx] > d_a[idx + 1]
// Then swap
//T temp = d_a[idx];
//d_a[idx] = d_a[idx + 1];
//d_a[idx + 1] = temp;
swap(d_a[idx], d_a[idx + 1]);
done = false;
}
// Wait every thread stop
__syncthreads();
} while (!done);
__syncthreads();
}
 
===== selection sort =====
===== insertion sort =====
18
edits

Navigation menu