(20S) Bubble 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: (20S) Bubble Sort (/thread-17138.html) |
(20S) Bubble Sort - arhtur - 06-19-2021 03:49 PM After implementing the N-queens algorithm on the HP 20s, I thought I would try the bubble sort algorithm. A single register is used to store up to 10 single digit numbers (perhaps the algorithm can be expanded for more numbers or more digits, but there is a limitation on the number of programming steps available, and this program uses 97). An interesting way to swap 2 single digit numbers is used. I found that if you have a number like 73, the digits can be swapped by taking the difference of the digits, multiplying by 9 and then subtracting from the number: N1 = N1 - (D1 - D0) * 9 (7-3) * 9 = 36, 73 - 36 = 37 In the program (see attached .PDF file), subroutine A gets 2 digits (register 1 holds the position of the least significant digit, starting at 0). subroutine B sets 2 digits (again, register 1 is the digit position, the original 2 digits must be in register 1, and the new digits are in register 3). subroutine C is the sort algorithm. To run the sort, store the numbers (e.g. 9547621384) in register 0 and XEQ C. After about 20 seconds, RCL 0 to see the sorted numbers. If 0 is among the numbers, it will not show up in the final result, since calculators automatically eliminate leading zeros. RE: (20S) Bubble Sort - rprosperi - 06-20-2021 01:58 AM "(20S) Bubble Sort" Wow, that's a title I never expected to see... Bravo for taking this machine where it likely has never gone before, nor where it's developers expected it to tread. |