Katie,

For fun, I took your 12C 21 liner below and changed it to do an

insertion sort, appended below.

I needed 5 extra lines and 1 extra register. One could say

this requires 33/21 (approx pi/2) extra resources :-)

Insertion sorting does seem more efficient, even on the 12C.

For example sorting 12 numbers takes about a minute via bubble

sort, whether the numbers are already pre-sorted in ascending

or descending order. The insertion sort is assymetric in that

it takes 12 seconds if the numbers are pre-sorted in ascending

order, and the full minute if they are pre-sorted in

descending order. It appears that the speed/resource trade-off

works in favour of the insertion, even on the 12C.

Personally I still really like your Bubble sort - for me it is

a classic 12C program, and really made me see a whole

new dimension hidden in the 12C.

----------------

Bubble Sort on HP12C
Posted by Katie on 26 Oct 2003, 11:11 p.m.

It's easy to use, just fill up registers 0 - n with your data

and call the program with n in the x register.

Keystrokes |Display | Comments

[f][P/R] | |

[f]CLEAR[PRGM] |00- |

[STO][i] |01- 44 12 | i = maximum register

1 |02- 1 |

[STO][n] |03- 44 11 | A: n = 1

[RCL][g][CFj] |04- 45,43 14 | B: y = A(n)

[RCL][g][CFj] |05- 45,43 14 | x = A(n-1)

[g][x<=y] |06- 43 34 | if (x <= y)

[x<>y] |07- 34 | swap x,y

[g][CFj] |08- 43 14 | A(n-1) = x

[RDN] |09- 33 |

[g][CFj] |10- 43 14 | A(n) = y

[RCL][i] |11- 45 12 |

[RCL][n] |12- 45 11 |

1 |13- 1 |

[+] |14- 40 | n = n+1

[g][x<=y] |15- 43 34 | if (n <= i)

[g][GTO]03 |16- 43,33 03 | goto B

2 |17- 2 |

[-] |18- 30 | i = i-1

[g][x=0] |19- 43 35 | if (i = 0)

[g][GTO]00 |20- 43,33 00 | return

[g][GTO]01 |21- 43,33 01 | else goto A

[f][P/R] | |

----------------------------

Insertion sort. 26 lines plus 1 extra register (PMT)

Keystrokes |Display | Comments

[f][P/R] | |

[f]CLEAR[PRGM] |00- |

[STO][PMT] |01- 44 14 | PMT = maximum register

[CLx] |02- 35 |

[i] |03- 12 |

[RCL][i] |04- 45 12 |

1 |05- 1 |

[+] |06- 40 |

[STO][i] |07- 44 12 | 1+i=sublist length

[STO][n] |08- 44 11 |

[RCL][g][CFj] |09- 45,43 14 | B: y = A(n)

[RCL][g][CFj] |10- 45,43 14 | x = A(n-1)

[g][x<=y] |11- 43 34 | if (x <= y)

[g][GTO]22 |12- 43,33 22 |

[x<>y] |13- 34 | swap x,y

[g][CFj] |14- 43 14 | A(n-1) = x

[RDN] |15- 33 |

[g][CFj] |16- 43 14 | A(n) = y

[RCL][g][CFj] |17- 45,43 14 | decrement n

[RCL][n] |18- 45 11 |

[g][x=0] |19- 43 35 | finished?

[g][GTO]22 |20- 43,33 22 | do next sublist

[g][GTO]09 |21- 43,33 09 | continue

[RCL][i] |22- 45 12 | test sublist length

[RCL][PMT] |23- 45 14 |

[g][x<=y] |24- 43 34 |

[g][GTO]00 |25- 43,33 00 | finished.

[g][GTO]04 |26- 43,33 04 | sort next sublist

[f][P/R] | |

Cheers,

Tony