Ok I managed to make a Quick Sort, it's not as hard as I first thought. It was confusing at first cause the function would call itself, although I'm not completly new to that method I've always thought it was funny.
Quick Sort is blazing fast! I did a comparison between the different sorting algorithm I got so far, also found a speed difference amongst different machines, although not by much.
The Comb Sort is suprisingly stable.
These are the results I got from my laptop.
Random filenames 500
Sort - Time taken to finnish sorting
Bubble Sort - 29,523
Selection Sort - 461
Comb Sort - 371
Insertion Sort - 550
Quick Sort - 21
Random filenames 1000
Sort - Time taken to finnish sorting
Bubble Sort ---- too long
Selection Sort - 2133
Comb Sort ------ 430
Insertion Sort - 2744
Quick Sort ----- 30
Random filenames 2000
Sort - Time taken to finnish sorting
Bubble Sort ---- too long
Selection Sort - 7401
Comb Sort ------ 471
Insertion Sort - 8743
Quick Sort ----- 80
Random filenames 3000
Sort - Time taken to finnish sorting
Bubble Sort ---- too long
Selection Sort - too long
Comb Sort ------ 681
Insertion Sort - too long
Quick Sort ----- 130
Here's the codes.
sync on
sync rate 0
create bitmap 1,640,480
REM Set File amount here
FileCount = 3000
gosub SortSetup
REM *********************************************************************
REM Main Loop
REM *********************************************************************
do
gosub ShowName
gosub KeysUpdate
copy bitmap 1,0
sync
loop
REM *********************************************************************
REM SortSetup
REM *********************************************************************
SortSetup:
cls rgb(100,100,100)
dim GenName$(FileCount)
dim FileName$(FileCount)
gosub GenName
return
REM *********************************************************************
REM GenName
REM *********************************************************************
GenName:
REM Generating Random String
for FileLP = 0 to FileCount
CharLen = 3
for CharLP = 1 to CharLen
CharGroup = rnd(100)
REM Random 0 - 9
if CharGroup < 100 then Char$ = chr$(48+rnd(9))
REM Random A - Z
if CharGroup < 90 then Char$ = chr$(65+rnd(25))
GenName$(FileLP) = GenName$(FileLP) + Char$
FileName$(FileLP) = GenName$(FileLP)
next CharLP
next FileLP
return
REM *********************************************************************
REM KeysUpdate
REM *********************************************************************
KeysUpdate:
ink rgb(255,255,255),0
Tsize = 10
Ts = Tsize + Tsize/3
set text size 10
text 0,0*Ts,"1 = BubbleSort"
text 0,1*Ts,"2 = SelectSort"
text 0,2*Ts,"3 = CombSort"
text 0,3*Ts,"4 = InsertionSort"
text 0,4*Ts,"5 = QuickSort"
text 0,6*Ts,"R = Reset Print"
text 0,7*Ts,"Sorting "+str$(FileCount)+" FileNames"
i$ = inkey$()
if i$ > ""
cls
gosub ResetNames
t1 = timer()
if i$ = "1" then gosub BubbleSort
if i$ = "2" then gosub SelectSort
if i$ = "3" then gosub CombSort
if i$ = "4" then gosub InsertionSort
if i$ = "5" then QuickSort(0,FileCount)
if upper$(i$) = "R"
gosub GenName
gosub ResetNames
endif
t2 = timer()
text 0,8*Ts,"Time Test: " + str$(t2-t1)
endif
return
REM *********************************************************************
REM ResetNames
REM *********************************************************************
ResetNames:
for NameLP = 0 to FileCount
FileName$(NameLP) = GenName$(NameLP)
next NameLP
return
REM *********************************************************************
REM ShowNames
REM *********************************************************************
ShowName:
ink rgb(255,255,255),0
maxX = 11
maxY = FileCount/maxX
z = -1
for lpy = 0 to maxY
for lpx = 0 to maxX
z = z + 1
x = 180+lpx*35
y = lpy*14
text x,y,FileName$(z)
if z + 1 > FileCount or y > 450
lpx = maxX
lpy = maxY
endif
next lpx
next lpy
ink rgb(50,50,50),0
box 620,639,0,479
return
REM *********************************************************************
REM Insertion Sort
REM *********************************************************************
InsertionSort:
for SortLP = 1 to FileCount
Insert$ = FileName$(SortLP)
i = SortLP - 1
while i => 0
if upper$(FileName$(i)) > upper$(Insert$)
FileName$(i + 1) = FileName$(i)
FileName$(i) = Insert$
endif
i = i - 1
endwhile
next SortLP
return
REM *********************************************************************
REM Comb Sort
REM *********************************************************************
CombSort:
gap = FileCount
repeat
REM update the gap value for a next comb
if gap > 1
gap = gap / 1.3
if gap = 10 or gap = 9
gap = 11
endif
endif
i = 0
swaps = 0
repeat
if upper$(FileName$(i)) > upper$(FileName$(i+gap))
REM Swap
buffer$ = FileName$(i)
FileName$(i) = FileName$(i+gap)
FileName$(i+gap) = buffer$
swaps = 1
endif
i = i + 1
until i + gap > FileCount
sync
until gap =< 1 and swaps = 0
return
REM *********************************************************************
REM SelectSort
REM *********************************************************************
SelectSort:
for SortLP = 0 to FileCount-2
min = SortLP
for SelectLP = (SortLP + 1) to FileCount
if upper$(FileName$(SelectLP)) < upper$(FileName$(min))
min = SelectLP
endif
next SelectLP
REM Swap
buffer$ = FileName$(min)
FileName$(min) = FileName$(SortLP)
FileName$(SortLP) = buffer$
next SortLP
return
REM *********************************************************************
REM BubbleSort
REM *********************************************************************
BubbleSort:
for BubbleLP = 1 to FileCount
if FileName$(BubbleLP) < FileName$(BubbleLP-1)
REM Swap
buffer$ = FileName$(BubbleLP)
FileName$(BubbleLP) = FileName$(BubbleLP-1)
FileName$(BubbleLP-1) = buffer$
BubbleLP = 0
endif
next BubbleLP
return
REM *********************************************************************
REM Quick Sort
REM *********************************************************************
Function QuickSort(L,R)
CheckL = L
CheckR = R
Check$ = FileName$((L+R)/2)
repeat
REM Left Side Sort
while FileName$(CheckL) < Check$
CheckL = CheckL + 1
endwhile
REM Right Side Sort
while Check$ < FileName$(CheckR)
CheckR = CheckR - 1
endwhile
if CheckL <= CheckR
REM Swap
buffer$ = FileName$(CheckL)
FileName$(CheckL) = FileName$(CheckR)
FileName$(CheckR) = buffer$
CheckL = CheckL + 1
CheckR = CheckR - 1
endif
until CheckL >= CheckR
REM Calling this same Function multiple times
if L < CheckR then QuickSort(L,CheckR)
if CheckL < R then QuickSort(CheckL,R)
EndFunction