English 中文(简体)
C+++ Quicksort 算法崩溃 [关闭]
原标题:C++ Quicksort Algorithm Crashing [closed]

我正在开发一种 c+++ 快速排序算法, 该算法将计算此类型中重大操作的数量。 它将正确排序 100 个值, 但在此之后它会陷入无休止的循环中。 任何帮助都会非常感激!

//快速求收

#include "QuickSort.h"
#include <algorithm>

using namespace std;

QuickSort::QuickSort(int max) : SortData(max)
{
    /*
    maxSize = max;
    theData = new long[max];
    */
    SortData::randomize(12);

}

QuickSort::~QuickSort(void)
{

}

_int64 QuickSort:: sort()
{
    numops = 0;
    quicksort(theData, 0, (size()-1));
    return(numops);
}

//Include counting number of operations
void QuickSort::quicksort(long theData[], int left, int right)
{
    //Default variables

    if (size() <= 1 || left >= size() || right >= size()) return; //Bounds checking

    if (left < right)
    {
        long pivot = partition(theData, left, right);

        quicksort(theData, left, pivot-1);
        quicksort(theData, pivot+1, right);
    }
}

int QuickSort::partition(long theData[], int left, int right)
{
    long pivot = theData[left];
    while (true)
    {
        while (theData[left] < pivot) left++;
        numops ++;

        while (theData[right] > pivot) right--;
        numops ++; 

        if (left < right)
        {
            swap(theData[left], theData[right]);
            numops+=3;
        }

        else
        {
            return right;
        }
    }
}

//快速时间h

#pragma once
#include "SortData.h"

class QuickSort : public SortData
{

public:
    QuickSort(int max = 100);
    ~QuickSort(void);
    _int64 sort();
private:
    _int64 numops;
    void QuickSort::quicksort(long theData[], int left, int right);
    int partition(long theData[], int left, int right);
}; 
问题回答

在您的分区函数中,分隔函数

 if (left < right)

总是真实的。 因此,您正在得到无限的循环, 而( true) 循环 。

从 SOCData.h 函数的大小() 函数中可能存在问题, 我们目前还看不到这个问题 。

由于数据是随机的,所以您不时在某些输入组中看到问题。

微小修改必须帮助:

if (left <= right) {
         if (left < right) {
             swap(theData[left], theData[right]);
             numops += 3;
         }
         left++;
         right--;
}
else {
    return right;
}

抱歉重复检查 :)

partitation 被卡住, 如果 left & lt; 右 theData[left] ==Data[right]





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签