我正在尝试运行此代码以快速排序而无需递归,它在我输入一个 char 数组时运行,但是当我尝试输入字符串时,它在输出中返回符号而不是字符串。
因此,如果有人知道我在这段代码中犯了什么错误,我将不胜感激。因为我的计划是读取文件txt并将信息表单文件存储在一个数组中,然后在快速排序中输入这个数组。
此代码在 CUDA 中实现。
void quick_p(char c[][30], int count)
{
int partitions[1024];
int newpartitions[1024];
partitions[0]=0;
partitions[1]=count-1;
int threads=1;
char *dev_c;
cudaMalloc((void**)&dev_c, count * sizeof( char));
int *dev_p;
int *dev_pn;
cudaMalloc((void**)&dev_p , 1024 * sizeof(int));
cudaMalloc((void**)&dev_pn, 1024 * sizeof(int));
cudaMemcpy(dev_c, c, count * sizeof( char), cudaMemcpyHostToDevice);
while(true)
{
cudaMemcpy(dev_p, partitions , sizeof(partitions), cudaMemcpyHostToDevice);
cudaMemcpy(dev_pn, newpartitions , sizeof(newpartitions), cudaMemcpyHostToDevice);
Split<<<1,threads>>>(&dev_c,dev_p,dev_pn,threads);
// get result back and loop again
cudaMemcpy(newpartitions,dev_pn, sizeof(partitions), cudaMemcpyDeviceToHost);
int tmp=0;
for(int i=0;i<threads*2;i++)
{
int idx=i*2;
if (newpartitions[idx]<newpartitions[idx+1] && newpartitions[idx+1]-newpartitions[idx]>=1)
{
partitions[tmp]=newpartitions[idx];
partitions[tmp+1]=newpartitions[idx+1];
tmp+=2;
}
}
threads=tmp/2;
if (threads==0)
break;
} // end main loop
cudaMemcpy(c,dev_c, count* sizeof(unsigned char), cudaMemcpyDeviceToHost);
cudaFree(dev_c);
cudaFree(dev_p);
cudaFree(dev_pn);
}