Why do my binary heap inserts behave this way in practice?

I implemented in C ++ an array-based binary heap and a pointer-based binary heap. I am starting a small experiment where I made n inserts for different input sizes n. Elements are of type int32_t, and each of them is selected evenly randomly (with mersenne twister) from

{1,...,std::numeric_limits<int32_t>::max()}

So, I run each experiment 10 times and take the average processor time it takes to complete the experiment.

To calculate the processor time, I used the following functions:

clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);

and

clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);

Here is the working time

enter image description here

It seems to me that inserting n elements takes linear time instead of nlogn time. If I divide the runtime by n, I get the following graph:

enter image description here

Both runtimes converge to a constant. Thus, this confirms my assumption.

? ? O (logn)?

+4
4

, O(n), ( ) O(n log n). , , -, , -, - - R.W. Floyd.

, O (1), , . , .

, O (1), , , 0,5. , ; , - ; . , 0 + 0,5 + 0,25 +... == 1.

, . , . , , O (1). ; - " " (1991) , - .

Floyd heapify , , ( ) - . ., , 1999 : " , .


:

. , , , PRNG , .

, , .

, O (log n) O (1) n; c 1 O (1) + c 2 O (log n), c 1 c 2, O (1).

+1

, nlog (n) n.

O (N log N) - ?

0

, O(nlog(n))

  • f(n). , log(100000) , y, 6e+6.
  • f(n)/n. , . , log2(10000) = 13.9 log2(125000) = 16.9. 1,2 . , .

, :

  • n
  • , , .. {2^0, 2^1,...,2^p,..., 2^n}. , x, , .

, , nlog(n)

0

, n, , aheap, , n , ( ), , . , , , O(n log n), log n , .

, aheap , 25000 125000. log(n) 16% (ln(125000)/ln(25000)=1.1589...). .

0

Source: https://habr.com/ru/post/1611281/


All Articles