PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Python · Theory

Merge Sort in Python

← All stacks

Theory

81/268

Merge Sort in Python

Merge Sort orders a list. Say the idea in plain words, then complexity. Bubble/insertion/selection are O(n²) teaching sorts. Merge and heap are O(n log n). Python’s built-in sort is Timsort (O(n log n), very fast in practice).

Dry-run Merge Sort on a tiny list like [4, 1, 3, 2] on the board. Interviewers care that you can trace one pass, not that you recite a textbook page.

For Merge Sort in real code: sorted(a) / a.sort() is what you use. Write the algorithm only when they ask you to implement it.

Merge Sort in Python — output — [1, 2, 3, 4]. Split, sort halves, merge. O(n log n).

Exam tip

Idea + complexity + one tiny trace. Then say when to use built-in sort.

Example

# Merge Sort in Python
def merge(a):
    if len(a) <= 1:
        return a
    mid = len(a) // 2
    L, R = merge(a[:mid]), merge(a[mid:])
    o, i, j = [], 0, 0
    while i < len(L) and j < len(R):
        if L[i] <= R[j]:
            o.append(L[i]); i += 1
        else:
            o.append(R[j]); j += 1
    return o + L[i:] + R[j:]
print(merge([4, 1, 3, 2]))

Merge Sort in Python — output: [1, 2, 3, 4]. Split, sort halves, merge. O(n log n).

Short notes

  • DefMerge Sort puts items in order.
  • RuleMerge Sort — know idea + O(...).
  • RememberMerge Sort — real code uses sorted() / Timsort.
  • TrapMerge Sort — implementing O(n²) in production when sorted() exists.

Questions

1

Explain Merge Sort as if you are teaching a junior — definition, then one tiny script.

2

What does the example print, and why?

3

What mistake do freshers make with Merge Sort?

Previous← Selection Sort in PythonNextQuick Sort in Python →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.