Workspace Merge Sorted Arrays
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Merge Sorted Arrays visualizer

Learn Merge Sorted Arrays with a step-by-step Arrays visualizer, JavaScript source code and a worked example. Practice optimization and matrix.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 7Execution step
Reference example · JavaScript
Inspect reference input
{
  "a": [
    1,
    3,
    5
  ],
  "b": [
    2,
    4,
    6
  ]
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
a,b
},s)=>{
let i=0,j=0,o=[];
while(i<a.length||j<b.length){
o.push(j===b.length||i<a.length&&a[i]<=b[j]?a[i++]:b[j++]);
s("Take smaller head",{
A:a,B:b,Output:o,Pointers:[i,j]
})
}
return o
};

Execution canvas

Active Changed
STEP 1

Take smaller head

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

A3 items
B3 items
Output1 items
Pointers2 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Arrays · Optimization and matrix

Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.

Example input

{
  "a": [
    1,
    3,
    5
  ],
  "b": [
    2,
    4,
    6
  ]
}

Execution steps

  1. Take smaller head

Computed output

[
  1,
  2,
  3,
  4,
  5,
  6
]
JavaScript reference implementation
const solve = ({
  a,b
},s)=>{
  let i=0,j=0,o=[];
  while(i<a.length||j<b.length){
    o.push(j===b.length||i<a.length&&a[i]<=b[j]?a[i++]:b[j++]);
    s("Take smaller head",{
      A:a,B:b,Output:o,Pointers:[i,j]
    })
  }
  return o
};

Continue learning

Practice related DSA questions in the same topic.