Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
External sort compo?
#38
My entry for the QB4.5
Same idea, here we skip the reformatting steps, as it is a binary file, and we have only 4 passes of radix sort.
Code:
'Antoni's entry for the QB4.5 category
'QBN Forums External Sort Challenge 5 -2006
'---------------------------------------------
DEFINT A-Z
DECLARE SUB putform (digit%)
DECLARE SUB radix (i%, max%)
DECLARE SUB tallysmall (max%, i%)
DECLARE SUB tallybig (max%)
DECLARE SUB putunf (max%)
DECLARE SUB putstraight (max%)
DECLARE SUB acumbig ()
DECLARE FUNCTION getunf% ()
DECLARE FUNCTION getform% ()

'Qbasic external sort compo entry by Antoni Gual
'uses radix sort

CLEAR
t! = TIMER
'KILL "output.dat"
'data buffers
CONST regsize = 4
CONST passes = 3
CONST numkeys = 255
CONST maxi = 16382
REDIM SHARED s1(maxi) AS LONG
REDIM SHARED s2(maxi) AS LONG

'mem tallies
DIM SHARED c(numkeys)  AS INTEGER
DIM SHARED d(numkeys) AS INTEGER

'file tallies
DIM SHARED cd(numkeys, passes) AS LONG

f1$ = "temp1.dat"
f2$ = "temp2.dat"

'reformat data to right-aligned
OPEN "unsortqb.dat" FOR binary  AS #1
PRINT "Sorting a file of "; LOF(1)\regsize; " records"

'OPEN f1$ FOR binary AS #2

'poke is used to avoid the horrible asc(mid$(a$,i,1))
DEF SEG = VARSEG(s1(0))

'preformat and tally the file offsets

do
     f1 = getform
     tallybig (f1)
     'putstraight (f1)
loop until eof(1)
acumbig
CLOSE
PRINT TIMER - t!
'END

'radix sort
FOR i = 0 TO passes
  if i=0 then
    OPEN "unsortqb.dat" FOR binary  AS #1
  else
      OPEN f1$ FOR binary ACCESS READ AS #1
  end if
    OPEN f2$ FOR binary ACCESS WRITE AS #2
    WHILE NOT EOF(1)
        f1 = getform
        tallysmall f1, i
        radix i, f1
        putform (i)
    WEND
    CLOSE
    PRINT "pass "; i+1; " of 4 "; TIMER - t!
    'END
    SWAP f1$, f2$
NEXT

'format back
NAME f1$ AS "sort.dat"
kill f2$
PRINT : PRINT "File sorted in "; TIMER - t!; "seconds"
SYSTEM

'-------------------------------------------------
SUB acumbig
FOR i = 0 TO passes
i1& = 0
    FOR j = 0 TO numkeys
        i2& = cd(j, i): cd(j, i) = i1&*regsize+1: i1& = i1& + i2&
    NEXT
NEXT
END SUB

'--------------------------------------------
FUNCTION getform
        FOR j = 0 TO maxi
            GET #1,,s1(j)
            IF EOF(1) THEN EXIT FOR
        NEXT
        IF j =< maxi THEN getform = j-1 ELSE getform = maxi
END FUNCTION

'---------------------------------------------
SUB putform (digit)
'copy 10 mem buffers to 10 offsets of temp file
FOR i = 0 TO numkeys
    SEEK #2, cd(i, digit)
    FOR k = d(i) TO c(i) - 1
      PUT #2,,s2(k)
    NEXT
    cd(i, digit)=SEEK(2)
NEXT
END SUB

'----------------------------------------------
SUB putstraight (max)
'copy buffer to disk
FOR k = 0 TO max
     PUT #2,,s1(k)
NEXT
END SUB

'------------------------------------------------
SUB radix (digit, max)
offs& = VARPTR(s1(0)) + digit
FOR j = 0 TO max
    t = PEEK(offs&)
    s2(c(t)) = s1(j)
    c(t) = c(t) + 1
    offs& = offs& + regsize
NEXT
END SUB

'--------------------------------------------------
SUB tallybig (max)
offs& = VARPTR(s1(0))
FOR j = 0 TO max
     FOR k = 0 TO passes
         t = PEEK(offs& + k)
         cd(t, k) = cd(t, k) + 1
     NEXT
     offs& = offs& + regsize
NEXT
END SUB

'--------------------------------------------------
SUB tallysmall (max, digit)
offs& = VARPTR(s1(0)) + digit
ERASE c
FOR j = 0 TO max
         t = PEEK(offs&)
         c(t) = c(t) + 1
         offs& = offs& + regsize
NEXT
i1 = 0
FOR j = 0 TO numkeys
        i2 = c(j): c(j) = i1: d(j) = i1: i1 = i1 + i2
NEXT
END SUB
Antoni
Reply


