 		__syncthreads();
 	}
 
-	dA[pivotIdx + bid * (K << 1) + threadIdx.x] = sharedElem[threadIdx.x];
+	if (threadIdx.x + K >= bid)
+		dA[pivotIdx + bid * (K << 1) + threadIdx.x] = sharedElem[threadIdx.x];
 }
 
 template <typename T>


	if (threadIdx.x + K < bid) {
		sharedElem[threadIdx.x] = (T)0;
		return;
	} else
		sharedElem[threadIdx.x] = dA[pivotIdx + bid * (K << 1) + threadIdx.x];



	if (threadIdx.x + k < bid) {
		sharedElem[threadIdx.x] = (T)0;
	} else
		sharedElem[threadIdx.x] = dA[pivotIdx + bid * (k << 1) + threadIdx.x];

