简体   繁体   English

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

[英]Quicksort (with pthreads) only sorts half the array

I am doing Quicksort in parallel using pthreads.我正在使用 pthreads 并行执行 Quicksort。 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");
}

Ok, so from what I can see,好吧,就我所见,

#1 is that you set end = SIZE, which you then call partition with. #1 是您设置 end = SIZE,然后您将其称为分区。 This set high = 10, and then you access A[high], which is outside of the array.设置 high = 10,然后访问数组外的 A[high]。

#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. #2,除非我遗漏了某些东西,否则分区总是返回low的初始值,这使得pivot_idx - start = 0,这反过来调用quicksort(A, start, pivot_idx),它返回而不做任何事情,因为low = high。 Maybe you meant to set pivot_idx equal to high when you swap it?也许您打算在交换它时将 pivot_idx 设置为高?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM