« Tour for Techies | Main | Credit card scams and Internet hoaxes »

Tech Support: Bubble sorting

Kim wrote:

Hi David,

I'm trying to make a fault code which display onto the LCD.

I wish to make it like a circular buffer which has up to, say 10, faults recorded. Also I wish to display the most significant fault onto the LCD.

The way I visualise it, is that I give each fault a number, ie 0 to 255 (or to whatever the highest fault number is) and then sort the faults and display the highest priority first, until cleared, and then the next fault etc.

However, I'm having trouble doing a sort in splat, any ideas ?
Kim,

I just wrote a simple bubble sort (at the end of the post). I would not use an actual circular buffer structure - just build it as a linear FIFO buffer and shift data.

I've also attached a rather elaborate fault recording function I wrote some time ago for HVAC applications. It stores faults in time order (no sorting) but has a number of other interesting concepts.

I will incorporate this into future releases of SPLat/PC in the Examples folder.

David
;bblsort1.spt Simple bubble sort

;------------------------------------------------------------
;Test code

bArray          defByte         10

Test:
        LoadI           bArray  ;Fill test array
        iSetMem         0,25
        iSetMem         1,17
        iSetMem         2,250
        iSetMem         3,2
        iSetMem         4,0
        iSetMem         5,5
        iSetMem         6,67
        iSetMem         7,76
        iSetMem         8,17
        iSetMem         9,14
        
        LoadX           bArray  ;Prepare to sort
        LoadX           10
        GoSub           BblSort

Here    GoTo            Here

;------ Bubble sort -------------------------------------------------
;Sort an array of n values into ascending order (lowest first)
;Call with X = n)umber of values, Y=start of array
;Clobbers stack and I
;Method: Scan the array, comparing pairs of values. If two values are found in the
;wrong order, swap them and continue the scan. Repeat until you get a scan where
;no swaps take place.
;Note: This function could consume many mS of processor time depending on the array size.

sBBL_Swap               defSEM          ;Notes a swap took place
bBBL_N:                 defBYTE         ;Array size
bBBL_Addr:              defBYTE         ;Array address
bBBL_Count:             defBYTE         ;Comparison counter

BblSort:
        Store           bBBL_N
        Store           bBBL_Addr

BBL_OuterLoop:
        ClrS            sBBL_Swap
        Recall          bBBL_Addr
        XtoI
        Recall          bBBL_N
        Store           bBBL_Count

BBL_InnerLoop:  ;Back here to compare 1 pair of entries
;;;        ClrInstCount                    ;For D>=18, if inside a MultiTrack task
        iRecall         0               ;Get two consecutive elements
        iRecall         1
        CompareR
        BranchR
        Target          BBL_Equal       ;No swap
        Target          BBL_XGTY        ;No swap
        Target          BBL_XLTY        ;Swap

BBL_XLTY:
        iStore          0               ;Save in swapped order
        iStore          1
        SetS            sBBL_Swap       
;Fall thru
BBL_Equal:
BBL_XGTY:
        IncI                            ;Array pointer
        DMGNZ           bBBL_Count,BBL_InnerLoop        ;End of array?

;Done one scan. Test if any swaps were done
        GoIfST          sBBL_Swap,BBL_OuterLoop
        Return

TrackBack

TrackBack URL for this entry:
http://www.splatco.com/cgi-sys/cgiwrap/microcon/managed-mt/mt-tb.cgi/37

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)


About

This page contains a single entry from the blog posted on May 17, 2007 6:08 PM.

The previous post in this blog was Tour for Techies.

The next post in this blog is Credit card scams and Internet hoaxes.

Many more can be found on the main index page or by looking through the archives.

Creative Commons License
This weblog is licensed under a Creative Commons License.
Powered by
Movable Type 3.33