Algorithm Search⌘K

See Insertion sort run

Sort an array one item at a time. Take the next item and move it left until the item before it is not larger. Use the buttons or the keys: Space plays and pauses, the arrow keys step, Home and End jump.

  1. INSERTION-SORT(A)
  2. 1for i ← 1 to n − 1 do
  3. 2key ← A[i]
  4. 3j ← i − 1
  5. 4while j ≥ 0 and A[j] > key do ▷ > and not ≥ keeps the sort stable
  6. 5A[j + 1] ← A[j]
  7. 6j ← j − 1
  8. 7A[j + 1] ← key
0 / 0
Use your own input

Type the input as JSON, in the same form as a preset.

Open the full player

Read the full page about Insertion sort. To put this player on your site, use <iframe src="https://algorithm.tamnd.com/embed/insertion-sort/" width="100%" height="520"></iframe>.