C++ 快速排序(使用pthreads)只对数组的一半进行排序

C++ 快速排序(使用pthreads)只对数组的一半进行排序,c++,C++,我正在使用pthreads并行执行快速排序。问题是只对数组的后半部分进行排序 我怀疑分区函数中可能有问题,但我不知道如何调试这个问题。我已经添加了完整的代码 有人能给我指一下正确的方向吗 #include <pthread.h> #include <iostream> #include <stdio.h> #include <math.h> #include <cstdlib> #include <stdlib.h> #i

我正在使用pthreads并行执行快速排序。问题是只对数组的后半部分进行排序

我怀疑分区函数中可能有问题,但我不知道如何调试这个问题。我已经添加了完整的代码

有人能给我指一下正确的方向吗

#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");
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std::chrono;
使用名称空间std;
#定义尺寸10
#定义MAXTHREAD 8
#定义交换(A,A,b){无符号tmp;tmp=A[A];A[A]=A[b];A[b]=tmp;}
#定义阈值大小/MAXTHREAD
静态int A[大小];
静态int分区(int*A、int-low、int-high、int-pivot_-idx);
无效读取();
void*qsort(void*arg);
静态无效快速排序(int*A、int-low、int-high);
作废打印();
pthread_t pt[MAXTHREAD];
int main(int argc,char*argv[])
{
//双起点,双终点;
int i,rc;
rc=0;
i=0;
pthread_mutex_t lok1;
pthread_cond_t cond1;
作废*退出状态;
printf(“调用读取函数\n”);
read();
printf(“调用打印函数\n”);
打印();
printf(“调用排序函数\n”);
pthread_mutex_init(&lok1,NULL);
pthread_cond_init(&cond1,NULL);
自动启动=高分辨率时钟::现在();
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_状态);
printf(“\n”);
自动停止=高分辨率时钟::现在();
自动持续时间=持续时间(停止-启动);

好的,从我所看到的

#1是设置end=SIZE,然后用它调用分区。该设置为high=10,然后访问数组外部的[high]


#2,除非我遗漏了什么,否则分区总是返回初始值low,这使得pivot\u idx-start=0,这反过来调用quicksort(A,start,pivot\u idx),它返回时不执行任何操作,如low=high。可能您在交换它时想将pivot_idx设置为high?

我完全获得了您的第一个点,但我可以做些什么来纠正它?我已将pivot_idx更改为high,以便它在分区函数中返回high。这是我用于此代码的链接: