Code: Select all
'lrcvs 01.11.17
'Program to eliminate repeated elements in an array.
'1) we sort the initial array, using quicksort.
'2) we remove the repeated elements.
'3) skipping the ones that are the same and reducing the time of "cleaning the repeated elements".
DECLARE SUB ordenar2 (s()AS INTEGER,inicio AS INTEGER,final AS INTEGER)
DIM AS INTEGER c,f,k,n,x
CLS
x = 3000000 '<<< elements
DIM s (x)AS INTEGER
DIM t (x)AS INTEGER
RANDOMIZE, 3
FOR n = 1 TO x
s(n) = INT(RND * x) + 1
NEXT n
PRINT "Init array of "; x; " elements, some repeated"
ordenar2 (s(),1,x) '<<< quicksort
'******************************************************************************
'Here remove elements repeted
f = 1
c = 1
WHILE f <= x
k = 0
IF s(f) = s(f + 1) THEN
k = 1
f = f + 1
ELSE
t(c) = s(f)
f = f + 1
c = c + 1
END IF
WEND
'******************************************************************************
PRINT
PRINT "here are ";c-1;" unique elements"
PRINT
PRINT "end"
SLEEP
END
SUB ordenar2 (s()AS INTEGER,inicio AS INTEGER,final AS INTEGER)
'quick sort
DIM AS INTEGER i,j,y
i = inicio
j = final
y = s((INT(i + j) / 2))
WHILE i < j
WHILE s(i) < y
i = i + 1
WEND
WHILE s(j) > y
j = j - 1
WEND
IF i <= j THEN
SWAP s(i), s(j)
i = i + 1
j = j - 1
END IF
WEND
IF j > inicio THEN ordenar2(s(),inicio,j)
IF i < final THEN ordenar2(s(),i,final)
END SUB
Regards