我正在使用 Qsort,基于employee.lastname
.
left
是我们跑过多少的计数器。
emptotal
, (or 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.
}