FAQ: Merge Sort: Python - Testing the Sort

This community-built FAQ covers the “Testing the Sort” exercise from the lesson “Merge Sort: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Sorting Algorithms

FAQs on the exercise Testing the Sort

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I refactored the code to make it shorter:

def merge_sort(items):
  if len(items) < 2:
    return items
  left_sorted = merge_sort(items[:len(items) // 2])
  right_sorted = merge_sort(items[len(items) // 2:])
  return merge(left_sorted, right_sorted)

def merge(left, right):
  result = []
  while (left and right):
    result.append(left.pop(0) if left[0] < right[0] else right.pop(0))
  result.extend(left or right)
  return result

unordered_list1 = [356, 746, 264, 569, 949, 895, 125, 455]
unordered_list2 = [787, 677, 391, 318, 543, 717, 180, 113, 795, 19, 202, 534, 201, 370, 276, 975, 403, 624, 770, 595, 571, 268, 373]
unordered_list3 = [860, 380, 151, 585, 743, 542, 147, 820, 439, 865, 924, 387]

ordered_list1 = merge_sort(unordered_list1)
ordered_list2 = merge_sort(unordered_list2)
ordered_list3 = merge_sort(unordered_list3)

print(ordered_list1)
print(ordered_list2)
print(ordered_list3)

is it really necessary to make 2 functions for this?
I came up with this instead:

lista = [5, 4, 3, 2, 1, 0]

def merge(arr):
    if len(arr) == 1:
        return arr
    
    midx = len(arr) // 2
    left = merge(arr[:midx])
    right = merge(arr[midx:])
    
    new_list = []
    while left and right:
        if left[0] < right[0]:
            new_list.append(left.pop(0))
        else:
            new_list.append(right.pop(0))
    if left:
        new_list += left
    if right:
        new_list += right
    return new_list
 
  
print(merge(lista))

would it make any difference at all in the end?

result += left if left else right

i was just wondering about the pop method time complexity (i think in this case the time complexity itself for the merge function will be N^2