计算QuickSort算法中的基本操作

Counting basic operations in Quicksort algorithm

本文关键字:基本操作 算法 QuickSort 计算      更新时间:2023-10-16

我正在尝试计算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;                              
}