Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 快速排序算法中的基本运算计数_C++_Sorting - Fatal编程技术网

C++ 快速排序算法中的基本运算计数

C++ 快速排序算法中的基本运算计数,c++,sorting,C++,Sorting,我试图计算霍尔的快速排序算法所完成的基本操作的数量。我想知道我是否把柜台放在正确的位置。我的任务是统计100个随机生成的大小为10100、1k和10k的数组上的基本操作数 以下是放置计数器的算法(第6行): 或者我把柜台放错地方了。柜台放错地方了。你的任务是计算基本操作。排序的基本操作是什么?通常,我们计算比较操作的数量,以测量排序的复杂性 我们知道快速排序平均是O(N logn),其中N是被排序的项目数,而最坏的情况是O(N^2) 您的数字小于N,这是不可能的,因为N项中的每一项都必须与其他元

我试图计算霍尔的快速排序算法所完成的基本操作的数量。我想知道我是否把柜台放在正确的位置。我的任务是统计100个随机生成的大小为10100、1k和10k的数组上的基本操作数

以下是放置计数器的算法(第6行):


或者我把柜台放错地方了。

柜台放错地方了。你的任务是计算基本操作。排序的基本操作是什么?通常,我们计算比较操作的数量,以测量排序的复杂性

我们知道快速排序平均是O(N logn),其中N是被排序的项目数,而最坏的情况是O(N^2)

您的数字小于N,这是不可能的,因为N项中的每一项都必须与其他元素至少比较一次,排序的成本不能小于N(否则至少有一个元素没有与任何元素进行比较,因此您无法保证它已排序)

在算法中,比较操作发生在将数组元素与枢轴值进行比较时。因此,每次比较数组元素和枢轴时,都要增加计数器。您测量的数字应至少为N,通常约为N*logN,很少接近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;                              

}
int-SplitArray(int*array、int-pivot、int-startIndex、int-endIndex、int&counter){
int leftBoundary=startIndex;
int rightbundary=endIndex;
while((++计数器)&&(左边界<右边界))
{
while(枢轴<数组[右边界]
&&右边界>左边界)
{
右边界--;
}
交换(数组[leftBoundary],数组[rightBoundary]);
while((++counter)&&(pivot>=数组[leftBoundary])
&&左边界<右边界)
{
leftBoundary++;
}
交换(数组[rightBoundary],数组[leftBoundary]);
}
返回左边界;
}
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 &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;                              

}