Sort finite real values ascending in place (heapsort; equal values may change order).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=dp), | intent(inout) | :: | values(:) |
subroutine sort_real_ascending(values) real(dp), intent(inout) :: values(:) real(dp) :: key integer :: start, last last = size(values) do start = last/2, 1, -1 call sift_down(start, last) end do do last = size(values), 2, -1 key = values(last) values(last) = values(1) values(1) = key call sift_down(1, last - 1) end do contains subroutine sift_down(first, limit) integer, intent(in) :: first, limit integer :: root, child real(dp) :: key root = first key = values(root) do while (root <= limit/2) child = 2*root if (child < limit) then if (values(child) < values(child + 1)) child = child + 1 end if if (key >= values(child)) exit values(root) = values(child) root = child end do values(root) = key end subroutine sift_down end subroutine sort_real_ascending