How to resolve the algorithm Sorting algorithms/Permutation sort step by step in the NetRexx programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sorting algorithms/Permutation sort step by step in the NetRexx programming language
Table of Contents
Problem Statement
Implement a permutation sort, which proceeds by generating the possible permutations of the input array/list until discovering the sorted one. Pseudocode:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sorting algorithms/Permutation sort step by step in the NetRexx programming language
Source code in the netrexx programming language
/* NetRexx */
options replace format comments java crossref symbols nobinary
import java.util.List
import java.util.ArrayList
numeric digits 20
class RSortingPermutationsort public
properties private static
iterations
maxIterations
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method permutationSort(vlist = List) public static returns List
perm = RPermutationIterator(vlist)
iterations = 0
maxIterations = RPermutationIterator.factorial(vlist.size())
loop while perm.hasNext()
iterations = iterations + 1
pl = List perm.next()
if isSorted(pl) then leave
else pl = null
end
return pl
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method isSorted(ss = List) private static returns boolean
status = isTrue
loop ix = 1 while ix < ss.size()
vleft = Rexx ss.get(ix - 1)
vright = Rexx ss.get(ix)
if vleft.datatype('N') & vright.datatype('N')
then vtest = vleft > vright -- For numeric types we must use regular comparison.
else vtest = vleft >> vright -- For non-numeric/mixed types we must do strict comparison.
if vtest then do
status = isFalse
leave ix
end
end ix
return status
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) private static
placesList = -
"UK London, US New York, US Boston, US Washington" -
"UK Washington, US Birmingham, UK Birmingham, UK Boston"
anotherList = 'Alpha, Beta, Gamma, Beta'
reversed = '7, 6, 5, 4, 3, 2, 1'
unsorted = '734, 3, 1, 24, 324, -1024, -666, -1, 0, 324, 99999999'
lists = [makeList(placesList), makeList(anotherList), makeList(reversed), makeList(unsorted)]
loop il = 0 while il < lists.length
vlist = lists[il]
say vlist
runtime = System.nanoTime()
rlist = permutationSort(vlist)
runtime = System.nanoTime() - runtime
if rlist \= null then say rlist
else say 'sort failed'
say 'This permutation sort of' vlist.size() 'elements took' iterations 'passes (of' maxIterations') to complete. \-'
say 'Elapsed time:' (runtime / 10 ** 9)'s.'
say
end il
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method makeList(in) public static returns List
lst = ArrayList()
loop while in > ''
parse in val ',' in
lst.add(val.strip())
end
return lst
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method main(args = String[]) public static
runSample(Rexx(args))
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method isTrue() public static returns boolean
return (1 == 1)
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method isFalse() public static returns boolean
return (1 == 0)
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the Cowgol programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Quickselect algorithm step by step in the Crystal programming language
You may also check:How to resolve the algorithm Echo server step by step in the BASIC programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Soda programming language