'Radix Sorting procedure Demo.
'Based on multitudes of other examples in C, JS, and Python that I studied.
'Sorts 1000000 (the max a this setup will accomodate) in a little over .5 seconds.
'By: Phlashlite
'For demo======================================================================
Const ARRAY_SIZE
= WDTH
* HGHT
Dim Shared DemoArray
(ARRAY_SIZE
) As Long 'Used in procedures___________________
DemoArray
(i&
) = Int(Rnd * 255) DemoArrayCopy(i&) = DemoArray(i&)
i& = 0
PSet (x&
, y&
), _RGB(DemoArray
(i&
), DemoArray
(i&
), DemoArray
(i&
)) i& = i& + 1
Input "Press ENTER to Sort Array", a$
'==============================================================================
'Used in procedures____________________________________________________________
ArrayLength = ARRAY_LENGTH(DemoArray())
RADIX_SORT DemoArray()
Delta = tf - ts
'______________________________________________________________________________
'==============================================================================
i& = 0
PSet (x&
, y&
), _RGB(DemoArray
(i&
), DemoArray
(i&
), DemoArray
(i&
)) i& = i& + 1
Print "Set of 1000000 members sorted in"; Delta;
"seconds."
'==============================================================================
'Restore the background to the original state
i& = 0
PSet (x&
, y&
), _RGB(DemoArrayCopy
(i&
), DemoArrayCopy
(i&
), DemoArrayCopy
(i&
)) i& = i& + 1
Sort m
Delta = tf - ts
'______________________________________________________________________________
'==============================================================================
i& = 0
PSet (x&
, y&
), _RGB(DemoArray
(i&
), DemoArray
(i&
), DemoArray
(i&
)) i& = i& + 1
Print "Set of 1000000 members sorted in"; Delta;
"seconds."
'______________________________________________________________________________
'Convert our offset data over to something we can work with
_MemPut m1
, m1.OFFSET
, m.ELEMENTSIZE:
_MemGet m1
, m1.OFFSET
, ES
'Element Size _MemPut m1
, m1.OFFSET
, m.SIZE:
_MemGet m1
, m1.OFFSET
, EC
'Element Count will temporily hold the WHOLE array size
EC = EC / ES - 1 'Now we take the whole element size / the size of the elements and get our actual element count. We subtract 1 so our arrays start at 0 and not 1.
'And work with it!
i = 0
temp1(t1) = temp1(t1) + 1
i = i + 1
i1 = -128
counter = counter + 1
temp1(i1) = temp1(i1) - 1
i1 = i1 + 1
i = 0
temp2(t2) = temp2(t2) + 1
i = i + 1
i1 = -32768
counter = counter + 1
temp2(i1) = temp2(i1) - 1
i1 = i1 + 1
gap = EC
gap = 10 * gap \ 13
i = 0
swapped = 0
o = m.OFFSET + i * 4
o1 = m.OFFSET + (i + gap) * 4
swapped = -1
i = i + 1
gap = EC
gap = 10 * gap \ 13
i = 0
swapped = 0
o = m.OFFSET + i * 4
o1 = m.OFFSET + (i + gap) * 4
swapped = -1
i = i + 1
gap = EC
gap = 10 * gap \ 13
i = 0
swapped = 0
o = m.OFFSET + i * 8
o1 = m.OFFSET + (i + gap) * 8
swapped = -1
i = i + 1
gap = EC
gap = 10 * gap \ 13
i = 0
swapped = 0
o = m.OFFSET + i * 32
o1 = m.OFFSET + (i + gap) * 32
swapped = -1
i = i + 1
gap = EC
gap
= Int(gap
/ 1.247330950103979) i = 0
swapped = 0
o = m.OFFSET + i * ES
o1 = m.OFFSET + (i + gap) * ES
T7c = T7b
swapped = -1
i = i + 1
gap = EC
gap = 10 * gap \ 13
i = 0
swapped = 0
o = m.OFFSET + i * 8
o1 = m.OFFSET + (i + gap) * 8
swapped = -1
i = i + 1
Case 11:
'_UNSIGNED _BYTE i = 0
temp11(t11) = temp11(t11) + 1
i = i + 1
i1 = 0
counter = counter + 1
temp11(i1) = temp11(i1) - 1
i1 = i1 + 1
Case 12 '_UNSIGNED INTEGER i = 0
temp12(t12) = temp12(t12) + 1
i = i + 1
i1 = 0
counter = counter + 1
temp12(i1) = temp12(i1) - 1
i1 = i1 + 1
gap = EC
gap = 10 * gap \ 13
i = 0
swapped = 0
o = m.OFFSET + i * 4
o1 = m.OFFSET + (i + gap) * 4
swapped = -1
i = i + 1
Case 18:
'_UNSIGNED _INTEGER64 gap = EC
gap = 10 * gap \ 13
i = 0
swapped = 0
o = m.OFFSET + i * 8
o1 = m.OFFSET + (i + gap) * 8
swapped = -1
i = i + 1
'Sub for radix sort
'Find largest element in the Array
MaxArrayValue = ARRAY_MAX_VALUE(Array(), ArrayLength)
'Counting sort is performed based on Place, like ones Place, tens Place and so on.
Place = 1
While MaxArrayValue \ Place
> 0 Call COUNT_SORT
(Array
(), Place
) Place = Place * 10
'_______________________________________________________________________________
'Range of the number is 0-9 for each Place considered.
'Enumerate Place member occurrences in Count()
For i&
= 0 To ArrayLength
- 1 Index = Array(i&) \ Place
Count
(Index
Mod 10) = Count
(Index
Mod 10) + 1
'Change Count() so that Count() now contains actual Place position of this
'each digit in OwtPut()
Count(i&) = Count(i&) + Count(i& - 1)
'Build the OwtPut() array
i& = ArrayLength - 1
Index& = Array&(i&) \ Place
OwtPut
(Count
(Index&
Mod 10) - 1) = Array
(i&
) Count
(Index&
Mod 10) = Count
(Index&
Mod 10) - 1 i& = i& - 1
i& = 0
For i&
= 0 To ArrayLength
- 1 Array(i&) = OwtPut(i&)
'______________________________________________________________________________
'Find the largest member of an array set.
For i&
= 0 To ArrayLength
If Array&
(i&
) > tmx&
Then tmx&
= Array&
(i&
) ARRAY_MAX_VALUE = tmx&
'______________________________________________________________________________
'Calculate the size of an array.