# Bubblesort

## Recommended Posts

Hi all! Sorry for my bad english.

A few minutes ago i started coding the famous bubblesort algorithm.

This is the code with a shitty GUI made just for input the values faster.

```#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#Region ### START Koda GUI section ### Form=
\$Form2 = GUICreate("Form2", 405, 61, 302, 218)
\$insert = GUICtrlCreateInput("Insert", 16, 16, 121, 21)
\$inserted = GUICtrlCreateButton("inserted", 160, 16, 75, 25,\$BS_DEFPUSHBUTTON)
\$ok = GUICtrlCreateButton("ok", 256, 16, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
\$First=true
While 1
\$nMsg = GUIGetMsg()
Switch \$nMsg
Case \$GUI_EVENT_CLOSE
Exit

Case \$inserted
if \$First Then
GUICtrlSetData(\$insert,"")
\$First=False
Else
GUICtrlSetData(\$insert,"")
EndIf

Case \$ok
Ordina()

EndSwitch
WEnd

Func Ordina()
\$max=UBound(\$Array)
While \$max>0
For \$i=0 To \$max-2
If \$Array[\$i]>\$Array[\$i+1] Then
_ArraySwap(\$Array[\$i],\$Array[\$i+1])
EndIf
Next
\$max=\$max-1
WEnd
_ArrayDisplay(\$Array)
Exit
EndFunc ```

It all worked great until i noticed this error:

i don't know how to explain it well in english, but the numbers are ordered in a strange way, this is a photo of the result:

The script consider only the first 3 digits in this case, because the shortest number is made of three digits so it looks like:

123

124

125

127

It's not considering the real value of the number.

Any ideas?

##### Share on other sites

Thanzex,

Welcome to the AutoIt forum.

As you can see the sort algorithm is treating the values as strings and sorting them on the ASCII values of the characters. Use Number to force the comparison to use their numeric value:

`If Number(\$Array[\$i]) > Number(\$Array[\$i + 1]) Then`

M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

##### Share on other sites

...or replace the line

`_ArrayAdd(\$Array,GUICtrlRead(\$insert))`

with

`_ArrayAdd(\$Array,Int(GUICtrlRead(\$insert)))`

A-Jay

Rule #1: Always do a backup         Rule #2: Always do a backup (backup of rule #1)

##### Share on other sites

...or replace the line

`_ArrayAdd(\$Array,GUICtrlRead(\$insert))`

with

`_ArrayAdd(\$Array,Int(GUICtrlRead(\$insert)))`

A-Jay

`_ArrayAdd(\$Array, Number(GUICtrlRead(\$insert)))`

WIll allow for negative numbers.

• 1

"Just be fred, all we gotta do, just be fred."  -Vocaliod

"That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha

@tabhooked

## Create an account

Register a new account

×

• Wiki

• Back

• Git