# HP Forums

Full Version: (42S) Matrix Sort
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
A simple program for sorting a matrix by your choice of column, in either ascending or descending order.

Usage

Arguments:

y: Matrix
x: Column number (1-based)

Flag 1: Clear = Ascending Order, Set = Descending Order

Put the arguments on the stack and XEQ "SORT". The sorted matrix will be left on the stack in x. Attempting to sort a 1-row matrix will exit cleanly without doing anything.

EDIT: A nicer version that uses the built-in (hidden) "[MIN]" and "[MAX]" functions to do selection sort. When keying this in, note that the brackets are part of the function name and must be included! I've written that function as XEQ "[MIN]" for clarity, but you'll only see [MIN] after entering the command. Thanks Joe.

Code:
```LBL "SORT" RDown STO "_D" RDown STO "_Z" RDown STO "_T" RDown INDEX "_D" 1 X<>Y STOIJ LBL 00 RCLIJ X<>Y FC? 01 XEQ "[MIN]" FS? 01 XEQ "[MAX]" RDown R<>R I+ FC? 76 GTO 00 RCL "_T" ENTER ENTER RCL "_Z" RCL "_D" CLV "_M" CLV "_R" CLV "_D" CLV "_Z" CLV "_T" END```
For a much faster sort (and probably a shorter program), combining the "hidden" [MIN] and [MAX] functions built into the HP-42S with the R<>R function allows rapid sorting of the rows in a matrix into ascending or descending order using any desired column as the sorting key.
(07-05-2018 08:16 PM)Joe Horn Wrote: [ -> ]For a much faster sort (and probably a shorter program), combining the "hidden" [MIN] and [MAX] functions built into the HP-42S with the R<>R function allows rapid sorting of the rows in a matrix into ascending or descending order using any desired column as the sorting key.

Ah, I didn't realize the hidden MIN and MAX functions were column-specific and could have a starting point specified like that. That would make for a pretty easy selection sort implementation, and while that's the same complexity class as bubble sort, having MIN doing all the heavy lifting in the innermost loop would probably improve performance. I'll have to take a stab at that later this evening.

Half the reason I post things in the software library is because somebody undoubtedly knows some clever functions/tricks that I can learn about to make the program better.
Yikes, I just tried to write it up using Free42, and [MAX] and [MIN] don't seem to be in it! Is there some secret way to get to those functions in Free42, or are they really missing?

EDIT: Please note that the [MIN] and [MAX] functions are spelled exactly that way, including the brackets.
(07-05-2018 09:17 PM)Joe Horn Wrote: [ -> ]Yikes, I just tried to write it up using Free42, and [MAX] and [MIN] don't seem to be in it! Is there some secret way to get to those functions in Free42, or are they really missing?

EDIT: Please note that the [MIN] and [MAX] functions are spelled exactly that way, including the brackets.

What version are you running? They're definitely in there on 2.0.20, which is the current DM42 version. I did XEQ "[MIN]" and it worked fine.
(07-05-2018 09:27 PM)Dave Britten Wrote: [ -> ]What version are you running? They're definitely in there on 2.0.20, which is the current DM42 version. I did XEQ "[MIN]" and it worked fine.

Aha! I don't know which version I had (possibly an ancient one), but the App Store right now is updating me to version 2.0.21. Thanks!

EDIT: Yay! It works!
Reference URL's
• HP Forums: https://www.hpmuseum.org/forum/index.php
• :