Let's look at the individual parts (let n be the length of the list argument):
chunksOf 2 - O(n) , which leads to a list of length (n+1) `quot` 2 .map (sortBy ...) : Since all lists that are passed to sortBy ... have a length of <= 2 , each of these views is O(1) , and thus all map is O(n) .sortBy (\ab -> compare (last a) (head b)) : The comparison is always O(1) , since the lists whose last element is busy have a limited length ( <= 2 ), so the whole sortBy operation sortBy O(n*log n)concat again O(n) .
So we have O(n*log n) .
Please note, however, that
cmp = \ab -> compare (last a) (head b)
- an inconsistent comparison for the two lists a and b (say, [30,10] and [25,15] ), you can have
cmp ab == cmp ba = LT
I'm not sure your algorithm always works.
sortBy looked at the implementation of sortBy and following the sorting a bit in my head, I think that it works for this purpose (provided that the elements of the list are different), and an inconsistent comparison does not harm. For some sorting algorithms, inconsistent comparisons may result in sorting in a loop, but this should not happen for merge sort options.
source share