Reputation: 2027
I am reading a data (1-dimensional/vector) from a CSV file into a float
array and applying Quick Sort on it:
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();
}
and output is something like this:
but once I uncomment the #pragma omp parallel
part, output is something like this (its truncating I think):
Can anyone explain me why is this happening with OpenMP and how to fix it? Thanks in advance!
UPDATE
Here is QuickSort part:
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
}
}
and here is Splitting Code:
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
}
Upvotes: 2
Views: 108
Reputation: 91
There is one thing in your algorithm which I assume is causing the problem.
Change line:
int pivot = arr[startIndex];
to
float pivot = arr[startIndex];
Converting float to int truncates the decimals, as you correctly presumed. That might solve your issue.
Upvotes: 1