Skip to content

Instantly share code, notes, and snippets.

@nealmcb
Last active December 25, 2021 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nealmcb/4938c5869ff7ff9cc683e88629ec1c1e to your computer and use it in GitHub Desktop.
Save nealmcb/4938c5869ff7ff9cc683e88629ec1c1e to your computer and use it in GitHub Desktop.
insertion sort in basic, by Neal McBurnett, July 1978 Byte magazine page 121
100 REM Insertion sort by Neal McBurnett for Byte magazine 1978-07
101 REM from page 121 of https://vintageapple.org/byte/pdf/197807_Byte_Magazine_Vol_03-07_Antique_Mechanical_Computers.pdf
120 print "How many numbers to be sorted?"
130 input n
140 print "Input numbers one at a time"
150 for i = 1 to n
155 dim a(n)
160 input a(i)
170 next i
171 REM Straight Insertion SORT: puts the elements
172 REM A(1) through A(N) into ascending order
173 REM I: Loop counter - element of A to be
174 REM inserted next
175 REM J: Loop counter used to search for an
176 REM element less than K
177 REM K: (=A(I)) the number ("key") being inserted
180 for i = 2 to n
190 k = a(i)
200 j = i-1
205 REM while a(j) > k
210 if a(j) <= k then goto 250
215 endif
220 a(j+1) = a(j)
230 j = j - 1
235 REM until j = 0
240 if j > 0 then goto 205
245 endif
250 a(j+1) = k
260 next i
290 for i = 1 to n
300 print a(i)
320 next i
@nealmcb
Copy link
Author

nealmcb commented Dec 25, 2021

Works e.g. with yabasic on Ubuntu Linux 20.04.
I had to add a bit of code (dim statement, endif, gotos) to the original code, but given that, it seems to work fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment