Post Reply 
(42S) Matrix Sort
07-05-2018, 06:08 PM (This post was last modified: 07-05-2018 09:32 PM by Dave Britten.)
Post: #1
(42S) Matrix Sort
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
Visit this user's website Find all posts by this user
Quote this message in a reply
07-05-2018, 08:16 PM
Post: #2
RE: (42S) Matrix Sort
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.

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
07-05-2018, 08:28 PM
Post: #3
RE: (42S) Matrix Sort
(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. Smile
Visit this user's website Find all posts by this user
Quote this message in a reply
07-05-2018, 09:17 PM (This post was last modified: 07-05-2018 09:19 PM by Joe Horn.)
Post: #4
RE: (42S) Matrix Sort
Yikes, I just tried to write it up using Free42, and [MAX] and [MIN] don't seem to be in it! Sad 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.

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
07-05-2018, 09:27 PM
Post: #5
RE: (42S) Matrix Sort
(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! Sad 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.
Visit this user's website Find all posts by this user
Quote this message in a reply
07-05-2018, 10:21 PM (This post was last modified: 07-05-2018 10:27 PM by Joe Horn.)
Post: #6
RE: (42S) Matrix Sort
(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! Smile

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)