简体   繁体   English

回归n log(n)排序

[英]Regressive n log(n) sorting

I have a sorting algorithm that requires performing an n log n sort n times, with n decrementing by 1 in each step? 我有一个排序算法,需要执行n log n排序n 1中的每个步骤的时间,其中n递减? In other words: I would expect a performance of O(n log n + (n-1) log (n-1) + (n-2) log (n - 2) + ... ) . 换句话说:我期望性能为O(n log n + (n-1) log (n-1) + (n-2) log (n - 2) + ... ) This problem definitely doesn't strike me as something that hasn't been encountered before so I must ask: 这个问题肯定不会让我感觉像以前没有遇到的事情所以我必须要问:

What is the order-of-magnitude performance of an n log n sort n times, with n decrementing by 1 in each step? n log n排序n次的数量级性能是多少,每一步n递减1?

N^2 log(N) N ^ 2 log(N)

You are doing a NlogN operation N times so N times NlogN is the solution. 您正在进行N次NlogN操作N次,因此N次NlogN是解决方案。

Oh after your edit it's quite different. 哦,你的编辑后,它是完全不同的。 It's very hard to figure out that summation but it's upper bound (which is all big O is) is still N^2 log(N). 很难弄清楚总和,但它的上限(这是大O都是)仍然是N ^ 2 log(N)。 You may be able to figure out a closer upper bound but I think that would be a viable solution. 你可能能够找到更接近的上限,但我认为这将是一个可行的解决方案。

See https://math.stackexchange.com/questions/135787/asymptotic-formula-for-the-logarithm-of-the-hyperfactorial for a much more exact solution. 有关更精确的解决方案,请参阅https://math.stackexchange.com/questions/135787/asymptotic-formula-for-the-logarithm-of-the-hyperfactorial

The much more exact solution is still bounded above by N^2 logN (by quite a bit) so I think it is still a safe upper bound. 更精确的解决方案仍然受到N ^ 2 logN(相当多)的限制,所以我认为它仍然是一个安全的上限。

It's about 0.5 * n 2 * log(n): 它大约是0.5 * n 2 * log(n):

#include <stdio.h>
#include <math.h>

#define MAX 100
double sum[1 + MAX];

int main(void)
{
  int i;

  sum[0] = 0;

  for (i = 1; i <= MAX; i++)
  {
    sum[i] = sum[i - 1] + i * ceil(log2(i));
    printf("%i %.0f %.0f\n", i, sum[i], ceil(log2(i) * i * i / 2));
  }

  return 0;
}

Output ( ideone ): 输出( ideone ):

1 0 0
2 2 2
3 8 8
4 16 16
5 31 30
6 49 47
7 70 69
8 94 96
9 130 129
10 170 167
...
90 25871 26293
91 26508 26946
92 27152 27608
93 27803 28279
94 28461 28959
95 29126 29647
96 29798 30344
97 30477 31050
98 31163 31764
99 31856 32488
100 32556 33220
Time = SUM { k log k } for k: 1..n = log(H(n)) ~ Θ(log(H(n)))

H(n): Hyper-Factorial function in n H(n):n中的超因式函数

Asymptotic Approximation: 渐近逼近:

I'll try to deduce f(n) as an approximation for an upper bound, by generalizing k .. 我将通过推广k来尝试推导f(n)作为上界的近似值。

f(n) = log n * SUM { k } for k: 1..n

f(n) = log n * 1/2 n (n+1)

f(n) = 1/2 n log n (n+1)

O(f(n)) = O(1/2 n^2 log n (n+1))

~ O(n^2 log n)

I'll try to deduce g(n) as an approximation for a lower bound, by generalizing log(k) .. 我将通过推广log(k)来尝试推导g(n)作为下界的近似值。

g(n) = n * SUM { log(k) } for k: 1..n

g(n) = n * log(1/2 n(n+1))

g(n) = n * (log(1/2) + log(n) + log(n+1))

g(n) = n * (c + log(n) + log(n+1))

g(n) = n * (c + log(n(n+1)))

Ω(g(n)) = Ω(n * (c + log(n^2+n))) = Ω(n * log(n^2+n))

~ Ω(n log(n^2+n))

So, we have: 所以,我们有:

Ω(n log(n^2+n)) < Θ(log(H(n))) < O(n^2 log n)

Example: 例:

n = 100; Ω(922.02) < Θ(20,756.7) < O(46,051.7)

n = 1000; Ω(1.38 × 10^4) < Θ(3.2 × 10^6) < O(6.9 × 10^6)

Note : f(n) and g(n) are asymptotic approximation for bounds, they're not accurate .. 注意 :f(n)和g(n)是边界的渐近逼近,它们不准确。

It's Theta(N^2 log N) . 它是Theta(N^2 log N)

It's obviously O(N^2 log N) . 这显然是O(N^2 log N)

To show that it's Omega(N^2 log N) , consider only the big half of the sequence, where each value of k is at least N/2 . 为了表明它是Omega(N^2 log N) ,只考虑序列的大半部分,其中k每个值至少为N/2 For simplicity, assume that N is even. 为简单起见,假设N是偶数。

Sum[k=1..N](k log k) >= Sum[k=N/2..N](k log k)           ; drop the small half
                     >= Sum[k=N/2..N]((N/2) log (N/2))   ; since each k >= N/2
                     >= N/2 * N/2 * log (N/2)
                      = N^2/4 * (log N - log 2)
                      = N^2/4 * logN - c
                      ∈ Omega(N^2 log N)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM