• Print

Author Topic: help with error  (Read 115 times)

23452

  • Jr. Member
  • **
  • Posts: 72
    • Email
help with error
« on: October 22, 2012, 08:20:42 PM »
Hey remember how we were sorting arrays. Well  I tried to make the comb one faster, but I am getting a lot of errors. Can someone help. Just skip to the one that says Mysub.



limit = 99999

DIM x(limit) AS _INTEGER64
RANDOMIZE TIMER
PRINT "Initializing Data"
FOR i = 0 TO limit
    x(i) = RND(1) * 1234567890987654321
NEXT


t# = TIMER(0.001)

PRINT "Myway": Myway x()
' PRINT "Bubble Sorting Data": bubblesort x()
'PRINT "Comb Sorting Data" : combsort x()



t1# = TIMER(0.001)
PRINT USING "Data Sorted in ##,###.##### seconds."; t1# - t#
SLEEP
stepper = 20

FOR i = 0 TO limit - stepper STEP stepper
    FOR j = i TO i + stepper
        PRINT x(j)
    NEXT
    'SLEEP
NEXT


SUB combsort (array() AS _INTEGER64)
gap = UBOUND(array)

DO
    gap = INT(gap / 1.247330950103979)
    IF gap < 1 THEN gap = 1
    i = 0
    swapped = false
    DO
        IF array(i) > array(i + gap) THEN
            SWAP array(i), array(i + gap)
            swapped = true
        END IF
        i = i + 1
    LOOP UNTIL i + gap > UBOUND(array)
LOOP UNTIL gap = 1 AND swapped = false
END SUB

SUB bubblesort (array() AS _INTEGER64)
nr = UBOUND(array)
FOR k = 1 TO nr
    FOR i = k TO nr
        IF array(k) > array(i) THEN SWAP array(k), array(i)
    NEXT
NEXT
END SUB


SUB Myway (array() AS _INTEGER64)
nr1 = UBOUND(array)
FOR n = 1 TO nr1
    IF array(n) < 9 THEN
        array0to9(n0to9) = array(n)
        n0to9 = n0to9 + 1

    ELSEIF array(n) < 99 THEN
        array10to99(n10to99) = array(n)
        n10to99 = n10to99 + 1

    ELSEIF array(n) < 999 THEN
        array100to999(n100to999) = array(n)
        n100to9 = n100to999 + 1

    ELSEIF array(n) < 9999 THEN
        array1000to9999(n1000to9999) = array(n)
        n1000to9999 = n1000to9999 + 1

    ELSEIF array(n) < 99999 THEN
        array999to999(n10000to99999) = array(n)
        n1to9 = n10000to99999 + 1
    END IF

NEXT n

gap = UBOUND(array0to9)

DO
    gap = INT(gap / 1.247330950103979)
    IF gap < 1 THEN gap = 1
    i = 0
    swapped = false
    DO
        IF array0to9(i) > array0to9(i + gap) THEN
            SWAP array0to9(i), array0to9(i + gap)
            swapped = true
        END IF
        i = i + 1
    LOOP UNTIL i + gap > UBOUND(array0to9)
LOOP UNTIL gap = 1 AND swapped = false


gap = UBOUND(array10to99)

DO
    gap = INT(gap / 1.247330950103979)
    IF gap < 1 THEN gap = 1
    i = 0
    swapped = false
    DO
        IF array10to99(i) > array10to99(i + gap) THEN
            SWAP array10to99(i), array10to99(i + gap)
            swapped = true
        END IF
        i = i + 1
    LOOP UNTIL i + gap > UBOUND(array10to99)
LOOP UNTIL gap = 1 AND swapped = false

gap = UBOUND(array100to999)

DO
    gap = INT(gap / 1.247330950103979)
    IF gap < 1 THEN gap = 1
    i = 0
    swapped = false
    DO
        IF array100to999(i) > array100to999(i + gap) THEN
            SWAP array100to999(i), array100to999(i + gap)
            swapped = true
        END IF
        i = i + 1
    LOOP UNTIL i + gap > UBOUND(array100to999)
LOOP UNTIL gap = 1 AND swapped = false


gap = UBOUND(array1000to9999)

DO
    gap = INT(gap / 1.247330950103979)
    IF gap < 1 THEN gap = 1
    i = 0
    swapped = false
    DO
        IF array10000to99999(i) > array(i + gap) THEN
            SWAP array10000to99999(i), array10000to99999(i + gap)
            swapped = true
        END IF
        i = i + 1
    LOOP UNTIL i + gap > UBOUND(array)
LOOP UNTIL gap = 1 AND swapped = false

END SUB







Clippy

  • Hero Member
  • *****
  • Posts: 16431
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: help with error
« Reply #1 on: October 22, 2012, 08:24:20 PM »
Please use code box # button.
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

23452

  • Jr. Member
  • **
  • Posts: 72
    • Email
Re: help with error
« Reply #2 on: October 22, 2012, 08:26:15 PM »
like this?
Code: [Select]
limit = 99999

DIM x(limit) AS _INTEGER64
RANDOMIZE TIMER
PRINT "Initializing Data"
FOR i = 0 TO limit
    x(i) = RND(1) * 1234567890987654321
NEXT


t# = TIMER(0.001)

PRINT "Myway": Myway x()
' PRINT "Bubble Sorting Data": bubblesort x()
'PRINT "Comb Sorting Data" : combsort x()



t1# = TIMER(0.001)
PRINT USING "Data Sorted in ##,###.##### seconds."; t1# - t#
SLEEP
stepper = 20

FOR i = 0 TO limit - stepper STEP stepper
    FOR j = i TO i + stepper
        PRINT x(j)
    NEXT
    'SLEEP
NEXT


SUB combsort (array() AS _INTEGER64)
gap = UBOUND(array)

DO
    gap = INT(gap / 1.247330950103979)
    IF gap < 1 THEN gap = 1
    i = 0
    swapped = false
    DO
        IF array(i) > array(i + gap) THEN
            SWAP array(i), array(i + gap)
            swapped = true
        END IF
        i = i + 1
    LOOP UNTIL i + gap > UBOUND(array)
LOOP UNTIL gap = 1 AND swapped = false
END SUB

SUB bubblesort (array() AS _INTEGER64)
nr = UBOUND(array)
FOR k = 1 TO nr
    FOR i = k TO nr
        IF array(k) > array(i) THEN SWAP array(k), array(i)
    NEXT
NEXT
END SUB


SUB Myway (array() AS _INTEGER64)
nr1 = UBOUND(array)
FOR n = 1 TO nr1
    IF array(n) < 9 THEN
        array0to9(n0to9) = array(n)
        n0to9 = n0to9 + 1

    ELSEIF array(n) < 99 THEN
        array10to99(n10to99) = array(n)
        n10to99 = n10to99 + 1

    ELSEIF array(n) < 999 THEN
        array100to999(n100to999) = array(n)
        n100to9 = n100to999 + 1

    ELSEIF array(n) < 9999 THEN
        array1000to9999(n1000to9999) = array(n)
        n1000to9999 = n1000to9999 + 1

    ELSEIF array(n) < 99999 THEN
        array999to999(n10000to99999) = array(n)
        n1to9 = n10000to99999 + 1
    END IF

NEXT n

gap = UBOUND(array0to9)

DO
    gap = INT(gap / 1.247330950103979)
    IF gap < 1 THEN gap = 1
    i = 0
    swapped = false
    DO
        IF array0to9(i) > array0to9(i + gap) THEN
            SWAP array0to9(i), array0to9(i + gap)
            swapped = true
        END IF
        i = i + 1
    LOOP UNTIL i + gap > UBOUND(array0to9)
LOOP UNTIL gap = 1 AND swapped = false


gap = UBOUND(array10to99)

DO
    gap = INT(gap / 1.247330950103979)
    IF gap < 1 THEN gap = 1
    i = 0
    swapped = false
    DO
        IF array10to99(i) > array10to99(i + gap) THEN
            SWAP array10to99(i), array10to99(i + gap)
            swapped = true
        END IF
        i = i + 1
    LOOP UNTIL i + gap > UBOUND(array10to99)
LOOP UNTIL gap = 1 AND swapped = false

gap = UBOUND(array100to999)

DO
    gap = INT(gap / 1.247330950103979)
    IF gap < 1 THEN gap = 1
    i = 0
    swapped = false
    DO
        IF array100to999(i) > array100to999(i + gap) THEN
            SWAP array100to999(i), array100to999(i + gap)
            swapped = true
        END IF
        i = i + 1
    LOOP UNTIL i + gap > UBOUND(array100to999)
LOOP UNTIL gap = 1 AND swapped = false


gap = UBOUND(array1000to9999)

DO
    gap = INT(gap / 1.247330950103979)
    IF gap < 1 THEN gap = 1
    i = 0
    swapped = false
    DO
        IF array10000to99999(i) > array(i + gap) THEN
            SWAP array10000to99999(i), array10000to99999(i + gap)
            swapped = true
        END IF
        i = i + 1
    LOOP UNTIL i + gap > UBOUND(array)
LOOP UNTIL gap = 1 AND swapped = false

END SUB







TerryRitchie

  • Hero Member
  • *****
  • Posts: 2264
  • FORMAT C:\ /Q /U /AUTOTEST (How to repair Win8)
    • Email
Re: help with error
« Reply #3 on: October 22, 2012, 08:28:11 PM »
Yep, makes it more readable (monospaced font) and selectable to be copied for pasting into the IDE.

SMcNeill

  • Hero Member
  • *****
  • Posts: 2414
    • Email
Re: help with error
« Reply #4 on: October 23, 2012, 09:10:26 PM »
One thing you need is a const for true and false.

Const true = -1
Const false = 0

As it is, your routine always thinks its finished sorting and can exit prematurely. 
http://bit.ly/TextImage -- Library of QB64 code to manipulate text and images, as a BM library.
http://bit.ly/Color32 -- A set of color CONST for use in 32 bit mode, as a BI library.

http://bit.ly/DataToDrive - A set of routines to quickly and easily get data to and from the disk.  BI and BM files

23452

  • Jr. Member
  • **
  • Posts: 72
    • Email
Re: help with error
« Reply #5 on: October 23, 2012, 09:33:16 PM »
I fixed a lot of the simple mistakes and made it easier to read, but is is still not working right.
It works in 3 parts.
 
Part one divides the array( I used x10 because it was faster to type)
Part two uses a comb sort on each array
Part 3 writes all the arrays back into the original.

Could you explain the const true and false thing; I nominally just use 1 and 0 but I was coping and pasting parts of other code a lot with this.

Code: [Select]
limit = 10000

DIM x(limit) AS _INTEGER64
RANDOMIZE TIMER
PRINT "Initializing Data"
FOR i = 0 TO limit
    x(i) = RND(1) * 12345678
NEXT


t# = TIMER(0.001)
PRINT "mysub": mysub x()
'PRINT "Bubble Sorting Data": bubblesort x()
'PRINT "Comb Sorting Data": combsort x()


t1# = TIMER(0.001)
PRINT USING "Data Sorted in ##,###.##### seconds."; t1# - t#
SLEEP
stepper = 20

FOR i = 0 TO limit - stepper STEP stepper
    FOR j = i TO i + stepper
        PRINT x(j)
    NEXT
    'SLEEP
NEXT


SUB combsort (array() AS _INTEGER64)
gap = UBOUND(array)

DO
    gap = INT(gap / 1.247330950103979)
    IF gap < 1 THEN gap = 1
    i = 0
    swapped = false
    DO
        IF array(i) > array(i + gap) THEN
            SWAP array(i), array(i + gap)
            swapped = true
        END IF
        i = i + 1
    LOOP UNTIL i + gap > UBOUND(array)
LOOP UNTIL gap = 1 AND swapped = false
END SUB

SUB bubblesort (array() AS _INTEGER64)
nr = UBOUND(array)
FOR k = 1 TO nr
    FOR i = k TO nr
        IF array(k) > array(i) THEN SWAP array(k), array(i)
    NEXT
NEXT
END SUB
SUB mysub (array() AS _INTEGER64)
x = 10000
DIM array10(x)
DIM array100(x)
DIM array1000(x)
DIM array10000(x)
DIM array100000(x)
DIM array1000000(x)
DIM array10000000(x)

DIM n10 AS _INTEGER64: n10 = 1
DIM n100 AS _INTEGER64: n100 = 1
DIM n1000 AS _INTEGER64: n1000 = 1
DIM n10000 AS _INTEGER64: n10000 = 1
DIM n100000 AS _INTEGER64: n100000 = 1
DIM n1000000 AS _INTEGER64: n1000000 = 1
DIM n10000000 AS _INTEGER64: n10000000 = 1


