I had the time today to play with a very simple bubble sort algorithm. I
had a rather badly coded Pascal version so I converted it to PowerBASIC
without some of the "structured" irritations and it ran OK so I recoded
it from scratch in PowerBASIC inline assembler and got it a lot smaller
and somewhat faster.
Comparing bubble sorts is like watching snails race down a garden path
as there are others that are much better designs but it is useful when
only doing small simple single dimension arrays. In my own case, MASM
does not have the high speed sorting that come with PowerBASIC so I am
stuck with having to develop a few to do basic stuff.
The format of both is "ascending" on DWORD/LONG size data in an array.
Regards,
[email protected]
------------------
had a rather badly coded Pascal version so I converted it to PowerBASIC
without some of the "structured" irritations and it ran OK so I recoded
it from scratch in PowerBASIC inline assembler and got it a lot smaller
and somewhat faster.
Comparing bubble sorts is like watching snails race down a garden path
as there are others that are much better designs but it is useful when
only doing small simple single dimension arrays. In my own case, MASM
does not have the high speed sorting that come with PowerBASIC so I am
stuck with having to develop a few to do basic stuff.
The format of both is "ascending" on DWORD/LONG size data in an array.
Regards,
[email protected]
Code:
Test Code ~~~~~~~~~ redim a(1 to 20) as LONG a(1) = 95 a(2) = 84 a(3) = 100 a(4) = 99 a(5) = 91 a(6) = 98 a(7) = 94 a(8) = 92 a(9) = 96 a(10) = 93 a(11) = 90 a(12) = 81 a(13) = 88 a(14) = 83 a(15) = 97 a(16) = 85 a(17) = 87 a(18) = 86 a(19) = 82 a(20) = 89 ' bubblesort 1,20,a() ' PB high level version bSort 1,20,VarPtr(a(1)) ' PB assembler version lf$ = chr$(13,10) tst$ = str$(a(1))+lf$ + _ str$(a(2))+lf$ + _ str$(a(3))+lf$ + _ str$(a(4))+lf$ + _ str$(a(5))+lf$ + _ str$(a(6))+lf$ + _ str$(a(7))+lf$ + _ str$(a(8))+lf$ + _ str$(a(9))+lf$ + _ str$(a(10))+lf$ + _ str$(a(11))+lf$ + _ str$(a(12))+lf$ + _ str$(a(13))+lf$ + _ str$(a(14))+lf$ + _ str$(a(15))+lf$ + _ str$(a(16))+lf$ + _ str$(a(17))+lf$ + _ str$(a(18))+lf$ + _ str$(a(19))+lf$ + _ str$(a(20)) MessageBox hWin,ByCopy tst$,"Sorted", _ %MB_OK or %MB_ICONINFORMATION 2 Bubble sort algorithms ~~~~~~~~~~~~~~~~~~~~~~~~ '########################################################################## FUNCTION bubblesort(ByVal st as LONG, _ ' array start ByVal nd as LONG, _ ' array end Arr() as LONG) as LONG ' array LOCAL i as LONG LOCAL j as LONG LOCAL dmy as LONG For j = st to nd i = j While Arr(st) and Arr(i) < Arr(i-1) dmy = Arr(i) Arr(i) = Arr(i-1) Arr(i-1) = dmy ! dec i Wend Next FUNCTION = 0 END FUNCTION '########################################################################## FUNCTION bSort(ByVal st as LONG, _ ' array start ByVal nd as LONG, _ ' array end ByVal Arr as LONG) as LONG ' array address #REGISTER NONE ! mov esi, Arr ! sub esi, 4 ! mov ebx, nd ! sub ebx, st ! inc ebx ; EBX holds element count ! xor ecx, ecx ; set outer loop counter to 0 lbSt: ! mov edx, ecx lbNxt: ' ----------------- ' compare elements ' ----------------- ! mov eax, [esi+edx*4] ! cmp eax, [esi+edx*4+4] ! jl lbOut ' -------------- ' swap elements ' -------------- ! mov edi, [esi+edx*4] ! mov eax, [esi+edx*4+4] ! mov [esi+edx*4], eax ! mov [esi+edx*4+4], edi ! dec edx ! jmp lbNxt lbOut: ! inc ecx ! cmp ecx, ebx ! jne lbSt FUNCTION = 0 END FUNCTION '##########################################################################
Comment