How to resolve the algorithm Sorting algorithms/Stooge sort step by step in the Lua programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sorting algorithms/Stooge sort step by step in the Lua programming language

Table of Contents

Problem Statement

Show the   Stooge Sort   for an array of integers.

The Stooge Sort algorithm is as follows:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sorting algorithms/Stooge sort step by step in the Lua programming language

Source code in the lua programming language

local Y = function (f)
  return (function(x) return x(x) end)(function(x) return f(function(...) return x(x)(...) end) end)
end

function stoogesort(L, pred)

  pred = pred or function(a,b) return a < b end

  Y(function(recurse)
    return function(i,j)
      if pred(L[j], L[i]) then
        L[j],L[i] = L[i],L[j]
      end
      if j - i > 1 then
        local t = math.floor((j - i + 1)/3)
        recurse(i,j-t)
        recurse(i+t,j)
        recurse(i,j-t)
      end
    end
  end)(1,#L)
  
  return L
end

print(unpack(stoogesort{9,7,8,5,6,3,4,2,1,0}))


  

You may also check:How to resolve the algorithm Send email step by step in the C# programming language
You may also check:How to resolve the algorithm Discordian date step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the 0815 programming language
You may also check:How to resolve the algorithm Digital root step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Fork step by step in the C programming language