FOR n = 0 TO x
    IF array(n) < 10 THEN
        array10(n10) = array(n)
        n10 = n10 + 1

    ELSEIF array(n) < 100 THEN
        array100(n100) = array(n)
        n100 = n100 + 1

    ELSEIF array(n) < 1000 THEN
        array1000(n1000) = array(n)
        n1000 = n1000 + 1

    ELSEIF array(n) < 10000 THEN

        array10000(n10000) = array(n)
        n10000 = n10000 + 1

    ELSEIF array(n) < 100000 THEN
        array100000(n100000) = array(n)
        n100000 = n100000 + 1

    ELSEIF array(n) < 1000000 THEN
        array1000000(n1000000) = array(n)
        n1000000 = n1000000 + 1

    ELSEIF array(n) < 10000000 THEN

        array10000000(n10000000) = array(n)
        n10000000 = n10000000 + 1
    END IF
NEXT n

gap = UBOUND(array10)

DO
    gap = INT(gap / 1.247330950103979)
    IF gap < 1 THEN gap = 1
    i = 0
    swapped = false
    DO
        IF array10(i) > array10(i + gap) THEN
            SWAP array10(i), array10(i + gap)
            swapped = true
        END IF
        i = i + 1
    LOOP UNTIL i + gap > UBOUND(array)
LOOP UNTIL gap = 1 AND swapped = false

gap = UBOUND(array100)

DO
    gap = INT(gap / 1.247330950103979)
    IF gap < 1 THEN gap = 1
    i = 0
    swapped = false
    DO
        IF array100(i) > array100(i + gap) THEN
            SWAP array100(i), array100(i + gap)
            swapped = true
        END IF
        i = i + 1
    LOOP UNTIL i + gap > UBOUND(array)
LOOP UNTIL gap = 1 AND swapped = false

gap = UBOUND(array1000)

DO
    gap = INT(gap / 1.247330950103979)
    IF gap < 1 THEN gap = 1
    i = 0
    swapped = false
    DO
        IF array1000(i) > array1000(i + gap) THEN
            SWAP array1000(i), array1000(i + gap)
            swapped = true
        END IF
        i = i + 1
    LOOP UNTIL i + gap > UBOUND(array)
LOOP UNTIL gap = 1 AND swapped = false

gap = UBOUND(array10000)

DO
    gap = INT(gap / 1.247330950103979)
    IF gap < 1 THEN gap = 1
    i = 0
    swapped = false
    DO
        IF array10000(i) > array10000(i + gap) THEN
            SWAP array10000(i), array10000(i + gap)
            swapped = true
        END IF
        i = i + 1
    LOOP UNTIL i + gap > UBOUND(array)
LOOP UNTIL gap = 1 AND swapped = false

gap = UBOUND(array100000)

DO
    gap = INT(gap / 1.247330950103979)
    IF gap < 1 THEN gap = 1
    i = 0
    swapped = false
    DO
        IF array100000(i) > array10(i + gap) THEN
            SWAP array100000(i), array100000(i + gap)
            swapped = true
        END IF
        i = i + 1
    LOOP UNTIL i + gap > UBOUND(array)
LOOP UNTIL gap = 1 AND swapped = false

gap = UBOUND(array1000000)

DO
    gap = INT(gap / 1.247330950103979)
    IF gap < 1 THEN gap = 1
    i = 0
    swapped = false
    DO
        IF array1000000(i) > array1000000(i + gap) THEN
            SWAP array1000000(i), array1000000(i + gap)
            swapped = true
        END IF
        i = i + 1
    LOOP UNTIL i + gap > UBOUND(array)
LOOP UNTIL gap = 1 AND swapped = false

gap = UBOUND(array10000000)

DO
    gap = INT(gap / 1.247330950103979)
    IF gap < 1 THEN gap = 1
    i = 0
    swapped = false
    DO
        IF array10000000(i) > array10000000(i + gap) THEN
            SWAP array10000000(i), array10000000(i + gap)
            swapped = true
        END IF
        i = i + 1
    LOOP UNTIL i + gap > UBOUND(array)
LOOP UNTIL gap = 1 AND swapped = false

n = 0

FOR num = 1 TO n10
    array(n) = array100(num)
    n = n + 1
NEXT num

FOR num = 1 TO n100
    array(n) = array100(num)
    n = n + 1
NEXT num

FOR num = 1 TO n1000
    array(n) = array1000(num)
    n = n + 1
NEXT num

FOR num = 1 TO n10000
    array(n) = array10000(num)
    n = n + 1
NEXT num

FOR num = 1 TO n100000
    array(n) = array100000(num)
    n = n + 1
NEXT num

FOR num = 1 TO n1000000
    array(n) = array1000000(num)
    n = n + 1
NEXT num

FOR num = 1 TO n10000000
    array(n) = array10000000(num)
    n = n + 1
NEXT num

END SUB
   
 

  • Print