Code: Select all
#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
Using FB '' Screen mode flags are in the FB namespace in lang FB
#endif
'' Sets screen mode 18 (640*480) with 32bpp color depth and 4 pages, in windowed mode; switching disabled
'Screen 18, 32, 4, (GFX_WINDOWED Or GFX_NO_SWITCH)
#Define inc += 1
#Define dec -= 1
#Define hres 1024
#Define vres 600
#Define mode1 1
#Define mode2 2
#Define mode3 4
Screenres hres, vres, 8, 2
Screenset 1, 0
'dim as String k, buff
Dim as Long a(hres)
dim shared as Long dcmp, dswap, plot, sync = 0, r=1
dim Shared as String dmeth
dim as Long modes(4) => {0,1,2,4}
dim Shared as String smodes(4) '=> {"off", "mode1", "mode2", "mode3"}
smodes(0)="off":smodes(1)="mode1":smodes(2)="mode2":smodes(3)="mode3"
sub display(a() as Long)
Cls
for i as Long = 0 to Ubound(a)
if plot = 0 Then
Pset (i,a(i)+vres\4)
Else
Line (i,(a(i)\2+vres\2))-(i,(-a(i)\2+vres\2))
End If
Next
Locate 3,100:Print "comparaciones: " & dcmp
Locate 4,100:Print "intercambios: " & dswap
locate 5,100:Print "metodo: " & dmeth
Locate 6,100:print smodes(sync)
if sync = 1 Then Screensync
dim as String s=Inkey
if s=" " Then 'Multikey(SC_S) Then
sync inc
sync mod= 3
End If
Screencopy
Sleep 1,1
End Sub
Sub bubble_sort( a() as long, l as Long, h as Long )
Dim as Boolean flag = true
dcmp = 0
dswap = 0
dmeth = "bubble"
While 1
flag=true
for i as Long = 0 to Ubound(a)-1
if a(i)<a(i+1) Then
Swap a(i), a(i+1)
dswap inc
flag=False
End If
dcmp inc
if sync = 2 Then display(a())
if Multikey(SC_ESCAPE) Then Exit Sub
Next
display(a())
if flag Then exit While
Wend
End Sub
sub bubble2_sort( a() as Long, l as Long, h as Long )
dcmp = 0
dswap = 0
dmeth = "bubble2"
for i as Long = 0 to h-1
for j as Long = 0 to (h-(i+1))
if a(j)<a(j+1) Then
Swap a(j), a(j+1)
dswap inc
End If
dcmp inc
if sync = 2 Then display(a())
if Multikey(SC_ESCAPE) Then Exit Sub
Next
display(a())
Next
End Sub
Sub select_sort( a() as Long, l as Long, h as Long )
dcmp = 0
dswap = 0
dmeth = "select"
For i as Long = 0 to Ubound(a)-1
dim as long k = i
for j as Long = i+1 to Ubound(a)
if a(j)>a(k) Then k = j
dcmp inc
if sync = 2 Then display(a())
if Multikey(SC_ESCAPE) Then Exit Sub
Next
Swap a(k), a(i)
dswap inc
display(a())
Next
End Sub
sub insert_sort( a() as Long, l as Long, h as Long )
dcmp = 0
dswap = 0
dmeth = "insertion"
for i as Long = 1 to Ubound(a)
dim as long key = a(i)
dim as long j = i - 1
while j>=0 Andalso a(j)<key
a(j+1) = a(j)
j -= 1
dswap inc
dcmp inc
if sync = 2 Then display(a())
if Multikey(SC_ESCAPE) Then Exit Sub
Wend
a(j+1) = key
dswap inc
display(a())
Next
End Sub
sub shell_sort(a() as Long, l as Long, h as Long )
dcmp = 0
dswap = 0
dmeth = "shell"
dim as Long inter = 5
While inter > 0
dim as Long j
for i as Long = l to h
dim as Long key = a(i)
j = i
While j >= inter Andalso a(j-inter) <= key
a(j) = a(j-inter)
j = j - inter
dcmp inc
dswap inc
if sync = 2 Then display(a())
if Multikey(SC_ESCAPE) Then Exit Sub
Wend
a(j) = key
dswap inc
display(a())
Next
inter = ((inter-1)/5)
Wend
End Sub
sub _merge( a() as Long, aa() as Long, l as Long, m as Long, h as Long )
dim as long l1, l2, i
l1 = l
l2 = m + 1
i = l
while l1 <= m Andalso l2 <= h
if a(l1) >= a(l2) Then
aa(i) = a(l1)
l1 inc
Else
aa(i) = a(l2)
l2 inc
End If
dswap inc
dcmp inc
i inc
if sync = 2 Then display(a())
if Multikey(SC_ESCAPE) Then Exit Sub
Wend
While l1 <= m
aa(i) = a(l1)
i inc
l1 inc
dswap inc
if sync = 2 Then display(a())
if Multikey(SC_ESCAPE) Then Exit Sub
Wend
While l2 <= h
aa(i) = a(l2)
i inc
l2 inc
dswap inc
if sync = 2 Then display(a())
if Multikey(SC_ESCAPE) Then Exit Sub
Wend
for i = l to h
a(i) = aa(i)
Next
display(a())
End Sub
sub _merge_rec_sort( a() as Long, aa() as Long, l as Long, h as Long )
if l < h Then
if Multikey(SC_ESCAPE) Then Exit Sub
dim as Long m = (l+h)\2
_merge_rec_sort(a(), aa(), l, m)
_merge_rec_sort(a(), aa(), m+1, h)
_merge(a(), aa(), l, m, h)
End If
End Sub
sub merge_rec_sort( a() as Long, l as Long, h as Long )
dcmp = 0
dswap = 0
dmeth = "merge (recursive)"
dim as Long aa(hres)
_merge_rec_sort( a(), aa(), l, h )
End Sub
Function _min( a as Long, b as Long ) as Long
return Iif( a < b, a, b )
End Function
sub _merge_ite_sort( a() as Long, aa() as Long, l as Long, h as Long )
dim as Long curr_size, lstart
curr_size = 1
While curr_size <= h-1
lstart = 0
if Multikey(SC_ESCAPE) Then Exit Sub
While lstart < h-1
dim as long m = lstart + curr_size - 1
dim as long rend = _min(lstart + 2*curr_size - 1, h - 1 )
_merge( a(), aa(), lstart, m, rend )
lstart += (2*curr_size)
Wend
curr_size *= 2
Wend
End Sub
sub merge_ite_sort( a() as Long, l as Long, h as Long )
dcmp = 0
dswap = 0
dmeth = "merge (iterative)"
dim as Long aa(hres)
_merge_ite_sort( a(), aa(), l, h )
End Sub
Sub _quick_rec_sort( a() as Long, l as Long, h as Long )
dim as long key, i, j, k = (l+h)\2
if l < h Then
if Multikey(SC_ESCAPE) Then Exit Sub
Swap a(l),a(k)
dswap inc
key = a(l)
i = l+1
j = h
While i<=j
while i<=h Andalso a(i)>=key
i inc
dcmp inc
if sync = 2 Then display(a())
if Multikey(SC_ESCAPE) Then Exit Sub
Wend
While j >= l Andalso a(j)<key
j -= 1
dcmp inc
if sync = 2 Then display(a())
if Multikey(SC_ESCAPE) Then Exit Sub
Wend
if i < j Then
Swap a(i),a(j)
dswap inc
if sync = 2 Then display(a())
if Multikey(SC_ESCAPE) Then Exit Sub
End If
display(a())
Wend
Swap a(l),a(j)
dswap inc
display(a())
_quick_rec_sort( a(), l, j-1 )
_quick_rec_sort( a(), j+1, h )
end If
End Sub
sub quick_rec_sort( a() as Long, l as Long, h as Long )
dcmp = 0
dswap = 0
dmeth = "quick (recursive)"
_quick_rec_sort(a(),l,h-1)
End Sub
#Define MAX_LEVELS 64
Function _quick_ite_sort( a() as Long, l as Long, h as Long ) as Long
dim as Long ll, rr, i, pstart(MAX_LEVELS), pend(MAX_LEVELS)
pstart(0) = 0
pend(0) = h
While i >= 0
ll = pstart(i)
rr = pend(i)
if (rr - ll) > 1 Then
dim as Long m = ll+((rr-ll) Shr 1)
dim as Long p = a(m)
a(m) = a(ll)
dswap inc
if i = MAX_LEVELS - 1 Then Return -1
rr dec
While ll < rr
while a(rr) <= p Andalso ll < rr
rr dec
dcmp inc
if sync = 2 Then display(a())
if Multikey(SC_ESCAPE) Then Exit Function
Wend
if ll < rr Then
a(ll) = a(rr)
ll inc
dswap inc
End If
While a(ll) >= p Andalso ll < rr
ll inc
dcmp inc
if sync = 2 Then display(a())
if Multikey(SC_ESCAPE) Then Exit Function
Wend
if ll < rr Then
a(rr) = a(ll)
rr dec
dswap inc
End If
display( a() )
Wend
a(ll) = p
dswap inc
m = ll + 1
While ll > pstart(i) Andalso a(ll -1) = p
ll dec
dcmp inc
if sync = 2 Then display(a())
if Multikey(SC_ESCAPE) Then Exit Function
Wend
While m < pend(i) Andalso a(m) = p
m inc
dcmp inc
if sync = 2 Then display(a())
if Multikey(SC_ESCAPE) Then Exit Function
Wend
if ll - pstart(i) > pend(i) - m Then
pstart(i+1) = m
pend(i+1) = pend(i)
pend(i) = ll
i inc
Else
pstart(i+1) = pstart(i)
pend(i+1) = ll
pstart(i) = m
i inc
End If
Else
i dec
End If
display( a() )
Wend
Return 0
End Function
sub quick_ite_sort( a() as Long, l as Long, h as Long )
dcmp = 0
dswap = 0
dmeth = "quick (iterative)"
_quick_ite_sort(a(),l,h)
End Sub
sub rrandom( a() as Long, s as Long = 1000)
Randomize Iif(s,1000,timer)
for i as Long = Lbound(a) to Ubound(a)
a(i)=rnd*(vres\2)
Next
End Sub
'for i as Long = 0 to 19
' ?a(i);
'Next
'?
Do
locate 1,2:? "1.- Bubble"
locate 2,2:? "2.- Bubble (variant)"
locate 3,2:? "3.- Select"
Locate 4,2:? "4.- Insert"
locate 5,2:? "5.- Shell"
locate 6,2:? "6.- Merge (recursive)"
locate 7,2:? "7.- Merge (iterative)"
locate 8,2:? "8.- Quick (recursive)"
locate 9,2:? "9.- Quick (iterative)"
locate 12,2:? Space(30):locate 12,2:? "r.- Random " & Iif(r, "off", "on")
locate 13,2:? Space(30):locate 13,2:? "p.- Draw " & Iif(plot, "line", "plot")
locate 14,2:? Space(30):locate 14,2:? "(spc).- Sync " & smodes(sync)
Screencopy
'Cls
dim as String k = Inkey
Select Case k
case "1":rrandom(a(),r):bubble_sort(a(),Lbound(a),Ubound(a))
case "2":rrandom(a(),r):bubble2_sort(a(),Lbound(a),Ubound(a))
case "3":rrandom(a(),r):select_sort(a(),Lbound(a),Ubound(a))
case "4":rrandom(a(),r):insert_sort(a(),Lbound(a),Ubound(a))
case "5":rrandom(a(),r):shell_sort(a(),Lbound(a),Ubound(a))
case "6":rrandom(a(),r):merge_rec_sort(a(),Lbound(a),Ubound(a))
case "7":rrandom(a(),r):merge_ite_sort(a(),Lbound(a),Ubound(a))
case "8":rrandom(a(),r):quick_rec_sort(a(),Lbound(a),Ubound(a))
case "9":rrandom(a(),r):quick_ite_sort(a(),Lbound(a),Ubound(a))
case "p":plot = 1 - plot
case " ":sync inc: sync Mod= 3
case "r":r = 1 - r
case "q": Exit Do
End Select
Sleep 1,1
Loop
'sleep