(41) Quick Sort - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: HP-41C Software Library (/forum-11.html) +--- Thread: (41) Quick Sort (/thread-18089.html) |
(41) Quick Sort - arhtur - 03-01-2022 04:56 AM When using the DM41x, I found that a bubble sort took approximately 1 minute to sort 50 numbers. Since the DM41x is about 50 times as fast as a HP 41C, that means the HP 41C would take approx. 50 minutes to bubble sort 50 numbers. I implemented a non-recursive version of quick sort based on a public domain algorithm by Darel Rex Finley. In quick sort, there is a begin array pointer (register 95), end array pointer (register 96), left pointer (register 97), right pointer (register 98) and pivot (register 92). There are a few more registers used for loop control, etc. The numbers to be sorted are in registers 0 to 49. I did SIZE 250 just to be safe. The begin array starts at register 120 and the end array starts at register 180. This version took about 31 seconds to sort 50 numbers on the DM41x. Because there’s a lot of overhead in this algorithm, it is only about twice as fast as the bubble sort. To run the quicksort, first do SIZE 250. Store your unsorted numbers in registers 0 to 49. If you want to view the unsorted numbers, you can XEQ VIEWS (and repeatedly press R/S to see each number). Then XEQ QS. You can XEQ VIEWS again to see the sorted numbers. Here is a simple DM41x program to fill the first 50 registers with random numbers using a time based random number generator: LBL "FIL" 0.049 STO 91 LBL "FIL1" TRNG 50 * STO IND 91 ISG 91 GTO "FIL1" RTN Quick Sort Program: Code: LBL "QS" RE: (41) Quick Sort - Gene - 03-01-2022 12:28 PM I'd like to see what the PPC ROM routines S2 and S3 do with large data sets on the DM41X. RE: (41) Quick Sort - Sylvain Cote - 03-01-2022 04:08 PM (03-01-2022 12:28 PM)Gene Wrote: I'd like to see what the PPC ROM routines S2 and S3 do with large data sets on the DM41X. DM41X Results (DMCP 3.2 & DM41X 2.1) Code: R= 25 // Power:USB // S2= 0s 66 // S3= 0s 71 41CL Results (latest update) Code: R= 25 // Turbo:x50 // S2= 2s 12 // S3= 2s 18 Test Code: CLRG Data Loader → Y=random-generator-seed X=Registers SSS.EEEII Code: LBL "DTALD" Code: LBL 10 Code: LBL "DTAS2" Code: LBL "DTAS3" RE: (41) Quick Sort - Gene - 03-02-2022 12:24 PM Wow, S2 and S3 are incredibly fast ! ty Sylvain. Gene RE: (41) Quick Sort - Sylvain Cote - 03-02-2022 02:15 PM (03-02-2022 12:24 PM)Gene Wrote: Wow, S2 and S3 are incredibly fast !I was surprised by the sort times. Both calculators under normal usage (DM41X bat & 41CL 50x), are 13.X times faster than the original calculator in this scenario. (03-02-2022 12:24 PM)Gene Wrote: ty Sylvain.My pleasure Gene. |