Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
 Welcome to our latest new user eightbitswide ! (Registered 2024-12-24) You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > Indirect Sorting Algorithm
2005-12-14 14:47
MRT
Account closed

Registered: Sep 2005
Posts: 149
Indirect Sorting Algorithm

Hi there...

I've written an Indirect Sorting Algorithm!
Whhhooooohhhooo... ;-)


But yeah... It's too slow. :-(
In the code below I can find some little things (few cycles) to speed it up, but this would not do the trick for me.

What does the code do?
It has an 16 bit data array (the elements in the array are 16 bits) and it has an index array. The index array contains indexes to the data array.
First the index array is initialized so that the first index points to the first element in the data array, the second index points to the second element in the data array, and so on...
Then the sorting starts...
It gets the first index and gets the msb of the element the index points to, then compares it to the msb of the element the second index points to. IF the second element is lower then the first, it swaps the indexes, so that the first index isplaced at the second pos in the index table and the second index is placed at the first pos of the index table...
This is the same for the lsb, and the same for all indexes (and elements) of the array.
Normally it goes about 16 times through the code to fully sort a 16 bits array of 16 elements (32 bytes).

Who can help me to speed up this code?
I want it to be twice as fast :-)


And here's the code:

;----------------------------------------------------------
; IndirectSort Routine
;----------------------------------------------------------
;
; Sorts the index table from Low to High
;
; (change the "bcc" commands to "bcs"
;  to sort from High to Low)
;
; USES:
;   IndexTable    An array of indexes. Indexes for the DataArray
;   DataTable     An array with 16 bit data. The index table has indexes to this array
;   ArrayLength   A variable filled with the number of elements (not bytes) in the data array.
;   DoMoreSort    A flag which is set to "true" if more sorting is needed
;
        
        ;init index table
        ldx #$00
        lda #$00
        clc
IS_nextIndexInit
        sta IS_IndexTable,x
        adc #$02
        inx
        cpx #IS_ArrayLength
        bne IS_nextIndexInit
        
IS_startOfSortPass
        ;init table index
        ldy #$00
        
        ;init DoMoreSort flag
        lda #false
        sta IS_DoMoreSort
        
IS_checkSwapOnMSB
        ;get y-pos (msb)
        ldx IS_IndexTable,y        ;index to y-pos-table
        lda IS_DataTable,x         ;load y pos (msb)
        
        ;compare element with next element (msb)
        ldx IS_IndexTable + 1,y    ;index to y-pos-table
        cmp IS_DataTable,x         ;compare y pos (msb)
        
        ;check which is higher
        beq IS_checkSwapOnLSB
        bcc IS_nextIndex           ;carry is clear when the second is higher then the first
        
        ;swap indexes in index table
        lda IS_IndexTable,y
        sta IS_IndexTable + 1,y
        stx IS_IndexTable,y

        ;set DoMoreSort flag
        lda #true
        sta IS_DoMoreSort
        
        ;jump to next index
        jmp IS_nextIndex
        
IS_checkSwapOnLSB
        ;get y-pos (lsb)
        ldx IS_IndexTable,y        ;index to y-pos-table
        lda IS_DataTable + 1,x     ;load y pos (lsb)
        
        ;compare element with next element (lsb)
        ldx IS_IndexTable + 1,y    ;index to y-pos-table
        cmp IS_DataTable + 1,x     ;compare y pos (lsb)
        
        ;check which is higher
        beq IS_nextIndex
        bcc IS_nextIndex           ;carry is clear when the second is higher then the first
        
        ;swap indexes in index table
        lda IS_IndexTable,y
        sta IS_IndexTable + 1,y
        stx IS_IndexTable,y
        
        ;set DoMoreSort flag
        lda #true
        sta IS_DoMoreSort
        
IS_nextIndex
        iny
        cpy #IS_ArrayLength - 1
        bne IS_checkSwapOnMSB
        
        ;check if sort is ready
        lda IS_DoMoreSort
        cmp #true
        beq IS_startOfSortPass
        
        ;done
        rts


 
... 10 posts hidden. Click here to view all posts....
 
2005-12-15 19:08
Monte Carlos

Registered: Jun 2004
Posts: 359
@mrt:thanks for the google tip
@others:
This is a link to several online chapter of "numerical recipes in c":
http://www.library.cornell.edu/nr/bookcpdf.html
look in the chapter 8 with sorting algorithms for different approaches.
2005-12-15 23:18
MRT
Account closed

Registered: Sep 2005
Posts: 149
@Monte Carlos:
Great link! Interesting stuff there!
2005-12-29 10:48
MRT
Account closed

Registered: Sep 2005
Posts: 149
I've found a way to speed it up even more!

What the main problem was, that it has to look at four bytes to sort two elements (word size). It first looks at the msb of one element and the msb of the next element and after that, it looks at the lsb of one element and the lsb of the next element.

Now what i've done is the following:
1. Create a list of all y-pos with one byte instead of one word per element. One element is formed like this:
lda msb
lsr
lda lsb
ror
sta smallYPos

So, the least-significant-bit is dropped and the most-significant-bit is rolled in.

2. Sorting the one-byte y-pos list with the normal sorting algo (bubble sort) as seen above only omitting the msb sort steps.

Now, this is enough accuracy for my routine, but if you still want to sort the least-significant-bit, you'll need one more step.

3. Do one single sorting pass through the two-byte y-pos table with the normal sorting routine. This only needs one pass, because elements can only go up or down one single place in the table.

Using only the step 1 and 2, the sorting needs only half of the CCs it needed before.
First I dropped the init-table routine (see above), which resulted the sorting to go 8 times as fast.
Now I use one byte instead of one word, which results the sorting routine to go twice as fast.
So in total, the algo needs only a 16th of the origonal CCs now!!!
whoooppyyy!!

2006-01-06 14:54
MRT
Account closed

Registered: Sep 2005
Posts: 149
I'll release the complete code here after our new demo is finished and released. :-)
Previous - 1 | 2 - Next
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
The Phantom
Guests online: 139
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.6)
5 The Demo Coder  (9.6)
6 Edge of Disgrace  (9.6)
7 What Is The Matrix 2  (9.6)
8 Uncensored  (9.6)
9 Comaland 100%  (9.6)
10 Wonderland XIV  (9.6)
Top onefile Demos
1 Layers  (9.6)
2 Cubic Dream  (9.6)
3 Party Elk 2  (9.6)
4 Copper Booze  (9.6)
5 X-Mas Demo 2024  (9.5)
6 Dawnfall V1.1  (9.5)
7 Rainbow Connection  (9.5)
8 Onscreen 5k  (9.5)
9 Morph  (9.5)
10 Libertongo  (9.5)
Top Groups
1 Performers  (9.3)
2 Booze Design  (9.3)
3 Oxyron  (9.3)
4 Censor Design  (9.3)
5 Triad  (9.3)
Top Webmasters
1 Slaygon  (9.6)
2 Perff  (9.6)
3 Sabbi  (9.5)
4 Morpheus  (9.4)
5 CreaMD  (9.1)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.039 sec.