How to resolve the algorithm Sorting algorithms/Permutation sort step by step in the EMal programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sorting algorithms/Permutation sort step by step in the EMal 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 EMal programming language

Source code in the emal programming language

type PermutationSort 
fun isSorted = logic by List a
  for int i = 1; i < a.length; ++i
    if a[i - 1] > a[i] do return false end
  end
  return true
end
fun permute = void by List a, int n, List lists
  if n == 1
    List b = int[]
    for int i = 0; i < a.length; ++i
      b.append(a[i])
    end
    lists.append(b)
    return
  end
  int i = 0
  while i < n
    a.swap(i, n - 1)
    permute(a, n - 1, lists)
    a.swap(i, n - 1)
    i = i + 1
  end
end
fun sort = List by List a
  List lists = List[]
  permute(a, a.length, lists)
  for each List list in lists
    if isSorted(list) do return list end
  end
  return a  
end
type Main
List a = int[3,2,1,8,9,4,6]
writeLine("Unsorted: " + a)
a = PermutationSort.sort(a)
writeLine("  Sorted: " + a)

  

You may also check:How to resolve the algorithm Empty directory step by step in the Delphi programming language
You may also check:How to resolve the algorithm Morse code step by step in the Red programming language
You may also check:How to resolve the algorithm Topswops step by step in the D programming language
You may also check:How to resolve the algorithm Sort an integer array step by step in the PL/I programming language
You may also check:How to resolve the algorithm Cumulative standard deviation step by step in the Racket programming language