(20S) Bubble Sort
06-19-2021, 03:49 PM (This post was last modified: 06-19-2021 08:11 PM by Gene.)
Post: #1
 arhtur Junior Member Posts: 25 Joined: Jun 2021
(20S) Bubble Sort
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.

Attached File(s)
06-20-2021, 01:58 AM
Post: #2
 rprosperi Super Moderator Posts: 6,122 Joined: Dec 2013
RE: (20S) Bubble Sort
"(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.

--Bob Prosperi
 « Next Oldest | Next Newest »

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