Data Structure on a 12C/CP -- sorting, median



#4

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.


#5

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 ...

#6

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


Possibly Related Threads...
Thread Author Replies Views Last Post
  Sheet data importer for HP Prime Marek Russ 4 1,116 11-15-2013, 04:55 AM
Last Post: debrouxl
  Entering,Saving,and Analysis /Fitting X Y Data on the Prime Harold A Climer 6 1,500 10-26-2013, 01:54 PM
Last Post: Tim Wessman
  HP PRIME: How to change the column headers and reset data Joseph Ec 5 1,340 10-18-2013, 02:26 PM
Last Post: Joseph Ec
  Directory Structure,if any, on the Prime Harold A Climer 9 1,390 10-07-2013, 11:08 PM
Last Post: Helge Gabert
  HP Prime data sharing Alberto Candel 5 1,134 10-06-2013, 07:49 PM
Last Post: Alberto Candel
  Printing HP 9825 data Norman Pillsbury 3 970 06-01-2013, 10:08 PM
Last Post: David Ramsey
  HP 86/87 - CP/M Ph.Théry 3 863 05-27-2013, 04:02 AM
Last Post: Olivier De Smet
  WP-34s data exchange with PC over IR Marcel Samek 3 935 02-26-2013, 11:53 PM
Last Post: Marcel Samek
  GROBs with hidden data in them... Chris Dreher 2 718 12-13-2012, 01:07 PM
Last Post: Chris Dreher
  I feel like a data miner... Luiz C. Vieira (Brazil) 6 1,321 12-03-2012, 10:59 PM
Last Post: Gerson W. Barbosa

Forum Jump: