sort_special(3f) - [M_orderpack:SORT] Sorts array into ascending order (Insertion sort, generally for small or nearly sorted arrays)
Synopsis
Description
Options
Examples
Author
Maintainer
License
Subroutine Sort_Special (INOUTVALS)
${TYPE} (kind=${KIND}), Intent (InOut) :: INOUTVALS(:)Where ${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=*)
Sorts INOUTVALS() into ascending order (Insertion sort).If certain requirements are met and performance is important this procedure can be far faster, but SORT(3f) and ranking routines RANK(3f) and RANK_BASIC(3f) are recommended for the general case.
This subroutine uses an Insertion sort. It does not use any work array and is faster when INOUTVALS() is of very small size (< 20), or already almost sorted; but worst case behavior can be triggered by commonly encountered data order (e.g. initially inverse sorted). Therefore, in many cases the Quick-sort or Merge-sort method is faster.
INOUTVALS array to sort
Sample program:
program demo_sort_special ! sort an array using insertion sort use,intrinsic :: iso_fortran_env, only : int32, real32, real64 use M_orderpack, only : sort_special implicit none ! an insertion sort is very efficient for very small arrays ! but generally slower than methods like quick-sort and merge-sort. integer,parameter :: isz=2000 real(kind=real64) :: dd(isz), hi, low ! make an array of random values call random_seed() call random_number(dd) dd=dd*1000000.0-500000.0 low= minval(dd) hi = maxval(dd) ! sort the data call sort_special(dd) ! cursory checks if(any(dd(1:isz-1) .gt. dd(2:isz)))stop ERROR: array not sorted write(*,*)check min:,dd(1).eq.low write(*,*)check max:,dd(isz).eq.hi write(*,*)PASSED: random array is now sorted end program demo_sort_specialResults:
check min: T check max: T PASSED: random array is now sorted
Michel Olagnon - Apr. 2000
John Urban, 2022.04.16
CC0-1.0
Nemo Release 3.1 | sort_special (3) | February 23, 2025 |