Firetron's BubbleSort

stable
By Firetron in Miscellaneous Published March 2021 👁 1,105 views 💬 0 comments

Description

Sorts a table using the bubble sorting algorithm. Custom Command Dependencies: None Test Code:
if not Load('done', false) then

  local table = {8, 3, 45, 23, 7, 2}

  Log(table)
  Log('Unsorted Table:')

  CC_BubbleSort(table)

  Log(table)
  Log('Sorted Table:')

  Save('done', true)

end
HaasScript
--  ============================================================================
--    Firetron's BubbleSort
--
--    Sorts a table using the bubble sorting algorithm.
--
--    Custom Command Dependencies:
--    None
--
--    Discord: @FiretronP75
--  ============================================================================

--  ========================================================
--    Variables
--  ========================================================

--  ------------------------------------
--    Definition
--  ------------------------------------

local defaultValue
local description
local inputSuggestions
local isRequired
local name
local output
local outputSuggestions
local type

--  ------------------------------------
--    Parameter
--  ------------------------------------

local pTable

--  ========================================================
--    Command Definition
--  ========================================================

name        = 'BubbleSort'
description = 'Sorts a table using the bubble sorting algorithm.'
DefineCommand(name, description)

--  ========================================================
--    Parameter Definition
--  ========================================================

type             = ListDynamicType
name             = 'table'
description      = 'Table to sort.'
isRequired       = true
defaultValue     = {3, 1, 2}
inputSuggestions = 'Prices'
pTable           = DefineParameter(type, name, description, isRequired, defaultValue, inputSuggestions)

--  ========================================================
--    Functions
--  ========================================================

local BubbleSort = function (list)

  local n = #list

  for i = 1, n do

    for j = 1, n - i do

      if (list[j] > list[j + 1]) then

        local temp  = list[j]
        list[j]     = list[j + 1]
        list[j + 1] = temp

      end

    end

  end

  return list

end

--  ========================================================
--    Execution
--  ========================================================

output = BubbleSort(pTable)

--  ========================================================
--    Output Definitions
--  ========================================================

type              = ListDynamicType
description       = 'The sorted table.'
outputSuggestions = ''
DefineOutput(type, output, description, outputSuggestions)

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!