Microsoft Small Basic

Program Listing: QBF315
' =================================
'
' Small Basic Bubble Sort Demo
'
' Version 1.00
'
' Copyright 2017 by Erich Kohl
'
' =================================

Init()
Scramble()

While "true"
If GraphicsWindow.LastKey = "Escape" Then
Program.End()
EndIf
EndWhile

Sub Init

sorted = ""
native = ""
startTime = 0
comparisons = 0
swaps = 0

GraphicsWindow.Title = "Small Basic Bubble Sort Demo v1.00"
GraphicsWindow.Width = 900
GraphicsWindow.Height = 700
GraphicsWindow.BackgroundColor = "cyan"
GraphicsWindow.CanResize = "false"

GraphicsWindow.DrawRectangle(10, 10, 890, 500)

GraphicsWindow.BrushColor = "black"

btnStartNative = Controls.AddButton("Start (Native Speed)", 10, 550)
btnStartSlow = Controls.AddButton("Start (Slow Speed)", 10, 600)
btnScramble = Controls.AddButton("Scramble", 10, 650)

Controls.ButtonClicked = OnButtonClicked

GraphicsWindow.DrawText(400, 575, "Time:")
GraphicsWindow.DrawText(400, 600, "Comparisons:")
GraphicsWindow.DrawText(400, 625, "Swaps:")

EndSub

Sub OnButtonClicked

LastButton = Controls.LastClickedButton

If LastButton = btnStartNative Then
native = "true"
Sort()
ElseIf LastButton = btnStartSlow Then
native = "false"
Sort()
Else
Scramble()
EndIf

EndSub

Sub Scramble

a = 0

GraphicsWindow.BrushColor = "white"
GraphicsWindow.FillRectangle(10, 10, 890, 500)

For a = 1 To 89
elements_height[a] = Math.GetRandomNumber(499)
elements_color[a] = GraphicsWindow.GetRandomColor()
GraphicsWindow.BrushColor = elements_color[a]
GraphicsWindow.FillRectangle(a * 10, 510 - elements_height[a], 10, elements_height[a])
EndFor

startTime = 0
comparisons = 0
swaps = 0

GraphicsWindow.BrushColor = "cyan"
GraphicsWindow.FillRectangle(500, 575, 200, 75)

GraphicsWindow.BrushColor = "red"

GraphicsWindow.DrawText(500, 575, "0 second(s)")
GraphicsWindow.DrawText(500, 600, "0")
GraphicsWindow.DrawText(500, 625, "0")

sorted = "false"

Controls.ShowControl(btnStartNative)
Controls.ShowControl(btnStartSlow)

EndSub

Sub Sort

temp_height = 0
temp_color = 0
a = 0

Controls.HideControl(btnStartNative)
Controls.HideControl(btnStartSlow)
Controls.HideControl(btnScramble)

startTime = Clock.ElapsedMilliseconds
comparisons = 0
swaps = 0

While sorted = "false"
sorted = "true"
For a = 1 To 88
comparisons = comparisons + 1
If elements_height[a] > elements_height[a + 1] Then
sorted = "false"
GraphicsWindow.BrushColor = "white"
GraphicsWindow.FillRectangle(a * 10, 510 - elements_height[a], 10, elements_height[a])
GraphicsWindow.FillRectangle((a + 1) * 10, 510 - elements_height[a + 1], 10, elements_height[a + 1])
temp_height = elements_height[a]
temp_color = elements_color[a]
elements_height[a] = elements_height[a + 1]
elements_color[a] = elements_color[a + 1]
elements_height[a + 1] = temp_height
elements_color[a + 1] = temp_color
GraphicsWindow.BrushColor = elements_color[a]
GraphicsWindow.FillRectangle(a * 10, 510 - elements_height[a], 10, elements_height[a])
GraphicsWindow.BrushColor = elements_color[a + 1]
GraphicsWindow.FillRectangle((a + 1) * 10, 510 - elements_height[a + 1], 10, elements_height[a + 1])
swaps = swaps + 1
EndIf
If native = "false" Then
Program.Delay(3)
EndIf
EndFor
EndWhile

Controls.ShowControl(btnScramble)

GraphicsWindow.BrushColor = "cyan"
GraphicsWindow.FillRectangle(500, 575, 200, 75)

GraphicsWindow.BrushColor = "red"
GraphicsWindow.DrawText(500, 575, ((Clock.ElapsedMilliseconds - startTime) / 1000) + " second(s)")
GraphicsWindow.DrawText(500, 600, comparisons)
GraphicsWindow.DrawText(500, 625, swaps)

EndSub