How to resolve the algorithm Sort disjoint sublist step by step in the ooRexx programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sort disjoint sublist step by step in the ooRexx 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 ooRexx programming language
Source code in the oorexx programming language
data = .array~of(7, 6, 5, 4, 3, 2, 1, 0)
-- this could be a list, array, or queue as well because of polymorphism
-- also, ooRexx arrays are 1-based, so using the alternate index set for the
-- problem.
indexes = .set~of(7, 2, 8)
call disjointSorter data, indexes
say "Sorted data is: ["data~toString("l", ", ")"]"
::routine disjointSorter
use arg data, indexes
temp = .array~new(indexes~items)
-- we want to process these in a predictable order, so make an array
tempIndexes = indexes~makearray
-- we can't just assign things back in the same order. The expected
-- result requires the items be inserted back in first-to-last index
-- order, so we need to sort the index values too
tempIndexes~sortWith(.numberComparator~new)
do index over tempIndexes
temp~append(data[index])
end
-- sort as numbers
temp~sortwith(.numberComparator~new)
do i = 1 to tempIndexes~items
data[tempIndexes[i]] = temp[i]
end
-- a custom comparator that sorts strings as numeric values rather than
-- strings
::class numberComparator subclass comparator
::method compare
use strict arg left, right
-- perform the comparison on the names. By subtracting
-- the two and returning the sign, we give the expected
-- results for the compares
return (left - right)~sign
You may also check:How to resolve the algorithm Shell one-liner step by step in the C programming language
You may also check:How to resolve the algorithm Spiral matrix step by step in the Pascal programming language
You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the Phix programming language
You may also check:How to resolve the algorithm Shoelace formula for polygonal area step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm 15 puzzle solver step by step in the Python programming language