How to resolve the algorithm Sort disjoint sublist step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sort disjoint sublist step by step in the Wren 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 Wren programming language

Source code in the wren programming language

import "/sort" for Sort

// sorts values in place, leaves indices unsorted
var sortDisjoint = Fn.new { |values, indices|
    var sublist = []
    for (ix in indices) sublist.add(values[ix])
    Sort.quick(sublist)
    var i = 0
    var indices2 = Sort.merge(indices)
    for (ix in indices2) {
        values[ix] = sublist[i]
        i = i + 1
    }
}

var values  = [7, 6, 5, 4, 3, 2, 1, 0]
var indices = [6, 1, 7]
System.print("Initial: %(values)")
sortDisjoint.call(values, indices)
System.print("Sorted : %(values)")

  

You may also check:How to resolve the algorithm Determine if a string has all unique characters step by step in the jq programming language
You may also check:How to resolve the algorithm CSV data manipulation step by step in the Aime programming language
You may also check:How to resolve the algorithm Closures/Value capture step by step in the R programming language
You may also check:How to resolve the algorithm Averages/Pythagorean means step by step in the 11l programming language
You may also check:How to resolve the algorithm Huffman coding step by step in the JavaScript programming language