Some people already benchmarked this against crt's quicksort and quick sort is a lot slower than this. Anyways, this would be great for non-z buffered sw 3d engines.
Happy easter to everyone!!! Yo shockie, sup man?
''Table sort
''A very fast sorting algo
''I use it for like every sorting stuff I need (mostly 3d during the DOS days)
''I heard this was used to sort polys on the playstation
''pros:
''1. No messy swaps, pivot points(Quick), half-pivots(shell)
''2. very fast (fastest general purpose sort algo I've come to know)
''cons:
''1. Maximum value in an element should be <= MAX_DEPTH
''2. More memory needed(directly propotional to MAX_DEPTH)
''
''Relminator 2010
''http://rel.betterwebber.com
''
''
''Notes:
''1. Array to sort need not be ordinal (ie 1,2,3,4... or 2,1,4,3..)
''They can be (1,4,6,11,99).
''
''2. Max Elements of array to sort need not be equal to MAX_DEPTH.
''this algo would work as long as values the array holds <=MAX_DEPTH
''
'' EDIT: duplicate handling added using a very simple implementation
'' of a singly linked-list
'' max number of array elements for our table
const as integer MAX_DEPTH = 1023
'' max array elements for our array to sort
const as integer MAX_ELEM = 127
'' filler for Tsort_table array
'' if Tsort_table(i).index = dummy means that the index is unoccupied
const as integer DUMMY = -(2^16)
'' the sort table
type Tsort_table
index as integer
next_index as Tsort_table ptr '' use this for duplicates ;*)
start_index as Tsort_table ptr '' foo speed
end type
'' our sort table array
dim as Tsort_table s_table(MAX_DEPTH)
'' our unsorted array
dim as integer int_list(MAX_ELEM)
'' clear or table
for i as integer = 0 to MAX_DEPTH
s_table(i).index = dummy
s_table(i).next_index = new Tsort_table ''create list
if s_table(i).next_index = 0 then
print "Out of memory!!!"
''exit(1)
endif
s_table(i).start_index = s_table(i).next_index ''foo speed
next i
randomize
'' our unsorted array
for i as integer = 0 to MAX_ELEM
int_list(i) = int(rnd*MAX_ELEM) '' comment this for a no duplicate array
next i
'' sort via sortable
for i as integer = 0 to MAX_ELEM
'' index in not occupied
if s_table(int_list(i)).index = dummy then
s_table(int_list(i)).index = int_list(i)
else '' index is occupied so make special linked list
'' iterate until found a free element
'' get last index
dim as Tsort_table ptr p_temp
p_temp = @s_table(int_list(i))
while (p_temp->next_index)
p_temp = p_temp->next_index
wend
'' add next
p_temp->next_index = new Tsort_table
if p_temp->next_index = 0 then
print "Out of memory!!!"
''exit(1)
endif
'' fill current with duplicate value
p_temp->index = int_list(i)
s_table(int_list(i)).next_index = p_temp ' increment the list
endif
next i
'' print
print "index","unsorted" , "sorted"
dim as integer j = 0
for i as integer = 0 to MAX_DEPTH
'' print only if our table index is has a value in it ie != 0
if s_table(i).index <> dummy then
print j, int_list(j), s_table(i).index
j += 1
if s_table(i).start_index then
dim as Tsort_table ptr p_stable = s_table(i).start_index
while p_stable->next_index '' do the list
print j,int_list(j),p_stable->index
j += 1
p_stable = p_stable->next_index
wend
endif
endif
next i
sleep
''delete linked list
for i as integer = 0 to MAX_DEPTH
'' delete only if depth buffer <> dummy
if s_table(i).index <> dummy then
dim as Tsort_table ptr p_temp, p_current
p_temp = s_table(i).start_index
p_current = p_temp
while (p_current)
p_temp = p_current
p_current = p_current->next_index
delete p_temp
p_temp = 0
wend
delete s_table(i).start_index
delete s_table(i).next_index
s_table(i).start_index = 0
s_table(i).next_index = 0
endif
next i
end