Firetron's ShellSort

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

Description

Sorts a table using the shell 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_ShellSort(table)

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

  Save('done', true)

end
HaasScript
--  ============================================================================
--    Firetron's ShellSort
--
--    Sorts a table using the shell 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        = 'ShellSort'
description = 'Sorts a table using the shell 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 ShellSort = function (list)

  local gap = Parse(Ceil(#list / 2), NumberType)

  while gap > 0 do

    for i = gap, #list do

      local temp = list[i]

      local j = i

      while j > gap and list[j - gap] > temp do

        list[j] = list[j - gap]

        j = j - gap

      end

      list[j] = temp

    end

    gap = Parse(Floor(0.5 + gap / 2.2), NumberType)

  end

  return list

end

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

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