簡體   English   中英

快速排序(使用 pthreads)只對數組的一半進行排序

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

我正在使用 pthreads 並行執行 Quicksort。 問題是只對數組的后半部分進行了排序。

我懷疑分區函數可能有問題,但我不知道如何調試這個問題。 我已經添加了我的完整代碼。

有人可以指出我正確的方向嗎?

#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");
}

好吧,就我所見,

#1 是您設置 end = SIZE,然后您將其稱為分區。 設置 high = 10,然后訪問數組外的 A[high]。

#2,除非我遺漏了某些東西,否則分區總是返回low的初始值,這使得pivot_idx - start = 0,這反過來調用quicksort(A, start, pivot_idx),它返回而不做任何事情,因為low = high。 也許您打算在交換它時將 pivot_idx 設置為高?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM