How to resolve the algorithm Sorting algorithms/Permutation sort step by step in the Icon and Unicon programming language

Published on 12 May 2024 09:40 PM

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

Source code in the icon programming language

procedure do_permute(l, i, n)
    if i >= n then
        return l
    else
        suspend l[i to n] <-> l[i] & do_permute(l, i+1, n)
 end
 
 procedure permute(l)
    suspend do_permute(l, 1, *l)
 end
 
 procedure sorted(l)
    local i
    if (i := 2 to *l & l[i] >= l[i-1]) then return &fail else return 1
 end
 
 procedure main()
    local l
    l := [6,3,4,5,1]
    |( l := permute(l) & sorted(l)) \1 & every writes(" ",!l)
 end


  

You may also check:How to resolve the algorithm Find the intersection of two lines step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Merge and aggregate datasets step by step in the R programming language
You may also check:How to resolve the algorithm Wordiff step by step in the Nim programming language
You may also check:How to resolve the algorithm Four bit adder step by step in the C++ programming language
You may also check:How to resolve the algorithm Sort an integer array step by step in the Nim programming language