{"id":16085580,"url":"https://github.com/chen0040/lua-algorithms","last_synced_at":"2025-08-15T22:31:03.690Z","repository":{"id":85884670,"uuid":"95279022","full_name":"chen0040/lua-algorithms","owner":"chen0040","description":"Lua algorithms library that covers commonly used data structures and algorithms","archived":false,"fork":false,"pushed_at":"2017-11-24T06:33:48.000Z","size":46,"stargazers_count":70,"open_issues_count":0,"forks_count":13,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-03T23:51:09.514Z","etag":null,"topics":["algorithms","balanced-search-trees","binary-search-tree","data-structures","lua","red-black-tree","sorting","tries"],"latest_commit_sha":null,"homepage":null,"language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chen0040.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-24T06:03:19.000Z","updated_at":"2025-01-24T11:13:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"369d6f5f-43f0-4ac8-b405-cb250e1163e0","html_url":"https://github.com/chen0040/lua-algorithms","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/chen0040/lua-algorithms","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chen0040%2Flua-algorithms","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chen0040%2Flua-algorithms/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chen0040%2Flua-algorithms/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chen0040%2Flua-algorithms/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chen0040","download_url":"https://codeload.github.com/chen0040/lua-algorithms/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chen0040%2Flua-algorithms/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270637924,"owners_count":24620430,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-15T02:00:12.559Z","response_time":110,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithms","balanced-search-trees","binary-search-tree","data-structures","lua","red-black-tree","sorting","tries"],"created_at":"2024-10-09T13:02:02.250Z","updated_at":"2025-08-15T22:31:03.387Z","avatar_url":"https://github.com/chen0040.png","language":"Lua","readme":"# lua-algorithms\n\nThis library was developed with the purpose of making general algorithms and data structures available in Java language also available in Lua.\n\n# Features\n\nThe library covers commonly used data structures (ArrayList, Stack, Queue, Priority Queue, Balanced Search Tree, HashMap, Set, Tries) and algorithms (various sorting and search algorithms, shuffling, union find, etc)\n\nFor developers on Windows platform, a Vagrantfile is provided in the source code to allow them to run luarocks under window environment.\n\n\n# Install\n\n```bash\nluarocks install lualgorithms\n```\n\n# Usage\n\n## Common Data Structures\n\n### Stack\n\n```lua\nlocal stack = require 'lualgorithms.data.stack'\n\nlocal s = stack.create()\ns:push(1)\ns:push(2)\ns:push(3)\nprint(s:size())\nprint(s:isEmpty())\nprint(s:pop())\nfor index,val in pairs(s:enumerate()) do\n    print(index, val)\nend\n```\n\n### ArrayList\n\nThe list behaves the same as the Java ArrayList API, and is zero-based indexing.\n\n```lua\nlocal list = require 'lualgorithms.data.list'\n\nlocal s = list.create()\ns:add(1) -- s becomes [1]\ns:add(2) -- s becomes [1, 2]\ns:add(3) -- s becomes [1, 2, 3]\ns:set(2, 4) -- s becomes [1, 2, 4]\n\nfor index,val in pairs(s:enumerate()) do\n    print(index, val)\nend\nfor i = 0,s:size()-1 do\n    print(s:get(i))\nend\nprint(s:size())\nprint(s:isEmpty())\ns:removeAt(0) -- s becomes [2, 4]\ns:remove(2) -- s becomes [4]\ns:removeAt(0) -- s is now empty\n```\n\n### Queue\n\n```lua\n local queue = require('lualgorithms.data.queue')\nlocal s = queue.create()\ns:enqueue(10)\ns:enqueue(20)\ns:enqueue(30)\nprint(s:size()) -- return 3\nprint(s:isEmpty()) -- return false\nfor key,value in pairs(s:enumerate()) do\n    print(key, value)\nend\nprint(s:dequeue()) -- return 10\nprint(s:dequeue()) -- return 20\nprint(s:dequeue()) -- return 30\n\n```\n\n### MinPQ\n\n```lua\nlocal minpq = require('lualgorithms.data.minpq')\nlocal comparer = function(a1, a2) return a1 - a2 end -- method that return negative value if if a1 \u003c a2; 0 if a1 == a2; positive otherwise\nlocal s = minpq.create(comparer)\ns:enqueue(10)\ns:enqueue(100)\ns:enqueue(20)\ns:enqueue(50)\nprint(s:size()) -- return 4\nprint(s:isEmpty()) -- return false\n\nprint(s:delMin()) -- return 10\nprint(s:delMin()) -- return 20\nprint(s:delMin()) -- return 50\nprint(s:delMin()) -- return 100\n\nprint(s:isEmpty()) -- return true\n```\n\n\n### MaxPQ\n\n```lua\nlocal maxpq = require('lualgorithms.data.maxpq')\nlocal comparer = function(a1, a2) return a1 - a2 end -- method that return negative value if if a1 \u003c a2; 0 if a1 == a2; positive otherwise\nlocal s = maxpq.create(comparer)\ns:enqueue(10)\ns:enqueue(100)\ns:enqueue(20)\ns:enqueue(50)\nprint(s:size()) -- return 4\nprint(s:isEmpty()) -- return false\n\nprint(s:delMax()) -- return 100\nprint(s:delMax()) -- return 50\nprint(s:delMax()) -- return 20\nprint(s:delMax()) -- return 10\n\nprint(s:isEmpty()) -- return true\n```\n\n### HashSet\n\n```lua\nlocal hashset = require('lualgorithms.data.hashset')\nlocal hash_func = function(x) return x % 1000 end\nlocal s = hashset.create(hash_func)\ns:add(100, 2)\ns:add(200, 4)\ns:add(450, 2)\n\nprint(s:contains(99)) -- return false\nprint(s:contains(100)) -- return true\nprint(s:size()) -- return 3\nprint(s:isEmpty()) -- return false\ns:remove(100)\nprint(s:contains(100)) -- return false)\n```\n\n### HashMap\n\n```lua\nlocal hashmap = require('lualgorithms.data.hashmap')\nlocal hash_func = function(x) return x % 1000 end\nlocal s = hashmap.create(hash_func)\ns:put(100, 2)\ns:put(200, 4)\ns:put(450, 2)\nprint(s:get(100)) -- return  2\nprint(s:get(200)) -- return  4\nprint(s:get(450)) -- return  2\nprint(s:get(99)) -- return  nil\nprint(s:containsKey(99)) -- return  false\nprint(s:containsKey(100)) -- return  true\nprint(s:size()) -- return  3\nprint(s:isEmpty()) -- return  false\nprint(s:remove(100)) -- return  2\nprint(s:containsKey(100)) -- return  false\nprint(s:size()) -- return  2\ns:remove(200)\ns:remove(450)\nprint(s:isEmpty()) -- return  true\n```\n\n### SortedMap (Left-Leaning Red Black Tree)\n\n```lua\nlocal map = require('lualgorithms.data.redblacktree')\nlocal comparator = function(a1, a2) return a1 - a2 end\nlocal s = map.create(comparator)\ns:put(100, 2)\ns:put(200, 4)\ns:put(450, 2)\nprint(s:minKey()) -- return 100\nprint(s:maxKey()) -- return 450\nprint(s:get(100)) -- return  2\nprint(s:get(200)) -- return  4\nprint(s:get(450)) -- return  2\nprint(s:get(99)) -- return  nil\nprint(s:containsKey(99)) -- return  false\nprint(s:containsKey(100)) -- return  true\nprint(s:size()) -- return  3\nprint(s:isEmpty()) -- return  false\nprint(s:remove(100)) -- return  2\nprint(s:containsKey(100)) -- return  false\nprint(s:size()) -- return  2\ns:remove(200)\ns:remove(450)\nprint(s:isEmpty()) -- return  true\n```\n\n### Tries (R-way Tries)\n\n```lua\nlocal rwaytries = require('lualgorithms.tries.rwaytries')\nlocal s = rwaytries.create()\n\ns:put(\"Hello\", \"World\")\ns:put(\"Hi\", \"Morning\")\ns:put(\"How\", \"are you?\")\n\nprint(s:isEmpty()) -- return false\nprint(s:size()) -- return 3\n\nprint(s:get(\"Hello\")) -- return \"World\"\nprint(s:get(\"Hi\")) -- return \"Morning\"\nprint(s:get(\"How\")) -- return \"are you?\"\nprint(s:containsKey(\"Hello\")) -- return true\nprint(s:containsKey(\"hello\")) -- return false\n\ns:remove(\"Hello\")\n\nprint(s:containsKey(\"Hello\")) -- return false\nprint(s:size()) -- return 2\n\nlocal keys = s:keys()\nfor i=0, keys:size()-1 do\n    print(keys:get(i))\nend\n\ns:put('there', 'is')\ns:put('the', 'ninja')\ns:put('those', 'turtles')\ns:put('these', 'ducks')\ns:put('turles', 'ducks')\nkeys = s:keysWithPrefix('th')\nfor i=0, keys:size()-1 do\n    print(keys:get(i))\nend\n```\n\n## Sorting\n\nAs in Java, the sorting is performed on ArrayList by default (which is lualgorithms.data.list).\n\nNote that the default is to sort ascendingly, which can be reversed via the comparator function pass in as the second parameter.\n\n### Sorting (Selection Sort)\n\n```lua\nlocal list = require(\"lualgorithms.data.list\")\nlocal a = list.create()\na:add(100)\na:add(200)\na:add(300)\na:add(600)\na:add(200)\na:add(400)\na:add(340)\na:add(120)\na:add(10)\n\nlocal selection = require(\"lualgorithms.sorting.selection\")\nselection.sort(a, function(a1, a2) return a1 - a2 end)\n\nfor i=0,(a:size()-1) do\n    print(a:get(i))\nend\n```\n\n### Sorting (Insertion Sort)\n\n```lua\nlocal list = require(\"lualgorithms.data.list\")\nlocal a = list.create()\na:add(100)\na:add(200)\na:add(300)\na:add(600)\na:add(200)\na:add(400)\na:add(340)\na:add(120)\na:add(10)\n\nlocal insertion = require(\"lualgorithms.sorting.insertion\")\ninsertion.sort(a, function(a1, a2) return a1 - a2 end)\n\nfor i=0,(a:size()-1) do\n    print(a:get(i))\nend\n```\n\n### Sorting (Shell Sort)\n\n```lua\nlocal list = require(\"lualgorithms.data.list\")\nlocal a = list.create()\na:add(100)\na:add(200)\na:add(300)\na:add(600)\na:add(200)\na:add(400)\na:add(340)\na:add(120)\na:add(10)\n\nlocal shellsort = require(\"lualgorithms.sorting.shellsort\")\nshellsort.sort(a, function(a1, a2) return a1 - a2 end)\n\nfor i=0,(a:size()-1) do\n    print(a:get(i))\nend\n```\n\n### Sorting (Merge Sort)\n\n```lua\nlocal list = require(\"lualgorithms.data.list\")\nlocal a = list.create()\na:add(100)\na:add(200)\na:add(300)\na:add(600)\na:add(200)\na:add(400)\na:add(340)\na:add(120)\na:add(10)\n\nlocal mergesort = require(\"lualgorithms.sorting.mergesort\")\nmergesort.sort(a, function(a1, a2) return a1 - a2 end)\n\nfor i=0,(a:size()-1) do\n    print(a:get(i))\nend\n```\n\n### Sorting (Quick Sort)\n\n```lua\nlocal list = require(\"lualgorithms.data.list\")\nlocal a = list.create()\na:add(100)\na:add(200)\na:add(300)\na:add(600)\na:add(200)\na:add(400)\na:add(340)\na:add(120)\na:add(10)\n\nlocal quicksort = require(\"lualgorithms.sorting.quicksort\")\nquicksort.sort(a, function(a1, a2) return a1 - a2 end)\n\nfor i=0,(a:size()-1) do\n    print(a:get(i))\nend\n```\n\n### Sorting (3-ways Quick Sort)\n\n```lua\nlocal list = require(\"lualgorithms.data.list\")\nlocal a = list.create()\na:add(100)\na:add(200)\na:add(300)\na:add(600)\na:add(200)\na:add(400)\na:add(340)\na:add(120)\na:add(10)\n\nlocal quicksort3ways = require(\"lualgorithms.sorting.quicksort3ways\")\nquicksort3ways.sort(a, function(a1, a2) return a1 - a2 end)\n\nfor i=0,(a:size()-1) do\n    print(a:get(i))\nend\n```\n\n### Sorting (Heap Sort)\n\n```lua\nlocal list = require(\"lualgorithms.data.list\")\nlocal a = list.create()\na:add(100)\na:add(200)\na:add(300)\na:add(600)\na:add(200)\na:add(400)\na:add(340)\na:add(120)\na:add(10)\n\nlocal heapsort = require(\"lualgorithms.sorting.heapsort\")\nheapsort.sort(a, function(a1, a2) return a1 - a2 end)\n\nfor i=0,(a:size()-1) do\n    print(a:get(i))\nend\n```\n\n## Shuffling\n\n```lua\nlocal list = require('lualgorithms.data.list')\nlocal s = list.create()\nfor i=1,10 do\n    s:add(i)\nend\nlocal shuffling = require('lualgorithms.shuffling')\nshuffling.shuffle(s)\nfor i=0,(s:size()-1) do\n    print(s:get(i))\nend\n```\n\n## Binary Search on Sorted ArrayList\n\n```lua\nlocal comparator = function(a1, a2) return a1 - a2 end\nlocal s = create_a_list_that_sorts_ascendingly(comparator)\n\nif s.isSortedAscedningly() then\n    local binarysearch = require('lualgorithms.binarysearch')\n    print(binarysearch.indexOf(s, 10, comparator)) -- return the index of value 10 in the array list s\nelse\n    print('error! list must be sorted before performing binary search')    \nend\n```\n\n## Union Find\n\n```lua\nlocal unionfind = require('lualgorithms.unionfind').create()\nunionfind:union(1, 2)\nunionfind:union(4, 6)\nunionfind:union(7, 4)\n\nprint(unionfind:connected(6, 7)) -- return true\nprint(unionfind:connected(4, 7)) -- return true\nprint(unionfind:connected(6, 4)) -- return true\nprint(unionfind:connected(6, 1)) -- return false\nprint(unionfind:connected(7, 2)) -- return false\n```\n","funding_links":[],"categories":["Libraries"],"sub_categories":["Programming Language"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchen0040%2Flua-algorithms","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchen0040%2Flua-algorithms","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchen0040%2Flua-algorithms/lists"}