CJCoding With Joseph
15free / daySign inCreate account

Merge Sort

Write merge_sort(values: list[int]) -> list[int] that returns a new sorted list: a list of one element or none is already sorted and is returned as it is; otherwise cut the list at mid = len(values) // 2 into the slices values[:mid] and values[mid:], sort each half by calling merge_sort on it, and combine the two sorted halves with a helper merge(left, right) that you also write: two indexes i and j walk the halves, the smaller value is appended to merged each time (the left one when they are equal) and the leftover slices left[i:] and right[j:] are added at the end. Merge sort is O(n log n) in every case. Do NOT write a main block or any top-level test code; the tests construct the calls. Input: one line of space-separated integers (possibly empty); the tests print the list your function returns.

Expected Output:

[3, 9, 10, 27, 38, 43, 82]

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Data Structures & AlgorithmsBig-O
📥 Auto-Input:
38 27 43 3 9 82 10\n
This input is automatically fed to your program's stdin
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (pr, inp, def)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.