我從CSV文件轉換成float
陣列讀取數據(1維/載體)和施加快速排序它:OpenMP的截斷浮子
int main()
{
float floatArr[2000];
ReadRandomData(floatArr, "D:\RandomData.csv");
//#pragma omp parallel
{
QuickSort(floatArr, 0,2000);
}
for(int i=0; i<2000; i++)
{
cout<<floatArr[i]<<endl;
}
_getch();
}
和輸出的東西像這樣:
但一旦我取消了#pragma omp parallel
部分,歐tput的是這樣的(其截斷我認爲):
任何人都可以解釋我爲什麼會這樣使用OpenMP以及如何解決它?提前致謝!
UPDATE
這裏是快速排序部分:
void QuickSort(float* arr, int startIndex, int endIndex)
{
int pivot = arr[startIndex]; //pivot element is the leftmost element
int splitPoint;
if(endIndex > startIndex) //if they are equal, it means there is only one element and quicksort's job here is finished
{
splitPoint = Splitarr(arr, pivot, startIndex, endIndex);
//Splitarr() returns the position where pivot belongs to
arr[splitPoint] = pivot;
QuickSort(arr, startIndex, splitPoint-1); //Quick sort first half
QuickSort(arr, splitPoint+1, endIndex); //Quick sort second half
}
}
這裏是分裂碼:
int Splitarr(float* arr, int pivot, int startIndex, int endIndex)
{
int leftBoundary = startIndex;
int rightBoundary = endIndex;
while(leftBoundary < rightBoundary) //shuttle pivot until the boundaries meet
{
while(pivot < arr[rightBoundary] //keep moving until a lesser element is found
&& rightBoundary > leftBoundary) //or until the leftBoundary is reached
{
rightBoundary--; //move left
}
swap(arr[leftBoundary], arr[rightBoundary]);
while(pivot >= arr[leftBoundary] //keep moving until a greater or equal element is found
&& leftBoundary < rightBoundary) //or until the rightBoundary is reached
{
leftBoundary++; //move right
}
swap(arr[rightBoundary], arr[leftBoundary]);
}
return rightBoundary; //leftBoundary is the split point because
//the above while loop exits only when
//leftBoundary and rightBoundary are equal
}
'QuickSort'看起來像什麼? – 1201ProgramAlarm
你也可以顯示你的'swap'方法嗎? – ScY
@ScY默認使用C++(std)給出的那個。問題不在我認爲它正確交換和列表排序,只是最終結果被截斷。 –