How to resolve the algorithm Sort disjoint sublist step by step in the Icon and Unicon programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sort disjoint sublist step by step in the Icon and Unicon programming language
Table of Contents
Problem Statement
Given a list of values and a set of integer indices into that value list, the task is to sort the values at the given indices, while preserving the values at indices outside the set of those to be sorted. Make your example work with the following list of values and set of indices: Where the correct result would be: In case of one-based indexing, rather than the zero-based indexing above, you would use the indices {7, 2, 8} instead. The indices are described as a set rather than a list but any collection-type of those indices without duplication may be used as long as the example is insensitive to the order of indices given.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sort disjoint sublist step by step in the Icon and Unicon programming language
Source code in the icon programming language
link sort # get the 'isort' procedure for sorting a list
procedure sortDisjoint (items, indices)
indices := isort (indices) # sort indices into a list
result := copy (items)
values := []
every put (values, result[!indices])
values := isort (values)
every result[!indices] := pop (values)
return result
end
procedure main ()
# set up and do the sort
items := [7, 6, 5, 4, 3, 2, 1, 0]
indices := set(7, 2, 8) # note, Icon lists 1-based
result := sortDisjoint (items, indices)
# display result
every writes (!items || " ")
write ()
every writes (!indices || " ")
write ()
every writes (!result || " ")
write ()
end
every put (values, result[!indices])
You may also check:How to resolve the algorithm Munching squares step by step in the Ring programming language
You may also check:How to resolve the algorithm Read a specific line from a file step by step in the Arturo programming language
You may also check:How to resolve the algorithm Quickselect algorithm step by step in the Fortran programming language
You may also check:How to resolve the algorithm User input/Graphical step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Multiplicative order step by step in the Seed7 programming language