30
3
Merge sort is a sorting algorithm which works by splitting a given list in half, recursively sorting both smaller lists, and merging them back together to one sorted list. The base case of the recursion is arriving at a singleton list, which cannot be split further but is per definition already sorted.
The execution of the algorithm on the list [1,7,6,3,3,2,5]
can be visualized in the following way:
[1,7,6,3,3,2,5]
/ \ split
[1,7,6,3] [3,2,5]
/ \ / \ split
[1,7] [6,3] [3,2] [5]
/ \ / \ / \ | split
[1] [7] [6] [3] [3] [2] [5]
\ / \ / \ / | merge
[1,7] [3,6] [2,3] [5]
\ / \ / merge
[1,3,6,7] [2,3,5]
\ / merge
[1,2,3,3,5,6,7]
The Task
Write a program or function which takes a list of integers in any reasonable way as input and visualizes the different partitions of this list while being sorted by a merge sort algorithm. This means you don't have to output a graph like above, but just the lists are fine:
[1,7,6,3,3,2,5]
[1,7,6,3][3,2,5]
[1,7][6,3][3,2][5]
[1][7][6][3][3][2][5]
[1,7][3,6][2,3][5]
[1,3,6,7][2,3,5]
[1,2,3,3,5,6,7]
Furthermore, any reasonable list notation is fine, therefore the following would also be a valid output:
1 7 6 3 3 2 5
1 7 6 3|3 2 5
1 7|6 3|3 2|5
1|7|6|3|3|2|5
1 7|3 6|2 3|5
1 3 6 7|2 3 5
1 2 3 3 5 6 7
Finally, the way to split a list in two smaller lists is up to you as long as the length of both resulting lists differs at most by one. That means instead of splitting [3,2,4,3,7]
into [3,2,4]
and [3,7]
, you could also split by taking elements at even and odd indexes ([3,4,7]
and [2,3]
) or even randomize the split every time.
This is code-golf, so the shortest code in any language measured in bytes wins.
Test cases
As noted above, the actual format and the way to split lists in half is up to you.
[10,2]
[10][2]
[2,10]
[4,17,1,32]
[4,17][1,32]
[4][17][1][32]
[4,17][1,32]
[1,4,17,32]
[6,5,4,3,2,1]
[6,5,4][3,2,1]
[6,5][4][3,2][1]
[6][5][4][3][2][1]
[5,6][4][2,3][1] <- Important: This step cannot be [5,6][3,4][1,2], because 3 and 4 are on different branches in the the tree
[4,5,6][1,2,3]
[1,2,3,4,5,6]
Is it a requirement to actually implement merge sort, or only visualize it? – JungHwan Min – 2017-12-14T23:03:20.567
@JungHwanMin The program needs to be able to handle arbitrary integer lists, but you are not required to actually implement merge sort. – Laikoni – 2017-12-14T23:11:22.697
What's the difference between implementing and visualizing merge sort if at the end of the visualization you have a sorted list? – dylnan – 2017-12-14T23:30:22.530
5@dylnan You can use another sorting algorithm or a built in sort function to do the sorting... – flawr – 2017-12-14T23:31:05.280
5Some golfing idea: the bottom half of the result can be generated by sorting each sublist in the first half and reversing the order. – JungHwan Min – 2017-12-15T00:46:10.427
is 321,3/21,3/2/1,23/1,123 allowed? – l4m2 – 2017-12-15T07:49:23.190
@JungHwan Min Just sort the whole array holding the spliting shuffle the array and cut by half – l4m2 – 2017-12-15T08:48:53.953
2@Arnauld The
[[1,2],[3],[4,5],[6]]
stage is actually the correct solution, as merge sort is working recursively. That is if we start with[1,2,3,4,5,6]
and split it into[1,2,3]
and[4,5,6]
, then those lists are independently processed until they are merged in the final step. – Laikoni – 2017-12-15T11:10:02.9072@l4m2 Ok, final try for an answer: 1. you need delimiters because also integers >9 should be supported. 2. This is not valid for the same reason as given in my comment above. If we split into
[3]
and[2,1]
, then those are on different branches, so we can't merge[3]
and[2]
after[2,1]
is split into[2]
and[1]
. – Laikoni – 2017-12-15T11:34:26.030Can the input be split into non-contiguous sublists? One answer seems to do that. – Zgarb – 2017-12-15T18:49:00.077
@Zgarb
Finally, the way to split a list in two smaller lists is up to you as long as the length of both resulting lists differs at most by one.
Does this answer your question or am I misunderstanding it? – Laikoni – 2017-12-15T19:04:15.2971In fact the sentence after that answers my question exactly. Sorry for missing that. :P – Zgarb – 2017-12-15T19:05:59.290
Few days ago there was a Jelly answer. Where is it now? – jimifiki – 2017-12-20T16:58:17.663
@jimifiki It was deleted because it did not work for some testcases, e.g.
– Laikoni – 2017-12-20T17:04:55.887[1,2,3,4,5,6]
: Try it online!