Firetron's MergeSort

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

Description

Sorts a table using the merge 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:')

  table = CC_MergeSort(table)

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

  Save('done', true)

end
HaasScript
--  ============================================================================
--    Firetron's MergeSort
--
--    Sorts a table using the merge 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        = 'MergeSort'
description = 'Sorts a table using the merge 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 GetLower = function (a, b)

  local i = 1
  local j = 1

  return function()

    if not b[j] or a[i] and a[i] < b[j] then

      i = i + 1

      return a[i - 1]

    else

      j = j + 1

      return b[j - 1]

    end

  end

end

--  ----------------

local Merge = function (a, b)

  local result = {}
  local lower  = GetLower(a, b)

  for value in lower do

    local i = #result + 1

    result[i] = value

  end

  return result

end

--  ----------------

local MergeSort

MergeSort = function (list)

  if #list <= 1 then return list end

  local half1 = {}
  local half2 = {}
  local split = Floor(#list / 2)

  for i = 1, #list do

    if i > split then

      local j = Parse(i - split, NumberType)

      half2[j] = list[i]

    else

      half1[i] = list[i]

    end

  end

  local sorted1 = MergeSort(half1)
  local sorted2 = MergeSort(half2)

  return Merge(sorted1, sorted2)

end

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

output = MergeSort(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!