The Museum of HP Calculators

HP Forum Archive 16

[ Return to Index | Top of Index ]

Data Structure on a 12C/CP -- sorting, median
Message #1 Posted by Katie Wasserman on 17 June 2006, 5:18 a.m.

Now that the new 12CP has 81 cash flow registers with accompanying Nj's (that can have values from 0 to 99) I thought that I find some non-obvious use for them. Also, I've been thinking about the median problem that Gene presented a while back. Combining these ideas along with other people's ideas about incremental sorting I came up with a little linked list insertion sort program.

This program treats the CFj/Nj registers as a linked list and does an
incremental insertion sort into it.  It works on the original HP 12C
and the later model HP 12C Platinum (and related editions), but will
not work as-is on the early model Platinum due the limitation on N(0). 

To use the program:

(1) Set n<--0, N(0)<--0 (0, STO n, Nj)

(2) Enter each number to sort and press R/S. The program will execute CFj adding the data to the cash flow registers and then "link" this number into the sorted data structure. Data will remain in the order that you enter them, only the Nj's are modified. (Entry will take a varying amount of time based on how many numbers have been previously entered and where the current number is in sorted order.)

N(0) contains the number of the CF register with the lowest data value. The N(x) register contains the number of the CF register with the next lowest value, etc.. The CF register with the highest data value will have a value of zero in N(x) indicating the end of the list. Note that CF(0) does not contain a data element.

Keystrokes |Display | Comments [f][P/R] | | [f]CLEAR[PRGM] |00- | [g][CFj] |01- 43 14 | increment n and add to CF register [STO]0 |02- 44 0 | save the data being added for easy access [RCL][n] |03- 45 11 | [STO][i] |04- 44 12 | save n 0 |05- 0 | [STO][PV] |06- 44 13 | PV is the current value of the link [STO][n] |07- 44 11 | use it to point to the next CF register [RCL][g][Nj] |08- 45,43 15 | [STO][FV] |09- 44 15 | save the next value of the link [STO][n] |10- 44 11 | [RCL][g][CFj] |11- 45,43 14 | [RCL]0 |12- 45 0 | [g][x<=y] |13- 43 34 | compare CFj with the new entry [g][GTO]17 |14- 43,33 17 | same or smaller value so insert it into the linked-list here [RCL][FV] |15- 45 15 | [g][GTO]06 |16- 43,33 06 | follow the link if the new entry is larger [RCL][PV] |17- 45 13 | [STO][n] |18- 44 11 | [RCL][i] |19- 45 12 | [g][Nj] |20- 43 15 | current link now points to latest data entry [STO][n] |21- 44 11 | restore n [RCL][FV] |22- 45 15 | [g][Nj] |23- 43 15 | link of last data entry now points to [g][GTO]00 |24- 43,33 00 | where previous link pointed [f][P/R] | |

The following program can be used to traverse the linked list displaying the data in sorted order:

Keystrokes |Display | Comments [f][P/R] | | [f]CLEAR[PRGM] |00- | 0 |01- 0 | [STO][n] |02- 44 11 | [RCL][g][Nj] |03- 45,43 15 | [STO][PV] |04- 44 13 | [STO][n] |05- 44 11 | [RCL][g][CFj] |06- 45,43 14 | [g][PSE] |07- 43 31 | [RCL][PV] |08- 45 13 | [g][x=0] |09- 43 35 | [g][GTO]00 |10- 43,33 00 | [g][GTO]02 |11- 43,33 02 | [f][P/R] | |

This next program will find the N'th largest data element in the sorted list; call with N in the X register:

Keystrokes |Display | Comments [f][P/R] | | [f]CLEAR[PRGM] |00- | [STO]0 |01- 44 0 | 0 |02- 0 | [STO][n] |03- 44 11 | [RCL]0 |04- 45 0 | [g][x=0] |05- 43 35 | [g][GTO]11 |06- 43,33 11 | 1 |07- 1 | [STO][-]0 |08- 44 30 0 | [RCL][g][Nj] |09- 45,43 15 | [g][GTO]03 |10- 43,33 03 | [RCL][g][CFj] |11- 45,43 14 | [g][GTO]00 |12- 43,33 00 | [f][P/R] | |

[Thanks to tony(nz) for the nifty 12C formatting utility!]

Edited: 17 June 2006, 5:21 a.m.

      
Re: Data Structure on a 12C/CP -- sorting, median
Message #2 Posted by Gene on 17 June 2006, 11:01 a.m.,
in response to message #1 by Katie Wasserman

Thanks, Katie! this is really good.

Now if only the 12cp had 750 program lines rather than 400 and 300 cash flow registers!

Gene

P.S. No, that's not any inside information about some upcoming revision. it would be nice, but ...

      
Re: Data Structure on a 12C/CP -- sorting, median
Message #3 Posted by tony(nz) on 17 June 2006, 5:53 p.m.,
in response to message #1 by Katie Wasserman

Thanks Katie! For me this is like a surprise gift! I never made the connection between the Nj going to 99 and the number of CF registers. Your program will still work when we have 99 CFj :-) I've tested it - keyed it in in a flash - I see the number of entries is nicely preserved in [i], so it is real easy to pick out the median, even if I forget how many numbers I've keyed in<G>.

Another gem!

Cheers, Tony


[ Return to Index | Top of Index ]

Go back to the main exhibit hall