18
Block shuffle sort
The block shuffle sort is a (rather artificial) method of sorting a list. It works as follows, illustrated by an example.
[6, 1, 0, 3, 2, 4, -2, -1]
Break list into contiguous blocks
[6][1, 0][3, 2, 4][-2, -1]
Sort each block
[6][0, 1][2, 3, 4][-2, -1]
Sort blocks lexicographically
[-2, -1][0, 1][2, 3, 4][6]
Concatenate
[-2, -1, 0, 1, 2, 3, 4, 6]
The partition into contiguous blocks can be chosen arbitrarily. However, not all choices of blocks will yield a sorted list at the end:
[6, 1, 0, 3, 2, 4, -2, -1]
[6, 1, 0][3, 2, 4][-2, -1]
[0, 1, 6][2, 3, 4][-2, -1]
[-2, -1][0, 1, 6][2, 3, 4]
[-2, -1, 0, 1, 6, 2, 3, 4]
If all blocks have length 1, or if there is only one block, then the result will of course be sorted. But these are rather extreme cases. In this challenge, your task is to find a balance between the number of blocks and the maximum length of a block.
The task
Your input is a nonempty list of integers L, taken in any reasonable format. Your output shall be the smallest integer N such that L can be block shuffle sorted so that the number of blocks and the length of each block are at most N.
The lowest byte count in each language wins. Standard code-golf rules apply.
Test cases
[5] -> 1
[1,2] -> 2
[0,2,1,-1] -> 3
[-1,0,2,1] -> 2
[9,3,8,2,7] -> 4
[9,2,8,3,7] -> 3
[5,9,3,7,2,4,8] -> 7
[-1,-2,1,2,-1,-2,7] -> 4
[6,1,0,3,2,4,-2,-1] -> 4
[12,5,6,-6,-1,0,2,3] -> 3
[1,0,1,0,1,0,1,0,1,0] -> 6
[1,2,1,3,1,2,3,2,4,3] -> 5
[7,7,7,7,8,9,7,7,7,7] -> 4
This gives 25. – recursive – 2018-03-04T22:35:15.613
1This is a kind of disappointing performance for stax, but I'm going to keep looking for savings. – recursive – 2018-03-04T22:36:38.353
http://staxlang.xyz/#c=%E2%95%A6%C6%92%E2%88%A9%E2%94%80%E2%95%AAZ%E2%99%80%E2%95%A6%C3%A1%E2%98%BB%E2%94%80%E2%94%8Cj%E2%80%BC+BI%E2%95%9F%CE%A9z%C2%B6%E2%8C%90%E2%95%A4%E2%96%84&i=%5B5%5D%0A%5B1%2C2%5D%0A%5B0%2C2%2C1%2C-1%5D%0A%5B-1%2C0%2C2%2C1%5D%0A%5B9%2C3%2C8%2C2%2C7%5D%0A%5B9%2C2%2C8%2C3%2C7%5D%0A%5B5%2C9%2C3%2C7%2C2%2C4%2C8%5D%0A%5B-1%2C-2%2C1%2C2%2C-1%2C-2%2C7%5D%0A%5B6%2C1%2C0%2C3%2C2%2C4%2C-2%2C-1%5D%0A%5B12%2C5%2C6%2C-6%2C-1%2C0%2C2%2C3%5D%0A%5B1%2C0%2C1%2C0%2C1%2C0%2C1%2C0%2C1%2C0%5D%0A%5B1%2C2%2C1%2C3%2C1%2C2%2C3%2C2%2C4%2C3%5D%0A%5B7%2C7%2C7%2C7%2C8%2C9%2C7%2C7%2C7%2C7%5D&a=1&m=2 – recursive – 2018-03-05T04:26:20.727
That's just ... amazing. – Weijun Zhou – 2018-03-05T10:55:27.887
Thanks. I found it amusing that the hyperlink used exactly the maximum comment size, after replacing "https://" with "http://". – recursive – 2018-03-05T18:31:32.693
That's interesting. Actually I have been thinking that the JS interpreter should be able to encode all those unicode symbols in the URL in a more efficient way ... – Weijun Zhou – 2018-03-05T18:46:03.923
Yes, I think you're right. It shouldn't be too difficult to improve it quite a bit. Also, I could have replaced commas in the input with spaces, which are also legal array separators. I think I will work on something like this for 1.0.5 – recursive – 2018-03-05T18:48:09.560
I look forward to the update and the online deployment. – Weijun Zhou – 2018-03-05T18:49:20.230
@recursive Consider using Base64? (like TIO) – user202729 – 2018-03-06T03:15:24.353
@WeijunZhou: The new version of stax has been released, but here's a 23 byte solution that doesn't use any new language features. It would have worked at the time this challenge was posted.
– recursive – 2018-03-07T04:33:30.110Well, I originally used
– Weijun Zhou – 2018-03-07T10:32:12.130:^
but I thought you changed it for good reasons ... Updated. Also, any advice for this?