Reputation: 1
I am doing Quicksort in parallel using pthreads. The problem is that only the second half of the array is sorted.
I suspect there might be a problem in the partition function, but I do not know how to debug this issue. I have added my complete code.
Can someone please point me in the right direction?
#include <pthread.h>
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <cstdlib>
#include <stdlib.h>
#include <time.h>
using namespace std::chrono;
using namespace std;
#define SIZE 10
#define MAXTHREAD 8
#define swap(A, a, b) {unsigned tmp; tmp=A[a]; A[a]=A[b]; A[b]=tmp;}
#define THRESHOLD SIZE/MAXTHREAD
static int A[SIZE];
static int partition(int *A, int low, int high, int pivot_idx);
void read();
void *qsort(void *arg);
static void quick_sort(int *A, int low, int high);
void print();
pthread_t pt[MAXTHREAD];
int main(int argc, char* argv[])
{
// double begin,end;
int i,rc;
rc = 0;
i = 0;
pthread_mutex_t lok1;
pthread_cond_t cond1;
void *exit_status;
printf("CALLING THE READ FUNCTION\n");
read();
printf("CALLING THE PRINT FUNCTION\n");
print();
printf("CALLING THE SORT FUNCTION\n");
pthread_mutex_init(&lok1, NULL);
pthread_cond_init(&cond1,NULL);
auto start = high_resolution_clock::now();
pthread_create(&pt[i],NULL, qsort,(void*)i);
if(rc = pthread_create(&pt[i],NULL, qsort,(void*)i))
{
printf("%THREAD FAILED\n", i);
}
pthread_join(pt[i], &exit_status);
printf("\n");
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout <<"\n" << "Duration: " << duration.count() << " microseconds" << endl;
printf("THREAD %d EXITED \n", 1);
pthread_mutex_destroy(&lok1);
pthread_cond_destroy(&cond1);
print();
return 0;
}
void *qsort(void *arg)
{
int i, pivot_idx, rc, start, end;
i = *((int*)(&arg));
start = 0;
end=SIZE;
void *exit_status;
printf("%d THREAD CREATED WITH I: %d\n,i");
if (start >= end)
{
return NULL;
}
else
{
pivot_idx = (start+end/2);
pivot_idx = partition(A, start, end, pivot_idx);
if((pivot_idx - start)<= THRESHOLD || (i == MAXTHREAD-1))
{
quick_sort(A, start, pivot_idx);
}
else if(((end-pivot_idx) <= THRESHOLD) || (i == MAXTHREAD-1))
{
quick_sort(A,pivot_idx,end);
}
else if(i <MAXTHREAD)
{
++i;
if(rc = pthread_create(&pt[i], NULL, qsort, (void *)i))
{
printf("%d THREAD FAILED\n",i);
}
pthread_join(pt[i],&exit_status);
}
}
return NULL;
}
static int partition(int *A, int low, int high, int pivot_idx)
{
if (pivot_idx != low)
{
swap(A,low, pivot_idx);
}
pivot_idx = low;
low++;
while (low < high)
{
if(A[low] <= A[pivot_idx])
{
low++;
}
else if(A[high] > A[pivot_idx])
{
high--;
}
else
{
swap(A, low, high);
}
}
if(high != pivot_idx)
{
swap(A, pivot_idx, high);
}
return pivot_idx;
}
static void quick_sort(int *A, int low, int high)
{
int pivot_idx;
if(low >= high)
{
return;
}
else
{
pivot_idx = (low+high/2);
pivot_idx = partition(A, low, high, pivot_idx);
if(low < pivot_idx)
{
quick_sort(A, low, pivot_idx-1);
}
if(pivot_idx < high)
{
quick_sort(A, pivot_idx+1, high);
}
}
}
void read()
{
int i;
for(i=0; i<SIZE; i++)
{
A[i] = rand()%10 +1;
}
}
void print()
{
int i, chunk;
chunk = SIZE/MAXTHREAD;
for(i=0; i<SIZE; i++)
{
if(i%chunk == 0)
{
printf("|");
}
printf(" %d ", A[i]);
}
printf("\n\n");
}
Upvotes: 0
Views: 145
Reputation: 31
Ok, so from what I can see,
#1 is that you set end = SIZE, which you then call partition with. This set high = 10, and then you access A[high], which is outside of the array.
#2, unless I'm missing something partition always returns the initial value of low, which makes pivot_idx - start = 0, which in turn calls quicksort(A, start, pivot_idx), which returns without doing anything as low = high. Maybe you meant to set pivot_idx equal to high when you swap it?
Upvotes: 1