The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 795 - File: showthread.php PHP 7.4.33 (FreeBSD)
File Line Function
/showthread.php 795 errorHandler->error





Post Reply 
(20S) Bubble Sort
06-19-2021, 03:49 PM (This post was last modified: 06-19-2021 08:11 PM by Gene.)
Post: #1
(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)
.pdf  HP20sSort.pdf (Size: 28.66 KB / Downloads: 15)
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
(20S) Bubble Sort - arhtur - 06-19-2021 03:49 PM
RE: (20S) Bubble Sort - rprosperi - 06-20-2021, 01:58 AM



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