Performance: sorting vectors "m" with N / m elems Vs, sorting a single vector with N elements

Operation A

I have N vectors, each of which contains a certain number of unique three-dimensional points. For example: std::vector<double*> vec1;etc.

I perform a sort operation on each of the vectors, for example:

 std::sort(vec1.begin(), vec1.end(), sortCriteria());
 std::sort(vec2.begin(), vec2.end(), sortCriteria());
 std::sort(vec3.begin(), vec3.end(), sortCriteria());

Operation B

Suppose I have a vector called "all_point_vector" that contains 3D points from vec1, vec2, vec3 ...

i.e. 3D points in all_point_vector = points_in_vec1 + .... + points_in_vector3.

and I do a sort operation:

std::sort(all_point_vec.begin(), all_point_vec.end(), sortCriteria());

My question is which of the above methods (operation A or B) will be faster at all? sorting a single vector (all_point_vector) or sorting individual vectors. I'm just interested in the speed of these two operations.

+3
3

- O(n log n). N m/N , m m.

m .

+4

avakar, , , - . :

k , i- n i . N = n 1 +... + n k. O (n 1 logn 1 +... + n k logn k), O (N logN) = O ((n 1 +... + n k) logN) = O (n 1 logN +... + n k logN).

A = n 1 logn 1 +... + n k logn k

B = n 1 logN +... + n k logN

N > n i i, logN > logn i i. , B A, .. .

+3

m - , N, .

m = 1024, m log m = 1024 * 10 = 10240.

N=2 512 * 9 + 512 * 9 = 9216, 1024 9216 + 1024 = 10240, .

[In fact, at each sorting level, the number of comparisons is 1 less than the number of elements to merge, but the overall result is still O (n log n)]

ADDED: If, as you commented, you do not need to perform the merge, then the split case is faster. (Of course, in this case you can split the elements minto arrays N=mand not even sort the sort ;-)

+1
source

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


All Articles