CS Summer Hub
🏁

Algorithm Race

All four algorithms sort the same shuffled array, simultaneously, at the same speed. Watch O(n²) algorithms get lapped by O(n log n) in real time.

🫧 Bubble Sort O(n²)
📌 Insertion Sort O(n²)
🔀 Merge Sort O(n log n)
Quick Sort O(n log n)

Why does this matter for your exam?

🫧📌 O(n²) — Bubble & Insertion

These algorithms compare each element against many others. For 30 items they might make ~450 comparisons. Double the array to 60 items and comparisons quadruple to ~1,800. They're fine for small or nearly-sorted arrays, but slow down badly as data grows.

🔀⚡ O(n log n) — Merge & Quick

These use divide-and-conquer — split the problem in half, solve each half, combine. For 30 items: roughly 150 operations. For 60 items: roughly 354 — less than double, not quadruple. The gap widens dramatically with larger data.

📝 What to write in the exam

"Merge sort has a time complexity of O(n log n) which is more efficient than bubble sort's O(n²) for large datasets, as the number of comparisons grows much more slowly as n increases."

🤔 But merge sort uses more memory

Merge sort requires O(n) extra space for the temporary arrays during merging. Bubble and insertion sort are in-place (O(1) extra space). Quick sort is also in-place on average, though worst-case is O(n) stack depth.