问题描述
这是一个家庭作业项目。
我的目标是通过使用递归实现快速排序,从而按名称对我的动物列表进行排序。 我已经用我的动物砝码成功完成了此任务,如下代码片段和与之相关的结果所示。
public static SortResult QuickSortByWeight(List<Animal> animals,int leftIndex,int rightIndex,SortResult sortResult)
{
// define variables to keep track of the left and right points in the list
// initialize them to the passed-in indexes
int leftPointer = leftIndex;
int rightPointer = rightIndex;
// find the animal to pivot on (the middle animal in this case)
Animal pivotAnimal = animals[(leftIndex + rightIndex) / 2];
// define and initialize a loop variable
bool done = false;
// start looping
while (!done)
{
// while the weight of the animal at the left pointer spot in the list is less than the pivot animal's weight
while (animals[leftPointer].Weight < pivotAnimal.Weight)
{
// increment the left pointer
leftPointer++;
// increment the sort result's compare count
sortResult.CompareCount++;
}
// while the pivot animal's weight is less than the weight of the animal at the right pointer spot in the list
while (pivotAnimal.Weight < animals[rightPointer].Weight)
{
// decrement the right pointer
rightPointer--;
// increment the sort result's compare count
sortResult.CompareCount++;
}
// if the left pointer is less than or equal to the right pointer
if (leftPointer <= rightPointer)
{
// swap the animals at the left pointer and right pointer spots
SortHelper.Swap(animals,leftPointer,rightPointer);
// increment the sort result's swap count
sortResult.SwapCount++;
// then increment the left pointer and decrement the right pointer
leftPointer++;
rightPointer--;
}
// if the left pointer is greater than the right pointer,// stop the outer while loop
if (leftPointer > rightPointer)
{
done = true;
}
}
// if the left index is less than the right pointer
// use recursion to sort the animals within the left index and right pointer
if (leftIndex < rightPointer)
{
// method call here
QuickSortByWeight(animals,leftIndex,rightPointer,sortResult);
}
//// if the left pointer is less than the right index
//// use recursion to sort the animals within the left pointer and right index
if (leftPointer < rightIndex)
{
// method call here
QuickSortByWeight(animals,rightIndex,sortResult);
}
sortResult.Animals = animals;
return new SortResult { SwapCount = sortResult.SwapCount,CompareCount = sortResult.CompareCount,ElapsedMilliseconds = sortResult.ElapsedMilliseconds,Animals = animals };
}
我认为,按名称快速排序时,可以将.Compareto与当前的动物名称以及按重量排序的相同逻辑结合使用,但是对于使用以下代码的IndexOutOfRange异常,我却没有成功:
public static SortResult QuickSortByName(List<Animal> animals,SortResult sortResult)
{
// define variables to keep track of the left and right points in the list
// initialize them to the passed-in indexes
int leftPointer = leftIndex;
int rightPointer = rightIndex;
// find the animal to pivot on (the middle animal in this case)
Animal pivotAnimal = animals[(leftIndex + rightIndex) / 2];
// define and initialize a loop variable
bool done = false;
// start looping
while (!done)
{
// while the name of the animal at the left pointer spot in the list is less than the pivot animal's name
while (animals[leftPointer].Name.Compareto(pivotAnimal.Name) < leftPointer)
{
// increment the left pointer
leftPointer++;
// increment the sort result's compare count
sortResult.CompareCount++;
}
// while the pivot animal's name is less than the name of the animal at the right pointer spot in the list
while (pivotAnimal.Name.Compareto(animals[rightPointer].Name) < rightPointer)
{
// decrement the right pointer
rightPointer--;
// increment the sort result's compare count
sortResult.CompareCount++;
}
// if the left pointer is less than or equal to the right pointer
if (leftPointer <= rightPointer)
{
// swap the animals at the left pointer and right pointer spots
SortHelper.Swap(animals,rightPointer);
// increment the sort result's swap count
sortResult.SwapCount++;
// then increment the left pointer and decrement the right pointer
leftPointer++;
rightPointer--;
}
// if the left pointer is greater than the right pointer,// stop the outer while loop
if (leftPointer > rightPointer)
{
done = true;
}
}
// if the left index is less than the right pointer
// use recursion to sort the animals within the left index and right pointer
if (leftIndex < rightPointer)
{
// method call here
QuickSortByName(animals,sortResult);
}
//// if the left pointer is less than the right index
//// use recursion to sort the animals within the left pointer and right index
if (leftPointer < rightIndex)
{
// method call here
QuickSortByName(animals,Animals = animals };
}
如果我能从这次讨论中了解到为什么我的语法在按名称排序时出错,我要感谢您的见解。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)