psort(3f) - [M_orderpack:SORT:PARTIAL] partially sorts an array (Insertion Sort, generally for small or nearly sorted arrays)
Synopsis
Description
Options
Examples
Author
Maintainer
License
Subroutine Psort (INOUTVALS, NORD)
${TYPE} (kind=${KIND}), Intent (InOut) :: INOUTVALS(:) Integer, Intent (In) :: NORDWhere ${TYPE}(kind=${KIND}) may be
o Real(kind=real32) o Real(kind=real64) o Integer(kind=int32) o Character(kind=selected_char_kind("DEFAULT"),len=*)
PSORT(3f) partially sorts INOUTVALS, bringing the NORD lowest values to the beginning of the array.Internally, this subroutine uses an insertion sort, limiting insertion to the first NORD values. It does not use any work array and is faster when NORD is very small (2-5), but worst case behavior can happen fairly probably (initially inverse sorted). Therefore, in many cases, the refined quick-sort method is faster.
INOUTVALS The array to partially sort NORD number of sorted values to return.
Sample program:
program demo_psort ! partially sort an array use M_orderpack, only : psort implicit none character(len=*),parameter :: g=(*(g0,1x)) integer :: nordResults:int: block integer,allocatable :: ia(:) ia=[10,5,7,1,4,5,6,8,9,10,1] nord=5 write(*,g)Original.................:,ia call psort(ia,nord) write(*,g)Number of indices to sort:,nord write(*,g)nord,Lowest values..........:,ia(:nord) write(*,g)Entire array.............:,ia write(*,g) endblock int char: block character(len=:),allocatable :: ca(:) integer :: i ca=[character(len=20) :: fan,a,car,be,egg,dam,gas] nord=3 write(*,g)Original.................:,(trim(ca(i)),i=1,size(ca)) call psort(ca,nord) write(*,g)Number of indices to sort:,nord write(*,g)nord,Lowest values..........:,(trim(ca(i)),i=1,nord) write(*,g)Entire array.............:,(trim(ca(i)),i=1,size(ca)) write(*,g) endblock char
end program demo_psort
Original.................: 10 5 7 1 4 5 6 8 9 10 1 Number of indices to sort: 5 5 Lowest values..........: 1 1 4 5 5 Entire array.............: 1 1 4 5 5 10 7 8 9 10 6Original.................: fan a car be egg dam gas Number of indices to sort: 3 3 Lowest values..........: a be car Entire array.............: a be car fan egg dam gas
Michel Olagnon - Feb. 2000
John Urban, 2022.04.16
CC0-1.0
Nemo Release 3.1 | psort (3) | February 23, 2025 |