# (j3.2006) an intrinsic for SORT()

Steven G. Kargl kargl
Mon Feb 5 18:08:52 EST 2018

```On Mon, Feb 05, 2018 at 10:47:04PM +0000, Bill Long wrote:
>
> > On Feb 5, 2018, at 3:48 PM, Steve Lionel <steve at stevelionel.com> wrote:
> >
> >  I could see the value in a proposal for a "quicksort" intrinsic that operated on intrinsic data types and rank-1 arrays only.
>
> Except that ?quicksort? is misnamed. OK, it?s better than Bubble sort.  But for sorting a long list of integers (representing, for example, genomic data), radix sort is often faster.  And it scales linearly both with the length of the list  and (inversely)  with the number of images.  At least in the versions I?m familiar with, the sorting part is mostly masked by the time it takes to read in the data.  You write the two operations intertwined to hide the cross-image  communication time. Or, alternatively, hide the READ time.
>
> We could easily add a crummy, non-performant quicksort and warn people it is only for convenience.  But that does not seem within our normal domain.  You can find a library routine that would do that.
>
> Cheers,
> Bill
>

Why restrict it to a single algorithm?

function sort(array, order, sort_type)
numeric_type_spec :: sort(:)
numeric_type_spec, intent(in) :: array(:)
logical, intent(in), optional :: order
character(len=:), intent(in), optional :: sort_type
end function sort

or

subroutine sort(array, order, sort_type)
numeric_type_spec, intent(inout) :: array(:)
logical, intent(in), optional :: order
character(len=:), intent(in), optional :: sort_type
end subroutine sort

If order is not present, then assume ascending order; else,
order == .true. means ascending
order == .false. means descending

If sort_type is not present, then use processor-dependent algorithm; else,
sort_type == 'quicksort' means quicksort
sort_type == 'insertion' means insertion sort
sort_type == 'heap' means heap sort
sort_type == 'radix' means ...
...

--
Steve

```