partition_indices_integer Function

private function partition_indices_integer(values, indices, low, high, ascending) result(pivot_idx)

Partition function for integer value quicksort

Partitions the indices array around a pivot for quicksort

@param[in] values Integer values to sort by @param[in,out] indices Index array to be partitioned @param[in] low Lower bound of partition range @param[in] high Upper bound of partition range (pivot position) @param[in] ascending Sort direction @return Index of the pivot after partitioning

Arguments

Type IntentOptional Attributes Name
integer(kind=ik), intent(in), dimension(:) :: values
integer, intent(inout), dimension(:) :: indices
integer, intent(in) :: low
integer, intent(in) :: high
logical, intent(in) :: ascending

Return Value integer


Source Code

    function partition_indices_integer(values, indices, low, high, ascending) result(pivot_idx)
        integer(ik), dimension(:), intent(in) :: values
        integer, dimension(:), intent(inout) :: indices
        integer, intent(in) :: low, high
        logical, intent(in) :: ascending
        integer :: pivot_idx
        integer(ik) :: pivot
        integer :: i, j, temp

        pivot = values(indices(high))
        i = low - 1

        do j = low, high - 1
            if ((ascending .and. values(indices(j)) <= pivot) .or. &
                (.not. ascending .and. values(indices(j)) >= pivot)) then
                i = i + 1
                temp = indices(i)
                indices(i) = indices(j)
                indices(j) = temp
            end if
        end do

        temp = indices(i + 1)
        indices(i + 1) = indices(high)
        indices(high) = temp
        pivot_idx = i + 1
    end function partition_indices_integer