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.

item_count
selection_sort_prefix.f90
Replay: real traced execution (multi-file project)
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 = 3
    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 = 2
    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 = 4
    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
  1. values ← [9, 4, 7, 2]

    9values = [9, 4, 7, 2]10item_count = 3
    values this step[9, 4, 7, 2]values
  2. item_count ← 3

    9values = [9, 4, 7, 2]10item_count = 311do i = 1, item_count - 1
    values this step3item_count
  3. i ← 1

    10item_count = 311do i = 1, item_count - 112    do j = i + 1, item_count
    values this step1i
  4. j ← 2

    11do i = 1, item_count - 112    do j = i + 1, item_count13        if (values(j) < values(i)) then
    values this step2j
  5. if (values(j) < values(i)) then

    12do j = i + 1, item_count13    if (values(j) < values(i)) then14        temp = values(i)
    values this step.true.values(2) < values(1)
  6. temp ← 9

    13if (values(j) < values(i)) then14    temp = values(i)15    values(i) = values(j)
    values this step9temp9values(1)
  7. values ← [4, 4, 7, 2]

    14temp = values(i)15values(i) = values(j)16values(j) = temp
    values this step[4, 4, 7, 2]values
  8. values ← [4, 9, 7, 2]

    15    values(i) = values(j)16    values(j) = temp17end if
    values this step[4, 9, 7, 2]values
  9. j ← 3

    11do i = 1, item_count - 112    do j = i + 1, item_count13        if (values(j) < values(i)) then
    values this step3j
  10. if (values(j) < values(i)) then

    12do j = i + 1, item_count13    if (values(j) < values(i)) then14        temp = values(i)
    values this step.false.values(3) < values(1)
  11. i ← 2

    10item_count = 311do i = 1, item_count - 112    do j = i + 1, item_count
    values this step2i
  12. j ← 3

    11do i = 1, item_count - 112    do j = i + 1, item_count13        if (values(j) < values(i)) then
    values this step3j
  13. if (values(j) < values(i)) then

    12do j = i + 1, item_count13    if (values(j) < values(i)) then14        temp = values(i)
    values this step.true.values(3) < values(2)
  14. temp ← 9

    13if (values(j) < values(i)) then14    temp = values(i)15    values(i) = values(j)
    values this step9temp9values(2)
  15. values ← [4, 7, 7, 2]

    14temp = values(i)15values(i) = values(j)16values(j) = temp
    values this step[4, 7, 7, 2]values
  16. values ← [4, 7, 9, 2]

    15    values(i) = values(j)16    values(j) = temp17end if
    values this step[4, 7, 9, 2]values
  17. print '(I0, *(1X, I0))', item_count, (values(i), i = 1, item_count)

    19    end do20    print '(I0, *(1X, I0))', item_count, (values(i), i = 1, item_count)21end program selection_sort_prefix_demo
    output3 4 7 9
    values this step3item_count[4, 7, 9, 2]values
  1. values ← [9, 4, 7, 2]

    9values = [9, 4, 7, 2]10item_count = 2
    values this step[9, 4, 7, 2]values
  2. item_count ← 2

    9values = [9, 4, 7, 2]10item_count = 211do i = 1, item_count - 1
    values this step2item_count
  3. i ← 1

    10item_count = 211do i = 1, item_count - 112    do j = i + 1, item_count
    values this step1i
  4. j ← 2

    11do i = 1, item_count - 112    do j = i + 1, item_count13        if (values(j) < values(i)) then
    values this step2j
  5. if (values(j) < values(i)) then

    12do j = i + 1, item_count13    if (values(j) < values(i)) then14        temp = values(i)
    values this step.true.values(2) < values(1)
  6. temp ← 9

    13if (values(j) < values(i)) then14    temp = values(i)15    values(i) = values(j)
    values this step9temp9values(1)
  7. values ← [4, 4, 7, 2]

    14temp = values(i)15values(i) = values(j)16values(j) = temp
    values this step[4, 4, 7, 2]values
  8. values ← [4, 9, 7, 2]

    15    values(i) = values(j)16    values(j) = temp17end if
    values this step[4, 9, 7, 2]values
  9. print '(I0, *(1X, I0))', item_count, (values(i), i = 1, item_count)

    19    end do20    print '(I0, *(1X, I0))', item_count, (values(i), i = 1, item_count)21end program selection_sort_prefix_demo
    output2 4 9
    values this step2item_count[4, 9, 7, 2]values
  1. values ← [9, 4, 7, 2]

    9values = [9, 4, 7, 2]10item_count = 4
    values this step[9, 4, 7, 2]values
  2. item_count ← 4

    9values = [9, 4, 7, 2]10item_count = 411do i = 1, item_count - 1
    values this step4item_count
  3. i ← 1

    10item_count = 411do i = 1, item_count - 112    do j = i + 1, item_count
    values this step1i
  4. j ← 2

    11do i = 1, item_count - 112    do j = i + 1, item_count13        if (values(j) < values(i)) then
    values this step2j
  5. if (values(j) < values(i)) then

    12do j = i + 1, item_count13    if (values(j) < values(i)) then14        temp = values(i)
    values this step.true.values(2) < values(1)
  6. temp ← 9

    13if (values(j) < values(i)) then14    temp = values(i)15    values(i) = values(j)
    values this step9temp9values(1)
  7. values ← [4, 4, 7, 2]

    14temp = values(i)15values(i) = values(j)16values(j) = temp
    values this step[4, 4, 7, 2]values
  8. values ← [4, 9, 7, 2]

    15    values(i) = values(j)16    values(j) = temp17end if
    values this step[4, 9, 7, 2]values
  9. j ← 3

    11do i = 1, item_count - 112    do j = i + 1, item_count13        if (values(j) < values(i)) then
    values this step3j
  10. if (values(j) < values(i)) then

    12do j = i + 1, item_count13    if (values(j) < values(i)) then14        temp = values(i)
    values this step.false.values(3) < values(1)
  11. j ← 4

    11do i = 1, item_count - 112    do j = i + 1, item_count13        if (values(j) < values(i)) then
    values this step4j
  12. if (values(j) < values(i)) then

    12do j = i + 1, item_count13    if (values(j) < values(i)) then14        temp = values(i)
    values this step.true.values(4) < values(1)
  13. temp ← 4

    13if (values(j) < values(i)) then14    temp = values(i)15    values(i) = values(j)
    values this step4temp4values(1)
  14. values ← [2, 9, 7, 2]

    14temp = values(i)15values(i) = values(j)16values(j) = temp
    values this step[2, 9, 7, 2]values
  15. values ← [2, 9, 7, 4]

    15    values(i) = values(j)16    values(j) = temp17end if
    values this step[2, 9, 7, 4]values
  16. i ← 2

    10item_count = 411do i = 1, item_count - 112    do j = i + 1, item_count
    values this step2i
  17. j ← 3

    11do i = 1, item_count - 112    do j = i + 1, item_count13        if (values(j) < values(i)) then
    values this step3j
  18. if (values(j) < values(i)) then

    12do j = i + 1, item_count13    if (values(j) < values(i)) then14        temp = values(i)
    values this step.true.values(3) < values(2)
  19. temp ← 9

    13if (values(j) < values(i)) then14    temp = values(i)15    values(i) = values(j)
    values this step9temp9values(2)
  20. values ← [2, 7, 7, 4]

    14temp = values(i)15values(i) = values(j)16values(j) = temp
    values this step[2, 7, 7, 4]values
  21. values ← [2, 7, 9, 4]

    15    values(i) = values(j)16    values(j) = temp17end if
    values this step[2, 7, 9, 4]values
  22. j ← 4

    11do i = 1, item_count - 112    do j = i + 1, item_count13        if (values(j) < values(i)) then
    values this step4j
  23. if (values(j) < values(i)) then

    12do j = i + 1, item_count13    if (values(j) < values(i)) then14        temp = values(i)
    values this step.true.values(4) < values(2)
  24. temp ← 7

    13if (values(j) < values(i)) then14    temp = values(i)15    values(i) = values(j)
    values this step7temp7values(2)
  25. values ← [2, 4, 9, 4]

    14temp = values(i)15values(i) = values(j)16values(j) = temp
    values this step[2, 4, 9, 4]values
  26. values ← [2, 4, 9, 7]

    15    values(i) = values(j)16    values(j) = temp17end if
    values this step[2, 4, 9, 7]values
  27. i ← 3

    10item_count = 411do i = 1, item_count - 112    do j = i + 1, item_count
    values this step3i
  28. j ← 4

    11do i = 1, item_count - 112    do j = i + 1, item_count13        if (values(j) < values(i)) then
    values this step4j
  29. if (values(j) < values(i)) then

    12do j = i + 1, item_count13    if (values(j) < values(i)) then14        temp = values(i)
    values this step.true.values(4) < values(3)
  30. temp ← 9

    13if (values(j) < values(i)) then14    temp = values(i)15    values(i) = values(j)
    values this step9temp9values(3)
  31. values ← [2, 4, 7, 7]

    14temp = values(i)15values(i) = values(j)16values(j) = temp
    values this step[2, 4, 7, 7]values
  32. values ← [2, 4, 7, 9]

    15    values(i) = values(j)16    values(j) = temp17end if
    values this step[2, 4, 7, 9]values
  33. print '(I0, *(1X, I0))', item_count, (values(i), i = 1, item_count)

    19    end do20    print '(I0, *(1X, I0))', item_count, (values(i), i = 1, item_count)21end program selection_sort_prefix_demo
    output4 2 4 7 9
    values this step4item_count[2, 4, 7, 9]values
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.