计算QuickSort算法中的基本操作
Counting basic operations in Quicksort algorithm
我正在尝试计算Hoare的QuickSort算法完成的基本操作量。我想知道我是否将计数器放在正确的位置。我的任务是计算100个随机生成的尺寸阵列10,100,1k和10k的基本操作数量。
这是带有计数器的算法(第6行):
void QuickSort(int* array, int startIndex, int endIndex, int &counter) {
int pivot = array[startIndex]; //pivot element is the leftmost element
int splitPoint;
if (endIndex > startIndex)
{
counter++; //counting
splitPoint = SplitArray(array, pivot, startIndex, endIndex);
array[splitPoint] = pivot;
QuickSort(array, startIndex, splitPoint - 1, counter); //Quick sort first half
QuickSort(array, splitPoint + 1, endIndex, counter); //Quick sort second half
}
}
void swap(int &a, int &b) {
int temp;
temp = a;
a = b;
b = temp;
}
int SplitArray(int* array, int pivot, int startIndex, int endIndex) {
int leftBoundary = startIndex;
int rightBoundary = endIndex;
while (leftBoundary < rightBoundary)
{
while (pivot < array[rightBoundary]
&& rightBoundary > leftBoundary)
{
rightBoundary--;
}
swap(array[leftBoundary], array[rightBoundary]);
while (pivot >= array[leftBoundary]
&& leftBoundary < rightBoundary)
{
leftBoundary++;
}
swap(array[rightBoundary], array[leftBoundary]);
}
return leftBoundary;
}
这些结果有意义吗?
Array[Amount] Array[10] Array[100] Array[1k] Array[10k]
MAX: 8 72 682 7122
MIN: 5 63 653 7015
AVERAGE: 6.36 66.54 667.87 7059.41
或我把计数器放在错误的地方。
计数器位于错误的位置。您的任务是计算基本操作。排序的基本操作是什么?通常,我们计算比较操作的数量以测量排序的复杂性。
我们知道,QuickSort平均为O(n log n),而n是要分类的项目数,而最坏的情况是O(n^2)。
您的数字小于n,这是不可能的任何东西,所以您无法保证它被排序)。
在您的算法中,当您将数组的元素与枢轴值进行比较时,会发生比较操作。因此,每次将数组元素与枢轴进行比较时,都会增加计数器。您的测量数字至少应为n,通常大约是n*log n,很少接近n^2。
请参阅以下建议的点 splitarray 在哪里增加计数器:
void QuickSort(int* array, int startIndex, int endIndex, int &counter) {
int pivot = array[startIndex]; //pivot element is the leftmost element
int splitPoint;
if (endIndex > startIndex)
{
// counter++; // Don't count here
splitPoint=SplitArray(array, pivot, startIndex, endIndex, counter);
array[splitPoint] = pivot;
QuickSort(array, startIndex, splitPoint - 1, counter); //Quick sort first half
QuickSort(array, splitPoint + 1, endIndex, counter); //Quick sort second half
}
}
没有更改交换:
void swap(int &a, int &b) {
int temp;
temp = a;
a = b;
b = temp;
}
splitarray进行比较,因此计数器应在此处增加:
int SplitArray(int* array,int pivot,int startIndex,int endIndex,int &counter) {
int leftBoundary = startIndex;
int rightBoundary = endIndex;
while ((++counter) && (leftBoundary < rightBoundary))
{
while (pivot < array[rightBoundary]
&& rightBoundary > leftBoundary)
{
rightBoundary--;
}
swap(array[leftBoundary], array[rightBoundary]);
while ((++counter) && (pivot >= array[leftBoundary])
&& leftBoundary < rightBoundary)
{
leftBoundary++;
}
swap(array[rightBoundary], array[leftBoundary]);
}
return leftBoundary;
}
相关文章:
- 像union_这样的 Boost.Geometry 操作如何处理浮点类型的基本不精确性?
- 在矢量数组C++中使用算法库操作
- OpenCV - 基本操作 - 性能问题 [模式:发布]
- 基本代数操作已知大小数组
- 基本字符串操作有问题 [c++, Visual Studio 2013]
- 计算QuickSort算法中的基本操作
- 罗马数字基本算法中基于字符串的错误(使用std::map)
- 基本算法优化
- 程序执行基本操作所花费的总时间
- 计算给定程序的基本操作
- 执行不同操作的模块函数的算法
- 用于创建、可视化和算法操作数据结构的C++库
- 尝试学习c++中的基本文件操作
- 基本深度复制(操作重载)
- OpenCV 中的基本颜色缩减算法
- 排序算法不适用于使用指针操作的模板
- 在 c++ 中实现基本的 MD5 算法
- 为什么我的基本优先级队列排序算法会导致 seg 错误
- 为什么这个 Java 代码不做与此C++代码相同的操作?(基本操作;C++输出正确)
- 快速排序算法中的计数基本操作