MyNixOS website logo
Description

Sort small lists with sorting network.

Functions that sort small (2~16 elements) lists or homogenous tuples.

sorting-network

Sort small lists with sorting network.

How to use

This library provides functions that can sort small (2 ~ 16 elements), fixed-sized list / homogenous tuple of elements.

To use this library, import Data.SortingNetwork and you will bring two sets of functions into scope:

-- where X is a number from 2-16
unsafeSortListXBy :: (a -> a -> Ordering) -> [a] -> [a]

-- where X is a number from 2-16
sortTup2By :: (a -> a -> Ordering) -> (a, a) -> (a, a)
sortTup3By :: (a -> a -> Ordering) -> (a, a, a) -> (a, a, a)
sortTup4By :: (a -> a -> Ordering) -> (a, a, a, a) -> (a, a, a, a)
...
sortTup16By :: (a -> a -> Ordering) -> (a, a, ..., a) -> (a, a, ..., a)

in which unsafeSortListXBy are partial functions that only accept list of corresponding length (you will get pattern matching failures if that is not the case).

How it works

A sorting network can sort fixed size list of elements.

Observe that the following function sorts a tuple of 3 elements (name shadowing is intentional for demostration):

sortTup3By :: (a -> a -> Ordering) -> (a, a, a) -> (a, a, a)
sortTup3By = \cmp (a, b, c) ->
  let sw = \u v f ->
        if cmp u v == GT then f v u else f u v
   in sw a b \a b ->
        sw a c \a c ->
          sw b c \b c ->
            (a, b, c)

This works because it simulates a sorting network of length 3: if we pretend variable a, b, and c are 3 wires in the network, and sw a b \a b -> {body} applies a comparator on wire a and b, shadowing old variables with now properly ordered a and b in the {body}.

To generalize, if we have n variables and a sorting network known to sort n elements, we can build a sort function of fixed size n by building sorting network with comparators applied layer-by-layer in that order.

This library builds those functions for you, utilizing template haskell.

Acknowledgment

This library takes inspiration from @oisdk's post Sorting Small Things in Haskell.

Optimal networks (by size) are taken from https://bertdobbelaere.github.io/sorting_networks.html.

Also thanks for all the suggestion, support, and criticism from the initial release thread on Reddit.

Metadata

Version

0.2.1.0

License

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows