A selection-style nested loop can order a small active prefix by swapping lower values forward.

Program

Play the program to sort a different number of active values.

selection_sort_prefix.f90
program selection_sort_prefix_demo
    implicit none
    integer :: values(4)
    integer :: item_count
    integer :: i
    integer :: j
    integer :: temp

    values = [9, 4, 7, 2]
    item_count = 
    do i = 1, item_count - 1
        do j = i + 1, item_count
            if (values(j) < values(i)) then
                temp = values(i)
                values(i) = values(j)
                values(j) = temp
            end if
        end do
    end do
    print '(I0, *(1X, I0))', item_count, (values(i), i = 1, item_count)
end program selection_sort_prefix_demo
program selection_sort_prefix_demo
    implicit none
    integer :: values(4)
    integer :: item_count
    integer :: i
    integer :: j
    integer :: temp

    values = [9, 4, 7, 2]
    item_count = 
    do i = 1, item_count - 1
        do j = i + 1, item_count
            if (values(j) < values(i)) then
                temp = values(i)
                values(i) = values(j)
                values(j) = temp
            end if
        end do
    end do
    print '(I0, *(1X, I0))', item_count, (values(i), i = 1, item_count)
end program selection_sort_prefix_demo
program selection_sort_prefix_demo
    implicit none
    integer :: values(4)
    integer :: item_count
    integer :: i
    integer :: j
    integer :: temp

    values = [9, 4, 7, 2]
    item_count = 
    do i = 1, item_count - 1
        do j = i + 1, item_count
            if (values(j) < values(i)) then
                temp = values(i)
                values(i) = values(j)
                values(j) = temp
            end if
        end do
    end do
    print '(I0, *(1X, I0))', item_count, (values(i), i = 1, item_count)
end program selection_sort_prefix_demo
active prefix `item_count` chooses how much of the array is sorted.
nested scan Each outer position compares against later active positions.
swap `temp` holds one value while two array positions trade places.