Messages In This Thread
External sort compo? - by Antoni Gual - 04-25-2006, 08:27 PM
External sort compo? - by Anonymous - 04-25-2006, 09:42 PM
External sort compo? - by Antoni Gual - 04-25-2006, 09:51 PM
External sort compo? - by RyanKelly - 04-26-2006, 06:23 AM
External sort compo? - by Moneo - 04-26-2006, 07:01 AM
External sort compo? - by Anonymous - 04-26-2006, 12:01 PM
External sort compo? - by Antoni Gual - 04-26-2006, 12:47 PM
External sort compo? - by RyanKelly - 04-27-2006, 04:27 AM
External sort compo? - by Moneo - 04-27-2006, 06:07 AM
External sort compo? - by stylin - 04-27-2006, 12:00 PM
External sort compo? - by Antoni Gual - 04-29-2006, 01:27 AM
External sort compo? - by yetifoot - 04-30-2006, 02:37 AM
External sort compo? - by stylin - 04-30-2006, 04:29 AM
External sort compo? - by yetifoot - 04-30-2006, 05:38 AM
External sort compo? - by Moneo - 04-30-2006, 06:12 AM
External sort compo? - by Antoni Gual - 04-30-2006, 02:23 PM
External sort compo? - by yetifoot - 04-30-2006, 09:20 PM
External sort compo? - by stylin - 04-30-2006, 11:45 PM
External sort compo? - by Antoni Gual - 04-30-2006, 11:53 PM
External sort compo? - by Mac - 05-01-2006, 03:34 AM
External sort compo? - by stylin - 05-01-2006, 03:46 AM
External sort compo? - by Mac - 05-01-2006, 03:57 AM
External sort compo? - by Antoni Gual - 05-01-2006, 01:04 PM
External sort compo? - by yetifoot - 05-01-2006, 07:32 PM
External sort compo? - by Antoni Gual - 05-01-2006, 08:09 PM
External sort compo? - by yetifoot - 05-01-2006, 09:00 PM
External sort compo? - by Mac - 05-01-2006, 09:20 PM
External sort compo? - by Antoni Gual - 05-01-2006, 11:25 PM
External sort compo? - by Moneo - 05-02-2006, 10:59 AM
External sort compo? - by stylin - 05-02-2006, 11:34 AM
External sort compo? - by Antoni Gual - 05-02-2006, 12:32 PM
External sort compo? - by Mac - 05-02-2006, 04:31 PM
External sort compo? - by Anonymous - 05-03-2006, 02:56 PM
External sort compo? - by Antoni Gual - 05-03-2006, 03:04 PM
External sort compo? - by Mac - 05-05-2006, 06:29 AM
External sort compo? - by Antoni Gual - 05-05-2006, 12:17 PM
External sort compo? - by Antoni Gual - 05-05-2006, 07:57 PM
External sort compo? - by Antoni Gual - 05-06-2006, 02:18 AM
External sort compo? - by Anonymous - 05-06-2006, 09:10 AM
External sort compo? - by Antoni Gual - 05-06-2006, 01:36 PM
External sort compo? - by yetifoot - 05-07-2006, 10:13 PM
External sort compo? - by Antoni Gual - 05-08-2006, 01:38 AM
External sort compo? - by yetifoot - 05-08-2006, 08:50 PM
External sort compo? - by Antoni Gual - 05-09-2006, 12:20 AM
External sort compo? - by yetifoot - 05-09-2006, 12:43 PM
External sort compo? - by Moneo - 05-10-2006, 05:20 AM
External sort compo? - by Antoni Gual - 05-10-2006, 01:07 PM
External sort compo? - by yetifoot - 05-10-2006, 04:10 PM
External sort compo? - by yetifoot - 05-10-2006, 05:13 PM
External sort compo? - by Moneo - 05-11-2006, 04:16 AM
External sort compo? - by Moneo - 05-11-2006, 04:35 AM
External sort compo? - by anarky - 05-11-2006, 09:57 AM
External sort compo? - by yetifoot - 05-11-2006, 12:18 PM
External sort compo? - by Antoni Gual - 05-11-2006, 12:23 PM
External sort compo? - by Moneo - 05-12-2006, 04:54 AM
External sort compo? - by Antoni Gual - 05-12-2006, 08:01 PM
External sort compo? - by Moneo - 05-14-2006, 04:56 AM
External sort compo? - by Moneo - 05-14-2006, 07:41 AM
External sort compo? - by Antoni Gual - 05-14-2006, 08:54 PM
External sort compo? - by Neo - 05-15-2006, 11:23 PM
External sort compo? - by anarky - 05-16-2006, 11:46 AM
External sort compo? - by Moneo - 05-17-2006, 05:36 AM
External sort compo? - by anarky - 05-17-2006, 06:01 AM
External sort compo? - by Antoni Gual - 05-17-2006, 11:12 AM
External sort compo? - by Neo - 05-17-2006, 11:20 AM
External sort compo? - by Antoni Gual - 05-17-2006, 12:00 PM
External sort compo? - by yetifoot - 05-21-2006, 10:33 PM
External sort compo? - by BigD - 05-23-2006, 07:59 PM
External sort compo? - by Antoni Gual - 05-23-2006, 08:20 PM
External sort compo? - by BigD - 05-23-2006, 08:27 PM
External sort compo? - by Antoni Gual - 05-24-2006, 10:40 AM
External sort compo? - by BigD - 05-24-2006, 12:42 PM
External sort compo? - by Antoni Gual - 05-24-2006, 06:06 PM
External sort compo? - by BigD - 05-24-2006, 07:05 PM
External sort compo? - by Antoni Gual - 05-24-2006, 11:35 PM
External sort compo? - by Neo - 05-25-2006, 02:47 AM
External sort compo? - by BigD - 05-25-2006, 09:40 AM
External sort compo? - by Antoni Gual - 05-25-2006, 10:51 PM
External sort compo? - by Neo - 05-25-2006, 11:04 PM
External sort compo? - by Neo - 05-29-2006, 10:07 PM
External sort compo? - by Antoni Gual - 05-30-2006, 01:06 AM
External sort compo? - by Neo - 05-30-2006, 02:29 AM
External sort compo? - by Anonymous - 05-30-2006, 05:15 AM
External sort compo? - by Neo - 05-30-2006, 12:23 PM
External sort compo? - by Anonymous - 05-30-2006, 12:40 PM
External sort compo? - by yetifoot - 06-01-2006, 06:49 AM
External sort compo? - by Moneo - 06-01-2006, 07:08 AM
External sort compo? - by Antoni Gual - 06-01-2006, 03:22 PM

Forum Jump:


Users browsing this thread: 2 Guest(s)