Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting #101

Open
jacobwilliams opened this issue Nov 27, 2019 · 6 comments
Open

Sorting #101

jacobwilliams opened this issue Nov 27, 2019 · 6 comments
Labels
Clause 16 Standard Clause 16: Intrinsic procedures and modules

Comments

@jacobwilliams
Copy link

I wonder if sorting should be built into the language? This is such a fundamental thing that currently take a staggering amount of code to make it work with all intrinsic types and user-defined types (see for example here). I think C++ has sorting built in to its standard library.

Features:

  • sort ascending or descending
  • it works for all intrinsic numeric types (integer(kind=*) and real(kind=*))
  • how about also a "sort x, while carrying y along"? That's always nice to have. See, for example, isort from SLATEC. Or even a variadic "sort x while carrying any number of other arrays along". And any of them can be any combination of types.
  • maybe also indexing?
  • how about lexical sorting? Including "[natural sorting]?(https://en.wikipedia.org/wiki/Natural_sort_order)".

I think it goes without saying that any well-designed future generic/templating feature should also allow one to apply a sorting algorithm (either one from a third-party library or maybe even the hypothetical intrinsic one) to a user-defined type with a minimum of code.

@certik
Copy link
Member

certik commented Nov 28, 2019

@jacobwilliams indeed. We had to implement our own (https://github.com/certik/fortran-utils/blob/b43bd24cd421509a5bc6d3b9c3eeae8ce856ed88/src/sorting.f90) and it's not very efficient. This would be a great addition.

@rweed
Copy link

rweed commented Dec 7, 2019

agree 100% about the need for sorting routines. Since there are numerous examples of quicksort etc. in the literature, implementing this in a standard library should be a no-brainer. I would point to the code in Robin Vowels excellent book "Algorithms and Data Structures in F and Fortran", Dick Hanson and Tim Hopkins book "Numerical Computing with Modern Fortran", and Ajen Markus's FLIBS implementation of quicksort. Also, if Robin Vowels is following this and the other issues on this site, I would love an updated version of your book focused on Modern Fortran implementations of the algorithms and data structures with additional coverage of things like octrees etc. I would buy it yesterday.

@certik
Copy link
Member

certik commented Dec 23, 2019

Let's implement good sorting routines in stdlib (#104). If we make stdlib successful, that would get us 90% there. There might be some language features that would be helpful, but we can have that discussion later.

@leonfoks
Copy link

leonfoks commented Jan 7, 2020

I have a decent introspection sort in Coretran.
Right now it's interfaced and operates on contiguous memory primitive arrays, real32, real64, int32, int64. I have argsort too. I went further than a quicksort to add robustness for edge cases.
Here's the description from my module.

!! Uses an introspective sort on a set of numbers. See this "http://www.geeksforgeeks.org/know-
your-sorting-algorithm-set-2-introsort-cs-sorting-weapon/" for more information
!!
!! To begin, a quicksort with a median of three pivot is used until the size of the array is less than 16. At this point, an insertion sort is used to reduce cache overhead and tail recursion.
!! Unfortunately, a quicksort is not ideal for sorted/almost sorted arrays or arrays with duplicate values. Therefore if the number of iterations exceededs a threshold, a heapsort is used instead.
!! This provides a robust sorting algorithm that is still very fast for almost sorted arrays.
!!
!! In this implementation, the quicksort and heapsort are unstable sorts. A stable merge sort is therefore provided as an alternative but it has an order(N) memory overhead.
!!
!! Often, the numbers wish to be maintained in their given order, so with an O(N) memory overhead we can sort an integer array instead by calling argsort()

The stable merge sort doesn't really matter for numbers, will be important if used for arbitrary types though.

@leonfoks
Copy link

leonfoks commented Jan 7, 2020

Also related, maybe it should be a separate issue.

Should searching algorithms be part of the stdlib? Binary search or interval search?
I use a binary search for a lot of functions in Coretran (again interfaced for the different primitives), including KdTree, the dynamic arrays, inserting into a sorted list etc.

@certik
Copy link
Member

certik commented Jan 7, 2020

I think searching algorithm would also fit into stdlib.

@certik certik added the Clause 16 Standard Clause 16: Intrinsic procedures and modules label Apr 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Clause 16 Standard Clause 16: Intrinsic procedures and modules
Projects
None yet
Development

No branches or pull requests

4 participants