Jump to content

BubbleSort - A slower alternative to _ArraySort


rcmaehl
 Share

Recommended Posts

Got Bored, and decided to write up BubbleSort from my early Prelude to Programming class in College.


Features:
BubbleSorting! Fast on small arrays, extremely slow on larger ones!
2 Dimesional Array Sorting (KINDA)! Sort 2 dimesional arrays!
83 Lines of Code you'll never use because _ArraySort is hundreds faster!

#include-once
#include <AutoItConstants.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: BubbleSort
; Description ...: Sorts a 1 or 2 dimensional array using the BubbleSort Algorithm
; Syntax ........: BubbleSort($_aArray[, $_bAscending = True])
; Parameters ....: $_aArray             - [in/out] The 1 dimensional array to sort.
;                  $_bAscending         - [optional] Sort by increasing values. Default is True.
; Return values .: Success - Returns 1
;                  Failure - Returns 0 and sets @error:
;                  |1 = Invalid Array, sets @extented: (1, if not a valid array; 2, if not 1 or 2 dimesional)
;                  |2 = Invalid $_bAscending Flag
; Author ........: Robert C. Maehl (rcmaehl)
; Modified ......: 11/30/2015
; Remarks .......: To do: Complete Two Dimesional Array sorting ([first,first] = lowest, [last,last] = highest)
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func BubbleSort(ByRef $_aArray, $_bAscending = True)
    Local $_vReturn = Null
    Local $_bFinished = False
    If Not IsArray($_aArray) Then SetError(1,1,0)
    If UBound($_aArray, $UBOUND_DIMENSIONS) > 2 Then SetError(1,2,0)
    If Not IsBool($_bAscending) Then SetError(2,0,0)
    If $_vReturn = Null Then
        If UBound($_aArray, $UBOUND_DIMENSIONS) = 1 Then
            While Not $_bFinished
                $_bFinished = True
                $_iLoop = 0
                $_vTemp = 0
                If $_bAscending Then
                    For $_iLoop = 0 To UBound($_aArray) - 2
                        If $_aArray[$_iLoop] > $_aArray[$_iLoop + 1] Then
                            $_vTemp = $_aArray[$_iLoop]
                            $_aArray[$_iLoop] = $_aArray[$_iLoop + 1]
                            $_aArray[$_iLoop + 1] = $_vTemp
                            $_bFinished = False
                        EndIf
                    Next
                Else
                    For $_iLoop = 0 To UBound($_aArray) - 2
                        If $_aArray[$_iLoop] < $_aArray[$_iLoop + 1] Then
                            $_vTemp = $_aArray[$_iLoop]
                            $_aArray[$_iLoop] = $_aArray[$_iLoop + 1]
                            $_aArray[$_iLoop + 1] = $_vTemp
                            $_bFinished = False
                        EndIf
                    Next
                EndIf
            WEnd
        Else ; 2 Dimesional Sorting, Not Yet Complete. (Sort all rows & columns with [0,0] with smallest, and [last,last] with largest)
            $_iLoop1 = 0
            For $_iLoop1 = 0 To UBound($_aArray, $UBOUND_ROWS) - 1
                $_bFinished = False
                While Not $_bFinished
                    $_bFinished = True
                    $_iLoop2 = 0
                    $_vTemp = 0
                    If $_bAscending Then
                        For $_iLoop2 = 0 To UBound($_aArray, $UBOUND_COLUMNS) - 2
                            If $_aArray[$_iLoop1][$_iLoop2] > $_aArray[$_iLoop1][$_iLoop2 + 1] Then
                                $_vTemp = $_aArray[$_iLoop1][$_iLoop2]
                                $_aArray[$_iLoop1][$_iLoop2] = $_aArray[$_iLoop1][$_iLoop2 + 1]
                                $_aArray[$_iLoop1][$_iLoop2 + 1] = $_vTemp
                                $_bFinished = False
                            EndIf
                        Next
                    Else
                        For $_iLoop2 = 0 To UBound($_aArray, $UBOUND_COLUMNS) - 2
                            If $_aArray[$_iLoop1][$_iLoop2] < $_aArray[$_iLoop1][$_iLoop2 + 1] Then
                                $_vTemp = $_aArray[$_iLoop1][$_iLoop2]
                                $_aArray[$_iLoop1][$_iLoop2] = $_aArray[$_iLoop1][$_iLoop2 + 1]
                                $_aArray[$_iLoop1][$_iLoop2 + 1] = $_vTemp
                                $_bFinished = False
                            EndIf
                        Next
                    EndIf
                WEnd
            Next
        EndIf
    EndIf
EndFunc

 

Edited by rcmaehl
Fix #include

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects

WhyNotWin11
Cisco FinesseGithubIRC UDFWindowEx UDF

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...