$include (..\lib\compstch.ext) /* *============================================================================ * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE * * Permission to use for any purpose, modify, copy, and make enhancements * and derivative works of the software is granted if attribution is given to * R.M. Gillmore, dba the ACME Software Deli, as the author * * While the ACME Software Deli does not work for money, there is nonetheless * a significant amount of work involved. The ACME Software Deli maintains the * rights to all code written, though it may be used and distributed as long as * the following conditions are maintained. * * 1. The copyright statement at the top of each code block is maintained in * your distribution. * 2. You do not identify yourself as the ACME Software Deli * 3. Any changes made to the software are sent to the ACME Software Deli *============================================================================ */ sortFunctionsModule: do; $if not noID declare IDString (*) byte data ( '@(#)srtFuncs.p86 $Author: rmgillmore $ $Date:: 2025-05-04 19:35:39#$:', 0 ); $endif /* * Tue 11 Sep 02:15 pm 2012 * rmg Starting stages of implementation */ $set ( SORT_FUNCTIONS_IMPLEMENTATION ) $include (..\lib\srtFuncs.ext) $include (..\lib\numStrs.ext) $include (..\lib\fileIO.ext) $include (..\lib\compAsm.ext) declare swapTemplate literally ' temp = right; right = left; left = temp'; byteSwap: procedure ( leftPtr, rightPtr ) reentrant; declare leftPtr pointer, left based leftPtr byte, rightPtr pointer, right based rightPtr byte, temp byte; swapTemplate; end byteSwap; wordSwap: procedure ( leftPtr, rightPtr ) reentrant; declare leftPtr pointer, left based leftPtr word, rightPtr pointer, right based rightPtr word, temp word; swapTemplate; end wordSwap; dwordSwap: procedure ( leftPtr, rightPtr ) reentrant; declare leftPtr pointer, left based leftPtr dword, rightPtr pointer, right based rightPtr dword, temp dword; swapTemplate; end dwordSwap; /* * Here is pseudocode for the bubble sort using a zero-based array: * * procedure bubblesort4( var a as array, size N ) * bound = N-1 * for i from 1 to N * newbound = 0 * for j from 0 to bound * if a[j] > a[j + 1] * swap( a[j], a[j + 1] ) * newbound = j - 1 * end if * end for * bound = newbound * end for * end procedure */ /* NOT reentrant because there are nested procedures */ bubbleSort: procedure ( arrayPtr, arrayLen, arrayType, cmpFunc ) public; declare arrayPtr pointer, arrayLen integer, arrayType word, cmpFunc pointer; declare b integer, aIndex integer, sIndex integer, nB integer; declare sortTemplate literally ' b = arrayLen - 2; do aIndex = 0 to ( arrayLen - 1 ); nB = 0; do sIndex = 0 to b; if ( runCompare( @array( sIndex ), @array( sIndex + 1 ), cmpFunc ) < 0 ) then do; call swap( @array( sIndex ), @array( sIndex + 1 ) ); nB = sIndex - 1; end; end; b = nB; end'; sortBytes: procedure; declare array based arrayPtr (1) byte, temp byte; declare swap literally 'byteSwap'; sortTemplate; end sortBytes; sortWords: procedure; declare array based arrayPtr (1) word, temp word; declare swap literally 'wordSwap'; sortTemplate; end sortWords; sortDWords: procedure; declare array based arrayPtr (1) dword, temp dword; declare swap literally 'dwordSwap'; sortTemplate; end sortDWords; if ( arrayType >= byteSortType ) and ( arrayType <= stringSortType ) then do; do case arrayType; Call sortBytes; Call sortWords; Call sortWords; /* integer the same size as words */ Call sortDWords; Call sortDWords; /* pointer the same as dwords */ Call sortDWords; /* string the same as dwords */ end; /* case arrayType */ end; end bubbleSort; end sortFunctionsModule;