Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
External sort compo?
#37
Ok, here is my entry for the QB1.1 category.
I'm using a radix sort on the disk, using two auxiliar files, one for input, the other for output. The actual sorting is made in memory, in chunks as big as possible. No aditional merge passes are required.
Due to the ASCII variable length formatting of the input file of this category, there is an aditional de-formatting pass at the start and a reformatting pass at the end.

Code:
'Antoni's entry for the QB1.1 category
'External sort challenge QBN may 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

'data buffers
CONST maxi = 10920
DIM SHARED s1(maxi + 1) AS STRING * 6
DIM SHARED s2(maxi + 1) AS STRING * 6

'mem tallies
DIM SHARED c(-1 TO 9)  AS INTEGER
DIM SHARED d(-1 TO 9) AS INTEGER

'file tallies
DIM SHARED cd(9, 5)  AS LONG

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

PRINT "sorting a file of 1M registers": PRINT

'reformat data to right-aligned
OPEN "qb1data.txt" FOR INPUT AS #1
OPEN f1$ FOR OUTPUT 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

WHILE NOT EOF(1)
   f1 = getunf
   tallybig (f1)
   putstraight (f1)
WEND
acumbig
CLOSE
PRINT TIMER - t!

'radix sort
FOR i = 5 TO 0 STEP -1
    OPEN f1$ FOR INPUT AS #1
    OPEN f2$ FOR OUTPUT AS #2
    WHILE NOT EOF(1)
      f1 = getform
      tallysmall f1, i
      radix i, f1
      putform (i)
    WEND
    CLOSE
    PRINT "pass ";6-i;" of 6 ";TIMER - t!
    SWAP f1$, f2$
NEXT

'format back
KILL f2$

OPEN f1$ FOR INPUT AS #1
OPEN "sort.txt" FOR OUTPUT AS #2
WHILE NOT EOF(1)
   putunf getform
WEND
CLOSE

KILL f1$
PRINT : PRINT "File sorted in "; TIMER - t!; "seconds"
SYSTEM

'-------------------------------------------------
SUB acumbig
FOR i = 0 TO 5
i1& = 0
  FOR j = 0 TO 9
    i2& = cd(j, i): cd(j, i) = i1& * 8 + 1: i1& = i1& + i2&
  NEXT
NEXT
END SUB

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

'--------------------------------------------
FUNCTION getunf
'gets to the output buffer!!!
FOR j = 0 TO maxi
  LINE INPUT #1, t$
  RSET s1(j) = LTRIM$(RTRIM$(t$))
  IF EOF(1) THEN EXIT FOR
NEXT
'PRINT "R";
IF j < maxi THEN getunf = j ELSE getunf = maxi
END FUNCTION

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

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

'----------------------------------------------
SUB putunf (max)
'copy a sorted memory buffer to disk
CONST sp$ = " "
FOR k = 0 TO max
   PRINT #2, sp$ + LTRIM$(s1(k)) + sp$
NEXT
END SUB

'-----------------------------------------------------
SUB radix (digit, max)
offs& = VARPTR(s1(0)) + digit
FOR j = 0 TO max
  t1 = PEEK(offs&)
  IF t1 > 32 THEN t1 = t1 - 48 ELSE t1 = 0
  s2(c(t1)) = s1(j)
  c(t1) = c(t1) + 1
  offs& = offs& + 6
NEXT
END SUB

'----------------------------------------------
SUB tallybig (max)
offs& = VARPTR(s1(0))
FOR j = 0 TO max
   FOR k = 5 TO 0 STEP -1
     t = PEEK(offs& + k)
     IF t > 32 THEN t = t - 48 ELSE t = 0
     cd(t, k) = cd(t, k) + 1
   NEXT
   offs& = offs& + 6
NEXT
END SUB

'--------------------------------------------------
SUB tallysmall (max, digit)
offs& = VARPTR(s1(0)) + digit
ERASE c
FOR j = 0 TO max
     t = PEEK(offs&)
     IF t > 32 THEN t = t - 48 ELSE t = 0
     c(t) = c(t) + 1
     offs& = offs& + 6
NEXT
i1 = 0
FOR j = 0 TO 9
    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: 1 Guest(s)