IF my_array_top >5 THENCR2SORT my_array_topELSEBBLSORT my_array_topEND IF
Option Base 1 Dim xx As Integer xx=my_arraysize 'lack of UBOUND() means we have to manually track the array size Dim SP$(xx)
CR2Sort my_arraysize
Sub CR2SORT(SPtop) Local INTEGER i,x,b Local FLOAT p,y Local STRING a$ p=1.2599211: x=2: b=1: y=SPtop\2 While y>2: y=y/p: Wend While x>1 y=y*p x=SPtop\y For i=1 To SPtop-x If SP$(b+i)>SP$(x+i) Then a$=SP$(b+i):SP$(b+i)=SP$(x+i):SP$(x+i)=a$ 'SWAP SP$(b+i),SP$(x+i) Next Wend End Sub
' 'CR2SORT DOCUMENTATION---6/19/1988 ' ' ' ' THIS SORT IS A RESULT OF AN INTEREST I DEVELOPED IN SORT PROGRAMS 'AND IS MY ATTEMPT AT WRITING AN EFFICIENT ALGORITHM. ' ' THE VALUE USED TO DETERMINE COMPARE/SWAP INTERVALS IS THE CUBE 'ROOT OF 2, HENCE THE NAME 'CR2SORT'. ' 'THE SORT FUNCTIONS IN THE FOLLOWING MANNER ' '1. THE LIST TO BE SORTED IS READ INTO AN ARRAY WITH (T) BEING EQUAL TO 'THE TOTAL NUMBER OF ELEMENTS IN THE ARRAY. ' ' 2. THE TOTAL (T) IS DIVIDED BY 2 (INTEGER DIVISION) THEN REPEATEDLY 'DIVIDED BY (P#) THE 'CR2' UNTIL THE RESULTANT (Y#) IS LESS THAN 2. 'THIS IS ACCOMPLISHED BY A WHILE/WEND LOOP. THIS IS THE SEED NUMBER 'WHICH IS USED TO BALANCE THE SORT. ' ' 3. THIS STARTING NUMBER (Y#) IS MULTIPLIED BY (P#) THE 'CR2' AND DIVIDED 'INTO THE TOTAL NO. OF ELEMENTS (T), TO OBTAIN THE VALUE (X) FOR THE 'COMPARE/SWAP INTERVAL. THIS VALUE IS OBTAINED BY INTEGER DIVISION. ' ' 4. THE INTEGER X IS THEN USED IN A FOR-NEXT LOOP FOR COMPARING B+I 'AND X+I ELEMENTS UNTIL THE (I) COUNT REACHES T-X. THE VALUE FOR X 'DECREASES AS STEP 3 IS REPEATED UNTIL X REACHES 2 AND ADJACENT 'ELEMENTS ARE BEING COMPARED. THIS IS ACCOMPLISED WITH A WHILE/WEND 'LOOP. ' ' BECAUSE THE STARTING NUMBER IS DETERMINED BY THE LENGTH OF THE 'ARRAY, A PERFECT SORT IS OBTAINED WITHOUT SETTING A FLAG TO CHECK IF 'A SWAP HAS BEEN MADE AND MAKING ADDITIONAL LOOPS TO COMPLETE THE SORT. ' ' I HAVE FOUND THIS ALGORITHM TO BE VERY EFFICIENT AND TO HAVE 'EXCELLENT SPEED WITH A MINIMAL NUMBER OF SWAPS PERFORMED. THIS SORT 'IS 17 OR MORE TIMES FASTER THAN A COMPARABLE BUBBLE SORT AND IS ALSO 'FASTER THAN 'THE SHELL SORT THAT I TESTED IT AGAINST. IT MADE FEWER COMPARES AND 'HAD BETTER RUN TIMES. ON A TEST LIST OF 5322 STRING ELEMENTS IT MADE '52 PERCENT AS MANY SWAPS AND RAN IN 29 PERCENT OF THE TIME THAT IT 'TOOK TO SORT THE LIST WITH A VERSION OF THE SHELL SORT THAT I HAVE. ' ' I CHECKED THIS SORT ON MANY LISTS AND FOUND NO ERRORS. I USED TEST 'LISTS OF COMPUTER GENERATED NONSENSE WORDS FROM 2 TO 8 CHARACTERS LONG 'WITH SUCCESIVE NUMBERS FOR THE INTEGER ARRAY. ' ' IT IS A VERY SHORT ROUTINE AND SIMPLE TO WRITE INTO YOUR OWN 'PROGRAMS OR TO USE AS A SUBROUTINE. ' ' THIS PROGRAM MAY BE COPIED FOR PERSONAL USE ONLY AND MAY NOT BE 'USED AS A PART OF OTHER MARKETABLE PROGRAMS WITHOUT PERMISSION. ' ' IF UPLOADED TO ANOTHER BULLETIN BOARD OR PASSED ALONG TO SOMEONE 'ELSE, PLEASE KEEP CR2SORT.DOC TOGETHER WITH THE CR2SORT PROGRAM IN 'THE CR2SORT.ARC VERSION. (couldn't support defunct archive format - sorry) ' 'DAVID J. HANSON '1213 SOUTH 21ST STREET 'FARGO, NORTH DAKOTA 58103 '
' 'SUB CR2SORT (W$(1),N%(1),T%) STATIC 'P#=1.2599211: X=2: B=1: Y#=T\2: WHILE Y#>2: Y#=Y#/P#: WEND 'WHILE X>1: Y#=Y#*P#: X=T\Y# ' FOR I=0 TO T-X ' IF W$(B+I)>W$(X+I) THEN SWAP W$(B+I),W$(X+I):SWAP N%(B+I),N%(X+I) ' NEXT I 'WEND 'END SUB