gpt4 book ai didi

c++ - 为什么我的 n log(n) 堆排序比我的 n^2 选择排序慢

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:52:00 24 4
gpt4 key购买 nike

我已经实现了两种算法来对元素进行从高到低的排序。

第一个在实际 RAM 模型上花费二次时间,第二个花费 O(n log(n)) 时间。第二个使用优先级队列来减少。

这里是时间,是上述程序的输出。

  • 第一列是随机整数数组的大小
  • 第二列是 O(n^2) 技术的时间(以秒为单位)
  • 第三列是 O(n log(n)) 技术的时间(以秒为单位)

     9600  1.92663      7.58865
    9800 1.93705 7.67376
    10000 2.08647 8.19094

尽管复杂性存在巨大差异,但就所考虑的数组大小而言,第 3 列大于第 2 列。为什么会这样? C++实现优先级队列慢吗?

我在 Windows 7、Visual Studio 2012 32 位上执行了这段代码。

这是代码,

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <Windows.h>
#include <assert.h>

using namespace std;

double time_slower_sort(vector<int>& a)
{
LARGE_INTEGER frequency, start,end;
if (::QueryPerformanceFrequency(&frequency) == FALSE ) exit(0);
if (::QueryPerformanceCounter(&start) == FALSE ) exit(0);

for(size_t i=0 ; i < a.size() ; ++i)
{

vector<int>::iterator it = max_element( a.begin() + i ,a.end() ) ;
int max_value = *it;
*it = a[i];
a[i] = max_value;

}
if (::QueryPerformanceCounter(&end) == FALSE) exit(0);
return static_cast<double>(end.QuadPart - start.QuadPart) / frequency.QuadPart;
}



double time_faster_sort(vector<int>& a)
{
LARGE_INTEGER frequency, start,end;
if (::QueryPerformanceFrequency(&frequency) == FALSE ) exit(0);
if (::QueryPerformanceCounter(&start) == FALSE ) exit(0);


// Push into the priority queue. Logarithmic cost per insertion = > O (n log(n)) total insertion cost
priority_queue<int> pq;
for(size_t i=0 ; i<a.size() ; ++i)
{
pq.push(a[i]);
}

// Read of the elements from the priority queue in order of priority
// logarithmic reading cost per read => O(n log(n)) reading cost for entire vector
for(size_t i=0 ; i<a.size() ; ++i)
{
a[i] = pq.top();
pq.pop();
}
if (::QueryPerformanceCounter(&end) == FALSE) exit(0);
return static_cast<double>(end.QuadPart - start.QuadPart) / frequency.QuadPart;

}




int main(int argc, char** argv)
{
// Iterate over vectors of different sizes and try out the two different variants
for(size_t N=1000; N<=10000 ; N += 100 )
{

// initialize two vectors with identical random elements
vector<int> a(N),b(N);

// initialize with random elements
for(size_t i=0 ; i<N ; ++i)
{
a[i] = rand() % 1000;
b[i] = a[i];
}

// Sort the two different variants and time them
cout << N << " "
<< time_slower_sort(a) << "\t\t"
<< time_faster_sort(b) << endl;

// Sanity check
for(size_t i=0 ; i<=N-2 ; ++i)
{
assert(a[i] == b[i]); // both should return the same answer
assert(a[i] >= a[i+1]); // else not sorted
}

}
return 0;
}

最佳答案

the 3rd column is larger than the second for the array sizes considered.

“大 O”符号仅告诉您时间如何随着输入大小增长

你的时代是(或应该是)

A + B*N^2          for the quadratic case,
C + D*N*LOG(N) for the linearithmic case.

但完全有可能 C 比 A 大得多,当 N 足够小时时,线性代码的执行时间会更长。

使线性对数性变得有趣的是,如果您的输入从 9600 增长到 19200(加倍),对于二次算法,您的执行时间应该大约 四倍,大约八秒,而线性对数算法执行时间应该不会超过两倍。

因此执行时间比将从 2:8 变为 8:16,即二次算法现在的速度只有原来的两倍。

再次将输入的大小加倍,8:16 变为 32:32;面对大约 40,000 的输入时,这两种算法的速度同样快。

当处理 80,000 的输入大小时,比例相反:32 的四倍是 128,而 32 的两倍只有 64。128:64 意味着线性算术算法现在是另一个算法的两倍。

您应该运行大小非常​​不同的测试,可能是 N、2*N 和 4*N,以便更好地估计 A、B、C 和 D 常量。

归根结底,不要盲目依赖 Big O 分类。如果您希望输入变大,请使用它;但对于小输入,很可能是可扩展性较低的算法变得更有效。

例如here您会看到,对于较小的输入大小,较快的算法是以指数时间运行的算法,比对数时间快 数百 倍。但是一旦输入大小超过 9,指数算法的运行时间就会猛增,而另一个则不会。

您甚至可能决定实现算法的两个 版本,并根据输入大小使用其中一个。有一些递归算法可以做到这一点,并在最后一次迭代中切换到迭代实现。在图中,您可以为每个尺寸范围实现最佳算法;但最好的折衷方案是只使用两种算法,二次算法直到 N=15,然后切换到对数算法。

我找到了 hereIntrosort 的引用,其中

is a sorting algorithm that initially uses Quicksort, but switches to Heapsort when the recursion depth exceeds a level based on the logarithm of the number of elements being sorted, and uses Insertion sort for small cases because of its good locality of reference, i.e. when the data most likely resides in memory and easily referenced.

在上面的例子中,插入排序利用了内存局部性,这意味着它的B常数非常小;递归算法可能会产生更高的成本,并且具有显着的 C 值。因此,对于小型数据集,即使它们的 Big O 分类较差,更紧凑的算法也能表现良好。

关于c++ - 为什么我的 n log(n) 堆排序比我的 n^2 选择排序慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25613533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com