C ++ Qt使用Quicksort按字母顺序排序字符串

时间:2013-08-15 08:46:20

标签: c++ algorithm qt

我想在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&amp;并且大小为900。

如何正确调试此错误或错误是什么。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

问题在于,在我的设备功能中,我返回了“1”而不是“l”,因此我的pivot元素始终为1。