cancel
Showing results for 
Search instead for 
Did you mean: 
JKova
Level 4
138 92 2 5
Message 1 of 2
428
Flag Post

How to do "selection sort" in HP 50g graphing calculator by using User RPL

HP Recommended
HP 50g graphing calculator

Hi dear HP Community , 

I want to know how to do selection sort in

HP 50g graphinc calculator without using SORT function ( or maybe command ) or without using built in - hidden commands such as #2FD006h FLASHEVAL or others.

 

Let's suppose that I have a unsorted list like this { 1 5 2 4 3 2 8 2 4 } and I want to order those elements by ther values from biggest to smallest or vice versa , whatever.

 

I want to know how to do that by using clean User RPL ,

if also possible ... if not possible let me know about that. I also found some literature for doing selection sort .

Also this is from wikipedia : 

https://en.m.wikipedia.org/wiki/Selection_sort

And also i have material from Geeks for Geeks site how to do this on well known programming languages like C , C++ , Java , Python etc..

https://www.geeksforgeeks.org/selection-sort/

 

Besides all of that i also post a question on HP Support Community about sorting , mister Horn replied to me and i think that he gave me useful advices but still i don't know how to do these things and how to use that knowledge for solving this - selection sort.

 

https://h30434.www3.hp.com/t5/Calculators/Two-basic-questions-for-programming-in-HP-50g-graphing/m-p...

 

I don't know dear people but I will be very very gratefull if someone knows how to do that because no one replied to me about that when i wrote that I don't know how to do that - I'm thinking about selection sort in HP 50g graphing calculator ( User RPL ).

 

Best regards ,

Bye 🤗

0 Kudos
1 REPLY 1
Joe_Horn
Level 8
622 609 117 207
Message 2 of 2
Flag Post
HP Recommended

Ok, here's what you were looking for: A 100% standard User RPL program which uses stack operations to sort a list of numbers using the Insertion Sort algorithm, optimized with a binary search.  Two local variables are used, one to store the size of the original list, and one as the counter in the FOR-NEXT loop.  In a nutshell, the list is exploded onto the stack, then all the sorting is performed on the stack, and when the stack is all sorted, the numbers on the stack are placed back into a list.  Therefore, while the work of sorting is being performed, there are no lists or arrays of any kind either on the stack nor stored in a variable.

 

Input: List of numbers.

Output: Same list, sorted into ascending order.

Speed: Sorts 128 random reals in 9 seconds; 128 random integers in 35 seconds.

 

This program is useful only for study purposes, since the built-in SORT command (which uses the same logic as this program) is roughly 8 times faster.

 

« OBJ→ → s « 1. s FOR j s ROLL 0. j

WHILE DUP2 SWAP – 2. / IP DUP

REPEAT OVER SWAP – DUP 4. + PICK 5. PICK

IF > THEN UNROT END SWAP DROP

END ROT DROP2 ROLLD NEXT s →LIST » »

-Joe-
0 Kudos
† The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation