A sorted table can tell where a new value belongs by counting values below it.

Program

Play the program to change the score and see its insertion position.

score
insert_position.f90
Replay: real traced execution (multi-file project)
program insert_position_demo
    implicit none
    integer :: sorted(4)
    integer :: score
    integer :: i
    integer :: position

    sorted = [10, 20, 30, 40]
    score = 25
    position = 1
    do i = 1, 4
        if (score > sorted(i)) position = i + 1
    end do
    print '(I0, 1X, I0)', score, position
end program insert_position_demo
program insert_position_demo
    implicit none
    integer :: sorted(4)
    integer :: score
    integer :: i
    integer :: position

    sorted = [10, 20, 30, 40]
    score = 15
    position = 1
    do i = 1, 4
        if (score > sorted(i)) position = i + 1
    end do
    print '(I0, 1X, I0)', score, position
end program insert_position_demo
program insert_position_demo
    implicit none
    integer :: sorted(4)
    integer :: score
    integer :: i
    integer :: position

    sorted = [10, 20, 30, 40]
    score = 45
    position = 1
    do i = 1, 4
        if (score > sorted(i)) position = i + 1
    end do
    print '(I0, 1X, I0)', score, position
end program insert_position_demo
  1. sorted ← [10, 20, 30, 40]

    8sorted = [10, 20, 30, 40]9score = 25
    values this step[10, 20, 30, 40]sorted
  2. score ← 25

    8sorted = [10, 20, 30, 40]9score = 2510position = 1
    values this step25score
  3. position ← 1

    9score = 2510position = 111do i = 1, 4
    values this step1position
  4. i ← 1

    10position = 111do i = 1, 412    if (score > sorted(i)) position = i + 1
    values this step1i
  5. position ← 2

    11do i = 1, 412    if (score > sorted(i)) position = i + 113end do
    values this step2position.true.score > sorted(1)
  6. i ← 2

    10position = 111do i = 1, 412    if (score > sorted(i)) position = i + 1
    values this step2i
  7. position ← 3

    11do i = 1, 412    if (score > sorted(i)) position = i + 113end do
    values this step3position.true.score > sorted(2)
  8. i ← 3

    10position = 111do i = 1, 412    if (score > sorted(i)) position = i + 1
    values this step3i
  9. if (score > sorted(i)) position = i + 1

    11do i = 1, 412    if (score > sorted(i)) position = i + 113end do
    values this step.false.score > sorted(3)
  10. i ← 4

    10position = 111do i = 1, 412    if (score > sorted(i)) position = i + 1
    values this step4i
  11. if (score > sorted(i)) position = i + 1

    11do i = 1, 412    if (score > sorted(i)) position = i + 113end do
    values this step.false.score > sorted(4)
  12. print '(I0, 1X, I0)', score, position

    13    end do14    print '(I0, 1X, I0)', score, position15end program insert_position_demo
    output25 3
    values this step25score3position
  1. sorted ← [10, 20, 30, 40]

    8sorted = [10, 20, 30, 40]9score = 15
    values this step[10, 20, 30, 40]sorted
  2. score ← 15

    8sorted = [10, 20, 30, 40]9score = 1510position = 1
    values this step15score
  3. position ← 1

    9score = 1510position = 111do i = 1, 4
    values this step1position
  4. i ← 1

    10position = 111do i = 1, 412    if (score > sorted(i)) position = i + 1
    values this step1i
  5. position ← 2

    11do i = 1, 412    if (score > sorted(i)) position = i + 113end do
    values this step2position.true.score > sorted(1)
  6. i ← 2

    10position = 111do i = 1, 412    if (score > sorted(i)) position = i + 1
    values this step2i
  7. if (score > sorted(i)) position = i + 1

    11do i = 1, 412    if (score > sorted(i)) position = i + 113end do
    values this step.false.score > sorted(2)
  8. i ← 3

    10position = 111do i = 1, 412    if (score > sorted(i)) position = i + 1
    values this step3i
  9. if (score > sorted(i)) position = i + 1

    11do i = 1, 412    if (score > sorted(i)) position = i + 113end do
    values this step.false.score > sorted(3)
  10. i ← 4

    10position = 111do i = 1, 412    if (score > sorted(i)) position = i + 1
    values this step4i
  11. if (score > sorted(i)) position = i + 1

    11do i = 1, 412    if (score > sorted(i)) position = i + 113end do
    values this step.false.score > sorted(4)
  12. print '(I0, 1X, I0)', score, position

    13    end do14    print '(I0, 1X, I0)', score, position15end program insert_position_demo
    output15 2
    values this step15score2position
  1. sorted ← [10, 20, 30, 40]

    8sorted = [10, 20, 30, 40]9score = 45
    values this step[10, 20, 30, 40]sorted
  2. score ← 45

    8sorted = [10, 20, 30, 40]9score = 4510position = 1
    values this step45score
  3. position ← 1

    9score = 4510position = 111do i = 1, 4
    values this step1position
  4. i ← 1

    10position = 111do i = 1, 412    if (score > sorted(i)) position = i + 1
    values this step1i
  5. position ← 2

    11do i = 1, 412    if (score > sorted(i)) position = i + 113end do
    values this step2position.true.score > sorted(1)
  6. i ← 2

    10position = 111do i = 1, 412    if (score > sorted(i)) position = i + 1
    values this step2i
  7. position ← 3

    11do i = 1, 412    if (score > sorted(i)) position = i + 113end do
    values this step3position.true.score > sorted(2)
  8. i ← 3

    10position = 111do i = 1, 412    if (score > sorted(i)) position = i + 1
    values this step3i
  9. position ← 4

    11do i = 1, 412    if (score > sorted(i)) position = i + 113end do
    values this step4position.true.score > sorted(3)
  10. i ← 4

    10position = 111do i = 1, 412    if (score > sorted(i)) position = i + 1
    values this step4i
  11. position ← 5

    11do i = 1, 412    if (score > sorted(i)) position = i + 113end do
    values this step5position.true.score > sorted(4)
  12. print '(I0, 1X, I0)', score, position

    13    end do14    print '(I0, 1X, I0)', score, position15end program insert_position_demo
    output45 5
    values this step45score5position
sorted table The existing values are already ordered from low to high.
insertion position `position` advances once for every sorted value below `score`.
rank idea The final position is a rank-like answer for where the score belongs.