Code: Select all
#include "crt/stdlib.bi"
Function callback Cdecl(n1 As Any Ptr,n2 As Any Ptr) As Long
Return (*Cptr(long Ptr,n1)) - (*Cptr(long Ptr,n2))
End Function
Sub sort(s() As long)
qsort( @s(Lbound(s)),(Ubound(s)-Lbound(s)+1),Sizeof(s),@callback)
End Sub
'standard
Sub QuickSort(array() As long,begin As long,Finish As long)
Dim As long i=begin,j=finish
Dim As long x =array(((I+J)\2))
While I <= J
While array(I) < X
I+=1
Wend
While array(J) > X
J-=1
Wend
If I<=J Then
Swap array(I),array(J)
I+=1
J-=1
End If
Wend
If J > begin Then QuickSort(array(),begin,J)
If I < Finish Then QuickSort(array(),I,Finish)
End Sub
dim as long limit=10000000
redim as long f(1 to limit)
randomize 1
for n as long=lbound(f) to ubound(f)
f(n)=rnd*1000000-rnd*1000000
next
print "please wait"
dim as double t=timer
sort(f())
print timer-t;" seconds to sort (C qsort) ";limit;" integers, press a key"
sleep
dim as string s
'show some
for n as long=lbound(f) to 100
s+=str(n) + " "+ str(f(n))+chr(10)
next
for n as long=1 to 5
s+="..."+chr(10)
next n
for n as long=ubound(f)-100 to ubound(f)
s+=str(n) + " "+ str(f(n))+chr(10)
next
print s
print "press a key"
sleep
'=============================================== standard quicksort =========
print "please wait"
'Do again
randomize 1
for n as long=lbound(f) to ubound(f)
f(n)=rnd*1000000-rnd*1000000
next
t=timer
quicksort(f(),lbound(f),ubound(f))
print timer-t;" seconds to sort (Quicksort) ";limit;" integers, press a key"
sleep
s=""
'show some
for n as long=lbound(f) to 100
s+=str(n) + " "+ str(f(n))+chr(10)
next
for n as long=1 to 5
s+="..."+chr(10)
next n
for n as long=ubound(f)-100 to ubound(f)
s+=str(n) + " "+ str(f(n))+chr(10)
next
print s
print "press a key"
sleep