How to resolve the algorithm Sort disjoint sublist step by step in the Fortran programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sort disjoint sublist step by step in the Fortran 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 Fortran programming language
Source code in the fortran programming language
program Example
implicit none
integer :: array(8) = (/ 7, 6, 5, 4, 3, 2, 1, 0 /)
integer :: indices(3) = (/ 7, 2, 8 /)
! In order to make the output insensitive to index order
! we need to sort the indices first
call Isort(indices)
! Should work with any sort routine as long as the dummy
! argument array has been declared as an assumed shape array
! Standard insertion sort used in this example
call Isort(array(indices))
write(*,*) array
contains
subroutine Isort(a)
integer, intent(in out) :: a(:)
integer :: temp
integer :: i, j
do i = 2, size(a)
j = i - 1
temp = a(i)
do while (j>=1 .and. a(j)>temp)
a(j+1) = a(j)
j = j - 1
end do
a(j+1) = temp
end do
end subroutine Isort
end program Example
You may also check:How to resolve the algorithm Sparkline in unicode step by step in the Rust programming language
You may also check:How to resolve the algorithm Permutations step by step in the Mercury programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the Ada programming language
You may also check:How to resolve the algorithm Text processing/2 step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Jaro similarity step by step in the Pascal programming language