0

我想在一个 trhead 中对我的字符串进行排序,但是我的程序一直在崩溃而没有有用的消息。

   void FarmSortWorker::processSort()
    {
    // FramContainer *park =& farm_container[i];
    //park->setFarmName("test");
        QMutex mutex;
        mutex.lock();
        quicksort(2, 4);

        mutex.unlock();
        emit finished();
    }

    void FarmSortWorker::quicksort(int leftIdx, int rightIdx)
    {
        if (leftIdx >= 0 && leftIdx <= farm_container.size() && rightIdx > leftIdx && rightIdx <= farm_container.size())
        {
            if(leftIdx < rightIdx)
            {
                int pivotIdx = divide(leftIdx, rightIdx);
                quicksort(leftIdx, pivotIdx - 1);
                quicksort(pivotIdx + 1, rightIdx);
            }
        }
    }

    void FarmSortWorker::swap(int leftIdx, int rightIdx)
    {
        if (leftIdx >= 0 && leftIdx <= farm_container.size() && rightIdx > leftIdx && rightIdx <= farm_container.size())
        {
            FramContainer temp = farm_container[leftIdx];
            farm_container[leftIdx] = farm_container[rightIdx];
            farm_container[rightIdx] = temp;
        }
    }


    int FarmSortWorker::divide(int leftIdx, int rightIdx)
    {
        int l = leftIdx;
        if (leftIdx >= 0 && leftIdx <= farm_container.size() && rightIdx > leftIdx && rightIdx <= farm_container.size())
        {
            int r = rightIdx - 1;
            int pivot = (int) farm_container[rightIdx].getFarmName().at(0).toLatin1();

            while (l <= r)
            {
                if((int) farm_container[l].getFarmName().at(0).toLatin1() < pivot)
                {
                    ++l;
                }
                else
                {
                    this->swap(l, r);
                    --r;
                }
            }
            this->swap(l, rightIdx);
        }
        return 1;
    }


void FarmSortWorker::processSort()
{
// FramContainer *park =& farm_container[i];
//park->setFarmName("test");
    QMutex mutex;
    mutex.lock();
    quicksort(2, 4);

    mutex.unlock();
    emit finished();
}

void FarmSortWorker::quicksort(int leftIdx, int rightIdx)
{
    if (leftIdx >= 0 && leftIdx <= farm_container.size() && rightIdx > leftIdx && rightIdx <= farm_container.size())
    {
        if(leftIdx < rightIdx)
        {
            int pivotIdx = divide(leftIdx, rightIdx);
            quicksort(leftIdx, pivotIdx - 1);
            quicksort(pivotIdx + 1, rightIdx);
        }
    }
}

void FarmSortWorker::swap(int leftIdx, int rightIdx)
{
    if (leftIdx >= 0 && leftIdx <= farm_container.size() && rightIdx > leftIdx && rightIdx <= farm_container.size())
    {
        FramContainer temp = farm_container[leftIdx];
        farm_container[leftIdx] = farm_container[rightIdx];
        farm_container[rightIdx] = temp;
    }
}


int FarmSortWorker::divide(int leftIdx, int rightIdx)
{
    int l = leftIdx;
    if (leftIdx >= 0 && leftIdx <= farm_container.size() && rightIdx > leftIdx && rightIdx <= farm_container.size())
    {
        int r = rightIdx - 1;
        int pivot = (int) farm_container[rightIdx].getFarmName().at(0).toLatin1();

        while (l <= r)
        {
            if((int) farm_container[l].getFarmName().at(0).toLatin1() < pivot)
            {
                ++l;
            }
            else
            {
                this->swap(l, r);
                --r;
            }
        }
        this->swap(l, rightIdx);
    }
    r

eturn 1;
}

farm_container 是一个 QList&,大小为 900。

我该如何正确调试或错误可能是什么。

先感谢您。

4

1 回答 1

0

问题是,在我的 devide 函数中,我返回了“1”而不是“l”,因此我的枢轴元素始终为 1。

于 2013-08-16T05:19:23.730 回答