HP Forums
(42S) Matrix Sort - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (42S) Matrix Sort (/thread-11010.html)



(42S) Matrix Sort - Dave Britten - 07-05-2018 06:08 PM

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



RE: (42S) Matrix Sort - Joe Horn - 07-05-2018 08:16 PM

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.


RE: (42S) Matrix Sort - Dave Britten - 07-05-2018 08:28 PM

(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


RE: (42S) Matrix Sort - Joe Horn - 07-05-2018 09:17 PM

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.


RE: (42S) Matrix Sort - Dave Britten - 07-05-2018 09:27 PM

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


RE: (42S) Matrix Sort - Joe Horn - 07-05-2018 10:21 PM

(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