Sorting and Ranking
Insert Position
Ranking Against Sorted Values
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.
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
sorted ← [10, 20, 30, 40]
8sorted = [10, 20, 30, 40]9score = 25values this step[10, 20, 30, 40]sortedscore ← 25
8sorted = [10, 20, 30, 40]9score = 2510position = 1values this step25scoreposition ← 1
9score = 2510position = 111do i = 1, 4values this step1positioni ← 1
10position = 111do i = 1, 412 if (score > sorted(i)) position = i + 1values this step1iposition ← 2
11do i = 1, 412 if (score > sorted(i)) position = i + 113end dovalues this step2position.true.score > sorted(1)i ← 2
10position = 111do i = 1, 412 if (score > sorted(i)) position = i + 1values this step2iposition ← 3
11do i = 1, 412 if (score > sorted(i)) position = i + 113end dovalues this step3position.true.score > sorted(2)i ← 3
10position = 111do i = 1, 412 if (score > sorted(i)) position = i + 1values this step3iif (score > sorted(i)) position = i + 1
11do i = 1, 412 if (score > sorted(i)) position = i + 113end dovalues this step.false.score > sorted(3)i ← 4
10position = 111do i = 1, 412 if (score > sorted(i)) position = i + 1values this step4iif (score > sorted(i)) position = i + 1
11do i = 1, 412 if (score > sorted(i)) position = i + 113end dovalues this step.false.score > sorted(4)print '(I0, 1X, I0)', score, position
13 end do14 print '(I0, 1X, I0)', score, position15end program insert_position_demooutput25 3values this step25score3position
sorted ← [10, 20, 30, 40]
8sorted = [10, 20, 30, 40]9score = 15values this step[10, 20, 30, 40]sortedscore ← 15
8sorted = [10, 20, 30, 40]9score = 1510position = 1values this step15scoreposition ← 1
9score = 1510position = 111do i = 1, 4values this step1positioni ← 1
10position = 111do i = 1, 412 if (score > sorted(i)) position = i + 1values this step1iposition ← 2
11do i = 1, 412 if (score > sorted(i)) position = i + 113end dovalues this step2position.true.score > sorted(1)i ← 2
10position = 111do i = 1, 412 if (score > sorted(i)) position = i + 1values this step2iif (score > sorted(i)) position = i + 1
11do i = 1, 412 if (score > sorted(i)) position = i + 113end dovalues this step.false.score > sorted(2)i ← 3
10position = 111do i = 1, 412 if (score > sorted(i)) position = i + 1values this step3iif (score > sorted(i)) position = i + 1
11do i = 1, 412 if (score > sorted(i)) position = i + 113end dovalues this step.false.score > sorted(3)i ← 4
10position = 111do i = 1, 412 if (score > sorted(i)) position = i + 1values this step4iif (score > sorted(i)) position = i + 1
11do i = 1, 412 if (score > sorted(i)) position = i + 113end dovalues this step.false.score > sorted(4)print '(I0, 1X, I0)', score, position
13 end do14 print '(I0, 1X, I0)', score, position15end program insert_position_demooutput15 2values this step15score2position
sorted ← [10, 20, 30, 40]
8sorted = [10, 20, 30, 40]9score = 45values this step[10, 20, 30, 40]sortedscore ← 45
8sorted = [10, 20, 30, 40]9score = 4510position = 1values this step45scoreposition ← 1
9score = 4510position = 111do i = 1, 4values this step1positioni ← 1
10position = 111do i = 1, 412 if (score > sorted(i)) position = i + 1values this step1iposition ← 2
11do i = 1, 412 if (score > sorted(i)) position = i + 113end dovalues this step2position.true.score > sorted(1)i ← 2
10position = 111do i = 1, 412 if (score > sorted(i)) position = i + 1values this step2iposition ← 3
11do i = 1, 412 if (score > sorted(i)) position = i + 113end dovalues this step3position.true.score > sorted(2)i ← 3
10position = 111do i = 1, 412 if (score > sorted(i)) position = i + 1values this step3iposition ← 4
11do i = 1, 412 if (score > sorted(i)) position = i + 113end dovalues this step4position.true.score > sorted(3)i ← 4
10position = 111do i = 1, 412 if (score > sorted(i)) position = i + 1values this step4iposition ← 5
11do i = 1, 412 if (score > sorted(i)) position = i + 113end dovalues this step5position.true.score > sorted(4)print '(I0, 1X, I0)', score, position
13 end do14 print '(I0, 1X, I0)', score, position15end program insert_position_demooutput45 5values 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.