Sort keys, increasing (a bottom-up merge sort).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=int64), | intent(inout) | :: | a(:) |
the keys |
pure subroutine sort_keys(a) integer(int64), intent(inout) :: a(:) !! the keys integer(int64), allocatable :: b(:) integer :: n, width, lo, mid, hi, i, j, k n = size(a) allocate(b(n)) width = 1 do while (width < n) do lo = 1, n, 2 * width mid = min(lo + width, n + 1) hi = min(lo + 2 * width, n + 1) i = lo j = mid do k = lo, hi - 1 if (i < mid .and. (j >= hi .or. a(min(i, n)) <= a(min(j, n)))) then b(k) = a(i) i = i + 1 else b(k) = a(j) j = j + 1 end if end do end do a = b width = 2 * width end do end subroutine sort_keys