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

Published on 12 May 2024 09:40 PM

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

Source code in the action! programming language

PROC PrintArray(INT ARRAY a INT size)
  INT i

  Put('[)
  FOR i=0 TO size-1
  DO
    IF i>0 THEN Put(' ) FI
    PrintI(a(i))
  OD
  Put(']) PutE()
RETURN

BYTE FUNC InSet(INT ARRAY s INT size INT v)
  INT i

  FOR i=0 TO size-1
  DO
    IF s(i)=v THEN
      RETURN (1)
    FI
  OD
RETURN (0)

PROC Sort(INT ARRAY arr INT arrSize
          INT ARRAY ind INT indSize)
  INT i,j,minpos,tmp

  FOR i=0 TO arrSize-2
  DO
    IF InSet(ind,indSize,i) THEN
      minpos=i
      FOR j=i+1 TO arrSize-1
      DO
        IF InSet(ind,indSize,j)=1 AND arr(minpos)>arr(j) THEN
          minpos=j
        FI
      OD
    
      IF minpos#i THEN
        tmp=arr(i)
        arr(i)=arr(minpos)
        arr(minpos)=tmp
      FI
    FI
  OD
RETURN

PROC Test(INT ARRAY arr INT arrSize
          INT ARRAY ind INT indSize)
  PrintE("Array before sort:")
  PrintArray(arr,arrSize)
  PrintE("Indices:")
  PrintArray(ind,indSize)
  Sort(arr,arrSize,ind,indSize)
  PrintE("Array after sort:")
  PrintArray(arr,arrSize)
RETURN

PROC Main()
  INT ARRAY
    arr(8)=[7 6 5 4 3 2 1 0],
    ind(3)=[6 1 7]
  
  Test(arr,8,ind,3)
RETURN

  

You may also check:How to resolve the algorithm 100 doors step by step in the Pony programming language
You may also check:How to resolve the algorithm Write float arrays to a text file step by step in the HicEst programming language
You may also check:How to resolve the algorithm Bitmap/Read a PPM file step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Palindrome dates step by step in the Go programming language
You may also check:How to resolve the algorithm Empty program step by step in the MUMPS programming language