:
Use Quicksort, select some core. This will split your list into two sub-lists, one smaller than the reference, one larger than this. If the size of the smaller sublist is <= N / 2, calculate the average a1.
If size == N/2 or size == N/2 -1
you do it immediately.
If you do not redo the large sublist, until the total size is equal to N / 2.
If size> N / 2 is shared by a smaller sublist.
Repeat all the way to the end.
PS: you do not need to sort.
source
share