Back to the tutorial

Insertion sort with pyramids

TLAC instruction cards

Insert one pyramid at a time into the sorted row. Compare it with the pyramid to its left; if the pair is out of order, swap them using A1 as a temporary space. Continue left until the new pyramid is in order. The input is 5, 4, 3, 1, 2 and the result is 1, 2, 3, 4, 5.

Read the numbered TLAC cards first, then use the debugger below. Skip a function’s enclosed cards until it is called; after returning, continue at the call site. The right-to-left search function is defined by the original program but is not called in this run.

Jump to the step-through debugger ↓

0001

Load the starting board: sizes 5, 4, 3, 1, 2 in F3–J3



0002

Place the turtle at E10 (X=4, Y=9)



0003

Display “initial board:”



0004

Display the current board



0005

Define function: Find the next unsorted pyramid · Run these cards only when called.



0006

For each x in columns 0 through 9, from left to right, repeat



0007

Display (“Ax:” + x written as text)



0008

Set (value, x, y) to the pyramid size and coordinates at X=x, Y=2; use “not found” if empty



0009

If value differs from “not found”, then



0010

Return to the caller with result: (value, x, y)



0011

Return to the caller with result: no value



0012

Define function: Find the rightmost pyramid in the sorting row · Run these cards only when called.



0013

For each x in columns 9 through 0, from right to left, repeat



0014

Display (“Bx:” + x written as text)



0015

Set (value, x, y) to the pyramid size and coordinates at X=x, Y=4; use “not found” if empty



0016

If value differs from “not found”, then



0017

Return to the caller with result: (value, x, y)



0018

Return to the caller with result: no value



0019

For each col in whole numbers from 0 up to, but not including, 5, repeat



0020

Display (“column ” + col written as text)



0021

Set (pyramid Number, x, y) to the result of calling “Find the next unsorted pyramid”



0022

Display (“HERE pyr ” + pyramid Number written as text)



0023

Move pyramid pyramid Number to X=col, Y=4



0024

Set current to col



0025

While current is greater than 0, repeat



0026

Set (left value, left x, left y) to the pyramid size and coordinates at X=(current − 1), Y=4; use “not found” if empty



0027

Set (current value, current x, current y) to the pyramid size and coordinates at X=current, Y=4; use “not found” if empty



0028

If (left value equals “not found”) or (current value equals “not found”), then



0029

Display “Error: Missing pyramid during bubble”



0030

Leave the innermost loop



0031

If left value is at most current value, then



0032

Leave the innermost loop



0033

Display (((“SWAP pyramids ” + left value written as text) + “ and ”) + current value written as text)



0034

Move pyramid current value to X=0, Y=0



0035

Move pyramid left value to X=current, Y=4



0036

Move pyramid current value to X=(current − 1), Y=4



0037

Update current: (current − 1)



0038

Display “Final sorted board:”



0039

Display the current board


Show every step and its board