Visualize the divide-and-conquer approach of Merge Sort with recursive
splitting and merging.
diff --git a/app/visualizer/sorting/mergesort/content.jsx b/app/visualizer/sorting/mergesort/content.jsx
index d50296e..f27f4f2 100755
--- a/app/visualizer/sorting/mergesort/content.jsx
+++ b/app/visualizer/sorting/mergesort/content.jsx
@@ -4,6 +4,293 @@ import { useTheme } from "@/app/contexts/ThemeContext";
import DailyDSAEmbed from "@/app/components/ui/DailyDSAEmbed";
import NewsletterEmbed from "@/app/components/ui/NewsletterEmbed";
import InContentAd from "@/app/components/ads/InContentAd";
+
+const BOX = 34;
+const GAP = 6;
+const SLOT = BOX + GAP;
+const PAD_X = 8;
+
+// Every level partitions the same slots, so keeping one global column per
+// element makes the parent/child connectors line up without any layout maths.
+const LevelsDiagram = ({ levels, mode, keyPrefix }) => {
+ const slots = levels[0].reduce((sum, group) => sum + group.length, 0);
+ const levelGap = 30;
+ const topPadding = 6;
+ const width = slots * SLOT - GAP + PAD_X * 2;
+ const height =
+ levels.length * BOX + (levels.length - 1) * levelGap + topPadding * 2;
+
+ const slotX = (slot) => PAD_X + slot * SLOT;
+ const levelY = (level) => topPadding + level * (BOX + levelGap);
+
+ // Walk each level once, tagging every group with the slot it starts at.
+ const placed = levels.map((groups) => {
+ let slot = 0;
+ return groups.map((values) => {
+ const start = slot;
+ slot += values.length;
+ return { values, start, end: slot - 1 };
+ });
+ });
+
+ const groupCenter = (group) =>
+ (slotX(group.start) + slotX(group.end) + BOX) / 2;
+
+ const accent = mode === "merge" ? "#10b981" : "#3b82f6";
+
+ return (
+
+ );
+};
+
+// One comparison inside a merge: the two candidates, and where the winner goes.
+const MergeStepDiagram = ({ left, right, li, ri, output, taken, keyPrefix }) => {
+ const halfGap = 26;
+ const topPadding = 22;
+ const rowGap = 40;
+ const totalSlots = left.length + right.length;
+ const width = totalSlots * SLOT - GAP + halfGap + PAD_X * 2;
+ const height = topPadding + BOX * 2 + rowGap + 20;
+
+ const leftX = (idx) => PAD_X + idx * SLOT;
+ const rightX = (idx) => PAD_X + (left.length + idx) * SLOT + halfGap;
+ const outX = (idx) => PAD_X + idx * SLOT;
+ const outY = topPadding + BOX + rowGap;
+
+ const fillFor = (isCandidate, isTaken) => {
+ if (isTaken) return "#10b981";
+ if (isCandidate) return "#f59e0b";
+ return "#3b82f6";
+ };
+
+ const renderHalf = (values, xFor, pointer, side) =>
+ values.map((value, idx) => {
+ const isCandidate = idx === pointer;
+ const isTaken = isCandidate && taken === side;
+ const consumed = idx < pointer;
+ return (
+
+
+
+ {value}
+
+
+ );
+ });
+
+ return (
+
+ );
+};
+
const Content = () => {
const { theme } = useTheme();
@@ -14,6 +301,99 @@ const Content = () => {
`Merge Sort is particularly useful when sorting linked lists (where random access is expensive) and is the algorithm of choice for many standard library sorting implementations when stability is required. It's also commonly used in external sorting where data doesn't fit in memory.`,
];
+ const divideLevels = [
+ [[38, 27, 43, 3, 9, 82, 10]],
+ [
+ [38, 27, 43, 3],
+ [9, 82, 10],
+ ],
+ [
+ [38, 27],
+ [43, 3],
+ [9, 82],
+ [10],
+ ],
+ [[38], [27], [43], [3], [9], [82], [10]],
+ ];
+
+ const mergeLevels = [
+ [[38], [27], [43], [3], [9], [82], [10]],
+ [
+ [27, 38],
+ [3, 43],
+ [9, 82],
+ [10],
+ ],
+ [
+ [3, 27, 38, 43],
+ [9, 10, 82],
+ ],
+ [[3, 9, 10, 27, 38, 43, 82]],
+ ];
+
+ const working = [
+ {
+ passes: "Divide the array:",
+ points: [
+ "[38, 27, 43, 3, 9, 82, 10] splits into [38, 27, 43, 3] and [9, 82, 10]",
+ "Each half splits again, and again, until every piece holds one element",
+ "A single element is already sorted — that is the base case where the splitting stops",
+ ],
+ levels: divideLevels,
+ mode: "divide",
+ },
+ {
+ passes: "Merge back up:",
+ points: [
+ "[38] + [27] → [27, 38], [43] + [3] → [3, 43], [9] + [82] → [9, 82]",
+ "[27, 38] + [3, 43] → [3, 27, 38, 43], and [9, 82] + [10] → [9, 10, 82]",
+ "[3, 27, 38, 43] + [9, 10, 82] → [3, 9, 10, 27, 38, 43, 82]",
+ ],
+ levels: mergeLevels,
+ mode: "merge",
+ },
+ ];
+
+ // A single merge of [27, 38] and [3, 43], one comparison per step.
+ const mergeSteps = [
+ {
+ caption: "Compare 27 and 3 → 3 is smaller, so it goes out first",
+ left: [27, 38],
+ right: [3, 43],
+ li: 0,
+ ri: 0,
+ taken: "right",
+ output: [3],
+ },
+ {
+ caption: "Compare 27 and 43 → take 27",
+ left: [27, 38],
+ right: [3, 43],
+ li: 0,
+ ri: 1,
+ taken: "left",
+ output: [3, 27],
+ },
+ {
+ caption: "Compare 38 and 43 → take 38",
+ left: [27, 38],
+ right: [3, 43],
+ li: 1,
+ ri: 1,
+ taken: "left",
+ output: [3, 27, 38],
+ },
+ {
+ caption: "The left half is empty → copy the rest of the right half",
+ left: [27, 38],
+ right: [3, 43],
+ li: 2,
+ ri: 1,
+ taken: "right",
+ output: [3, 27, 38, 43],
+ },
+ ];
+
const algorithm = [
{
points: "Divide:",
@@ -87,6 +467,111 @@ const Content = () => {