What is the complexity of this specialized sorting

I would like to know the complexity (as in O (...)) of the following sorting algorithm:

  • There are B barrels
  • which contain a total of N elements, are distributed unevenly over the barrels.
  • Elements in each barrel are already sorted.

Sorting combines all the elements from each trunk in one sorted list:

  • using an array of size B to store the last sorted element of each trunk (starting at 0)
  • check each barrel (at the last stored index) and find the smallest element
  • copy the element in the final sorted array, increase the counter of the array
  • add the last sorted element for the trunk that we selected from
  • complete these steps N times

or in pseudo code:

for i from 0 to N
    smallest = MAX_ELEMENT
    foreach b in B
        if bIndex[b] < b.length && b[bIndex[b]] < smallest
            smallest_barrel = b
            smallest = b[bIndex[b]]
    result[i] = smallest
    bIndex[smallest_barrel] += 1

, O (n), , , , , B , , N , B-. , , ?

+3
2

, . 2 . N-1 , | B |, | B | - B ( ). (N-1) * | B | O (NB).

+6

, . . , , , B. , O (B) N

+1

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


All Articles