Sorting and Ranking
Selection Sort Prefix
Swapping Into Order
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
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
values ← [9, 4, 7, 2]
9values = [9, 4, 7, 2]10item_count = 3values this step[9, 4, 7, 2]valuesitem_count ← 3
9values = [9, 4, 7, 2]10item_count = 311do i = 1, item_count - 1values this step3item_counti ← 1
10item_count = 311do i = 1, item_count - 112 do j = i + 1, item_countvalues this step1ij ← 2
11do i = 1, item_count - 112 do j = i + 1, item_count13 if (values(j) < values(i)) thenvalues this step2jif (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)temp ← 9
13if (values(j) < values(i)) then14 temp = values(i)15 values(i) = values(j)values this step9temp9values(1)values ← [4, 4, 7, 2]
14temp = values(i)15values(i) = values(j)16values(j) = tempvalues this step[4, 4, 7, 2]valuesvalues ← [4, 9, 7, 2]
15 values(i) = values(j)16 values(j) = temp17end ifvalues this step[4, 9, 7, 2]valuesj ← 3
11do i = 1, item_count - 112 do j = i + 1, item_count13 if (values(j) < values(i)) thenvalues this step3jif (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)i ← 2
10item_count = 311do i = 1, item_count - 112 do j = i + 1, item_countvalues this step2ij ← 3
11do i = 1, item_count - 112 do j = i + 1, item_count13 if (values(j) < values(i)) thenvalues this step3jif (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)temp ← 9
13if (values(j) < values(i)) then14 temp = values(i)15 values(i) = values(j)values this step9temp9values(2)values ← [4, 7, 7, 2]
14temp = values(i)15values(i) = values(j)16values(j) = tempvalues this step[4, 7, 7, 2]valuesvalues ← [4, 7, 9, 2]
15 values(i) = values(j)16 values(j) = temp17end ifvalues this step[4, 7, 9, 2]valuesprint '(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_demooutput3 4 7 9values this step3item_count[4, 7, 9, 2]values
values ← [9, 4, 7, 2]
9values = [9, 4, 7, 2]10item_count = 2values this step[9, 4, 7, 2]valuesitem_count ← 2
9values = [9, 4, 7, 2]10item_count = 211do i = 1, item_count - 1values this step2item_counti ← 1
10item_count = 211do i = 1, item_count - 112 do j = i + 1, item_countvalues this step1ij ← 2
11do i = 1, item_count - 112 do j = i + 1, item_count13 if (values(j) < values(i)) thenvalues this step2jif (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)temp ← 9
13if (values(j) < values(i)) then14 temp = values(i)15 values(i) = values(j)values this step9temp9values(1)values ← [4, 4, 7, 2]
14temp = values(i)15values(i) = values(j)16values(j) = tempvalues this step[4, 4, 7, 2]valuesvalues ← [4, 9, 7, 2]
15 values(i) = values(j)16 values(j) = temp17end ifvalues this step[4, 9, 7, 2]valuesprint '(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_demooutput2 4 9values this step2item_count[4, 9, 7, 2]values
values ← [9, 4, 7, 2]
9values = [9, 4, 7, 2]10item_count = 4values this step[9, 4, 7, 2]valuesitem_count ← 4
9values = [9, 4, 7, 2]10item_count = 411do i = 1, item_count - 1values this step4item_counti ← 1
10item_count = 411do i = 1, item_count - 112 do j = i + 1, item_countvalues this step1ij ← 2
11do i = 1, item_count - 112 do j = i + 1, item_count13 if (values(j) < values(i)) thenvalues this step2jif (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)temp ← 9
13if (values(j) < values(i)) then14 temp = values(i)15 values(i) = values(j)values this step9temp9values(1)values ← [4, 4, 7, 2]
14temp = values(i)15values(i) = values(j)16values(j) = tempvalues this step[4, 4, 7, 2]valuesvalues ← [4, 9, 7, 2]
15 values(i) = values(j)16 values(j) = temp17end ifvalues this step[4, 9, 7, 2]valuesj ← 3
11do i = 1, item_count - 112 do j = i + 1, item_count13 if (values(j) < values(i)) thenvalues this step3jif (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)j ← 4
11do i = 1, item_count - 112 do j = i + 1, item_count13 if (values(j) < values(i)) thenvalues this step4jif (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)temp ← 4
13if (values(j) < values(i)) then14 temp = values(i)15 values(i) = values(j)values this step4temp4values(1)values ← [2, 9, 7, 2]
14temp = values(i)15values(i) = values(j)16values(j) = tempvalues this step[2, 9, 7, 2]valuesvalues ← [2, 9, 7, 4]
15 values(i) = values(j)16 values(j) = temp17end ifvalues this step[2, 9, 7, 4]valuesi ← 2
10item_count = 411do i = 1, item_count - 112 do j = i + 1, item_countvalues this step2ij ← 3
11do i = 1, item_count - 112 do j = i + 1, item_count13 if (values(j) < values(i)) thenvalues this step3jif (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)temp ← 9
13if (values(j) < values(i)) then14 temp = values(i)15 values(i) = values(j)values this step9temp9values(2)values ← [2, 7, 7, 4]
14temp = values(i)15values(i) = values(j)16values(j) = tempvalues this step[2, 7, 7, 4]valuesvalues ← [2, 7, 9, 4]
15 values(i) = values(j)16 values(j) = temp17end ifvalues this step[2, 7, 9, 4]valuesj ← 4
11do i = 1, item_count - 112 do j = i + 1, item_count13 if (values(j) < values(i)) thenvalues this step4jif (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)temp ← 7
13if (values(j) < values(i)) then14 temp = values(i)15 values(i) = values(j)values this step7temp7values(2)values ← [2, 4, 9, 4]
14temp = values(i)15values(i) = values(j)16values(j) = tempvalues this step[2, 4, 9, 4]valuesvalues ← [2, 4, 9, 7]
15 values(i) = values(j)16 values(j) = temp17end ifvalues this step[2, 4, 9, 7]valuesi ← 3
10item_count = 411do i = 1, item_count - 112 do j = i + 1, item_countvalues this step3ij ← 4
11do i = 1, item_count - 112 do j = i + 1, item_count13 if (values(j) < values(i)) thenvalues this step4jif (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)temp ← 9
13if (values(j) < values(i)) then14 temp = values(i)15 values(i) = values(j)values this step9temp9values(3)values ← [2, 4, 7, 7]
14temp = values(i)15values(i) = values(j)16values(j) = tempvalues this step[2, 4, 7, 7]valuesvalues ← [2, 4, 7, 9]
15 values(i) = values(j)16 values(j) = temp17end ifvalues this step[2, 4, 7, 9]valuesprint '(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_demooutput4 2 4 7 9values 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.