| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=ip), | intent(inout), | dimension(:) | :: | ivec |
subroutine sort_ascending_64(ivec) integer(ip),dimension(:),intent(inout) :: ivec integer(ip),parameter :: max_size_for_insertion_sort = 20 !! max size for using insertion sort. call quicksort(1_ip,size(ivec,kind=ip)) contains recursive subroutine quicksort(ilow,ihigh) !! Sort the array integer(ip),intent(in) :: ilow integer(ip),intent(in) :: ihigh integer(ip) :: ipivot !! pivot element integer(ip) :: i !! counter integer(ip) :: j !! counter if ( ihigh-ilow<=max_size_for_insertion_sort .and. ihigh>ilow ) then ! do insertion sort: do i = ilow + 1,ihigh do j = i,ilow + 1,-1 if ( ivec(j) < ivec(j-1) ) then call swap64(ivec(j),ivec(j-1)) else exit end if end do end do elseif ( ihigh-ilow>max_size_for_insertion_sort ) then ! do the normal quicksort: call partition(ilow,ihigh,ipivot) call quicksort(ilow,ipivot - 1) call quicksort(ipivot + 1,ihigh) end if end subroutine quicksort subroutine partition(ilow,ihigh,ipivot) !! Partition the array, based on the !! lexical ivecing comparison. implicit none integer(ip),intent(in) :: ilow integer(ip),intent(in) :: ihigh integer(ip),intent(out) :: ipivot integer(ip) :: i,ip call swap64(ivec(ilow),ivec((ilow+ihigh)/2)) ip = ilow do i = ilow + 1, ihigh if ( ivec(i) < ivec(ilow) ) then ip = ip + 1 call swap64(ivec(ip),ivec(i)) end if end do call swap64(ivec(ilow),ivec(ip)) ipivot = ip end subroutine partition end subroutine sort_ascending_64