You will need steps n (2n-1) (in the worst case). Here is an intuitive explanation:
Suppose there are 4 sorted groups of size (n / 2). Let us call them A, B, C, D. We also assume that each of these groups is sorted, that the initial input vector is DCBA and that the final sorted vector must be ABCD.
In each individual operation, we can change the order of two groups (for example, change BA to AB).
DCBA sorting requires the following steps:
DCBA ā CDAB (2 steps) ā CADB (1 step) ā ACBD (2 steps) ā ABCD (1 step)
Total steps: 6 = 4 * 3/2
Now we support that you need to sort FEDCBA:
FEDCBA ā EFCDAB (3 steps) ā ECFADB (2 steps) ā CEAFBD (3 steps) ā CAEBFD (2 steps) ā ACBEDF (3 steps) ā ABCDEF (2 steps)
Total steps: 15 = 6 * 5/2
And so on....
To sort x blocks of size (n / 2), you need x (x-1) / 2 steps (each step sorts n consecutive elements).
n² are 2n * (n / 2) blocks, so you need steps (2n) (2n-1) / 2 = n (2n-1).
Edit:
What if one n-sorter (F) can sort arbitrary elements (not necessarily sequential)?
This turns out to be a research level problem related to sorting networks . See also here .
Take a look at this recent article by Shea, Jan, and Wag :
In this paper, we propose an n-dimensional merge algorithm that generalizes the odd-even merge using n-sorters as the main building blocks, where n (ā„ 2) is simple. Based on this merge algorithm, we also offer a sorting algorithm. For input values āāN = n ^ p, p + ān / 2ā Ć p (p-1) / 2 steps are necessary. The complexity of the sorting network by the total number of n-sorters is estimated. Closed form expression for The number of sorters is also determined.