C++ 循环递归函数c++;

C++ 循环递归函数c++;,c++,infinite-loop,tail-recursion,qsort,C++,Infinite Loop,Tail Recursion,Qsort,我正在使用Qsort,基于employee.lastname。 left是我们已经运行了多少次的计数器。 emptotal,(或right)是总数。 因为我知道有5个,所以我将轴点强制为3。我的问题是,第二个递归调用在整个东西循环中,我不知道为什么它会循环。它应该向上(或向下)计数,然后遇到它的结束计数器 #include "./record.h" #include <string.h> #include <algorithm> void externalSort

我正在使用Qsort,基于
employee.lastname

left
是我们已经运行了多少次的计数器。
emptotal
,(或
right
)是总数。
因为我知道有5个,所以我将轴点强制为3。我的问题是,第二个递归调用在整个东西循环中,我不知道为什么它会循环。它应该向上(或向下)计数,然后遇到它的结束计数器

#include "./record.h"
#include <string.h>
#include <algorithm>

void externalSort(EmployeeRecord employee[],int empcount,int emptotal)
{
    int left=empcount,
        right=emptotal;    
    EmployeeRecord pivot = employee[3];    
    while (left < right) 
    {
        if (strcmp(employee[left].lastname, pivot.lastname) < 0 )
        {    
            left++; 
        }
        else if (strcmp(employee[right].lastname, pivot.lastname) < 0 )
        {
            right--; 
        }
        else 
        {
            std::swap(employee[left],employee[right]);
            left++;
            right--;
        }
    }

    if (strcmp(employee[left].lastname, pivot.lastname) < 0 )
    {
        std::swap(employee[left],employee[empcount]);
        left--;
    }
    if (strcmp(employee[right].lastname, pivot.lastname) < 0 )
    {
        std::swap(employee[right],employee[empcount]);
        right++;
    }

    if (empcount < right) externalSort(employee,empcount,right);
    if (left < emptotal) externalSort(employee,left,emptotal);
// The 2nd call is what seems to be looping, when I comment it out, 
    //the function doesn't loop.

}
#包括“/record.h”
#包括
#包括
void externalSort(EmployeeRecord employee[],int empcount,int emptotal)
{
int left=empcount,
右=全空;
EmployeeRecord pivot=员工[3];
while(左<右)
{
if(strcmp(员工[left].lastname,pivot.lastname)<0)
{    
左++;
}
else if(strcmp(雇员[右].lastname,pivot.lastname)<0)
{
对--;
}
其他的
{
std::swap(员工[左]、员工[右];
左++;
对--;
}
}
if(strcmp(员工[left].lastname,pivot.lastname)<0)
{
std::swap(员工[左]、员工[empcount]);
左--;
}
if(strcmp(雇员[右].lastname,pivot.lastname)<0)
{
std::swap(员工[右],员工[empcount]);
右++;
}
if(empcount
员工[3]
选择第四件事,而不是第三件事,不管需要排序多少件事

环路

while (left < right)
while(左<右)
将检查您告诉它要检查的项目,但轴可能不在该范围内

在你决定如何处理之后,你还有三个错误/事情要考虑

  • 你需要交换最后一个分支吗
  • 递归时,请尝试externalSort(employee、empcount、pivot_index-1)
  • 与externalSort(employee,left,emptotal)类似,您需要使用pivot索引+1

  • 上面有一些相对清晰的伪代码。您假设每次都可以使用第三点。当您递归时,可能只有不到3条语句。

    我要做的第一件事是检查您的strcmp语句是否真正执行了您认为它们应该执行的操作

    if (strcmp(employee[left].lastname, pivot.lastname) < 0 )
    {    
    left++; 
    }
    else if (strcmp(employee[right].lastname, pivot.lastname) < 0 )
    {
    right--; 
    }
    
    if(strcmp(员工[left].lastname,pivot.lastname)<0)
    {    
    左++;
    }
    else if(strcmp(雇员[右].lastname,pivot.lastname)<0)
    {
    对--;
    }
    

    我相信你左边的陈述是正确的,但是你正在检查右边索引中的姓氏是否小于轴,而你可能应该检查它是否大于轴。通常,像这样的小事情会给您带来意想不到的代码流。但是,我认为这不会解决所有问题。

    请解决您的缩进问题。那么请修改语法。有没有理由不使用
    std::sort
    ?还要注意,
    externalSort
    函数不是尾部递归tail recursion是一种递归策略,在这种策略中,函数先做一些工作,然后调用自身。“尾部”是指递归位于函数的最末端。externalSort调用它self,调用发生在尾部,这怎么不是尾部递归呢?另外,我没有使用std::sort,因为我工作的范围中不允许使用它。这里有一些更多的信息,您可以使用它们来确保算法正常工作: