Colorsort

Developer(s): Lloyd Painter
Date: 1985
Type: Program
Platform(s): TS 2068
Tags: Demo

This program implements a bubble sort algorithm that operates directly on the TS 2068’s color attribute memory to produce a visual sorting demonstration. It begins by filling all 768 screen attribute cells (starting at address 22528) with random BRIGHT paper colors using POKE, then performs an optimized bubble sort that swaps adjacent attribute bytes when they are out of order. The optimization at line 85 exits the outer loop early if no swaps occurred in a pass (x=0 acting as a swap flag), which is a classic bubble sort improvement. A PAUSE 15 at line 77 slows each swap so the color rearrangement is visible on screen, and a continuous BEEP plays at the end once sorting is complete.


Program Analysis

Program Structure

The program divides cleanly into three phases: initialization (lines 10–35), the bubble sort loop (lines 40–90), and a completion signal (lines 95–105). The sort operates entirely on screen color attribute memory rather than display pixels, making the algorithm’s progress directly visible as a side effect of the sort itself.

Attribute Memory Layout

The color attribute file starts at address 22528 (decimal). The program sets a=22527 at line 20 and then accesses cells as a+n for n from 1 to 768, effectively addressing 22528 through 23295 — the full 768-byte attribute area covering all 24 rows of 32 columns. Each byte encodes ink, paper, bright, and flash bits.

Initialization

Lines 10–15 set the display to black border and background with yellow ink, then clear the screen. Line 30 fills every attribute byte with 8 + 8*INT(RND*7). Breaking this down: INT(RND*7) yields a random integer from 0 to 6 (seven non-black colors), multiplying by 8 shifts it into the paper color bit field (bits 3–5), and adding 8 sets the BRIGHT bit (bit 3 of the high nibble). This produces 7 distinct bright paper colors with black ink.

Bubble Sort Algorithm

The sort uses a standard optimized bubble sort with an early-exit condition:

  1. Outer loop (s) runs from 767 down to 1, shrinking the unsorted region each pass.
  2. Inner loop (n) compares adjacent attribute bytes at addresses c and c+1.
  3. If PEEK c > PEEK (c+1), the two bytes are swapped using x as a temporary variable, and a PAUSE 15 slows the animation.
  4. The variable x doubles as a swap-occurred flag: it is set to 0 at the start of each pass (line 45) and is set to the value of PEEK c whenever a swap occurs. If x remains 0 after a full inner pass, the array is already sorted and the program jumps to line 95.

Notable Techniques

  • Dual-use swap variable: x serves both as the temporary swap holder and as a dirty flag, saving a separate boolean variable.
  • Base address offset: Setting a=22527 (one below the attribute file) allows 1-based indexing in the loop without an off-by-one error.
  • PAUSE for animation: PAUSE 15 (about a quarter second at 50 Hz) on every swap makes each color exchange perceptible, turning the sort into a visual display.
  • Early termination: The IF x=0 THEN GO TO 95 check at line 85 skips unnecessary passes once the data is sorted, a classic bubble sort optimization.

Completion and Loop

After sorting, lines 95–105 emit a half-second beep (note 26, approximately B4) and then enter an infinite loop via GO TO 95, continuously repeating the beep every 10/50ths of a second. This keeps the sorted color display on screen indefinitely while providing an audible completion signal.

Potential Anomalies

  • The NEXT s at line 90 is only reached when a swap occurred; the loop variable s decrements normally. However, when GO TO 95 is taken at line 85, the FOR s loop is abandoned mid-execution, which is normal practice in Sinclair BASIC.
  • Because the sort is purely on attribute bytes, pixel data is entirely unaffected — the screen remains visually blank (black paper, black ink) except for the color blocks produced by the attribute values themselves.
  • The initial INK 6 at line 10 affects subsequent PRINT output but has no direct effect on the POKE-driven attribute animation.

Content

Appears On

Related Products

Related Articles

Basic and machine language programs to demonstrate sorting speeds.

Related Content

Image Gallery

Source Code

    5 REM 24 line BASIC color sort by Lloyd Painter
   10 BRIGHT 0: BORDER 0: INK 6
   15 PAPER 0: CLS 
   20 LET a=22527
   25 FOR n=1 TO 768
   30 POKE (a+n),8+8*INT (RND*7)
   35 NEXT n
   40 FOR s=767 TO  1 STEP -1
   45 LET x=0
   50 FOR n=1 TO s
   55 LET c=a+n
   60 IF PEEK c<=PEEK (c+1) THEN GO TO 80
   65 LET x=PEEK c
   70 POKE c,PEEK (c+1)
   75 POKE (c+1),x
   77 PAUSE 15
   80 NEXT n
   85 IF x=0 THEN GO TO 95
   90 NEXT s
   95 BEEP .5,26
  100 PAUSE 10
  105 GO TO 95

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.

Scroll to Top