Home

Awesome

Origin

Octosort is a block merge sort based on WikiSort and quadsort. This document primarily lists notable differences and some benchmarks.

Octo swap

Like quadsort has the quad swap, octosort has the octo swap. The swap sorts between 4 and 8 elements at a time and performs runs on reverse ordered data.

Monobound binary search

WikiSort's binary search has been replaced with a monobound binary search, which is up to two times faster.

Gries-Mills rotation

WikiSort's triple reversal rotation has been replaced with a Gries-Mills rotation, which is up to two times faster.

Quad merge

WikiSort already implemented a quad merge, which has been updated to no longer detect reverse order runs, since that's taken care off by the octo swap.

Tail merge

Quadsort's tail merge routine was added to perform partially in-place merges.

Data Types

Support was added for long doubles and 8, 16, 32, and 64 bit data types. By using 32 or 64 bit pointers it's possible to sort any other data type.

Interface

The interface was changed to use the same one as qsort, which is described in man qsort.

Memory

By default octosort uses 512 elements worth of stack memory.

The minimum memory requirement for octosort is 1 element of stack memory, it can be configured to use n / 2 memory.

Big O

                 ┌───────────────────────┐┌───────────────────────┐
                 │comparisons            ││swap memory            │
┌───────────────┐├───────┬───────┬───────┤├───────┬───────┬───────┤┌──────┐┌─────────┐┌─────────┐
│name           ││min    │avg    │max    ││min    │avg    │max    ││stable││partition││adaptive │
├───────────────┤├───────┼───────┼───────┤├───────┼───────┼───────┤├──────┤├─────────┤├─────────┤
│mergesort      ││n log n│n log n│n log n││n      │n      │n      ││yes   ││no       ││no       │
├───────────────┤├───────┼───────┼───────┤├───────┼───────┼───────┤├──────┤├─────────┤├─────────┤
│octosort       ││n      │n log n│n log n││1      │1      │1      ││yes   ││no       ││yes      │
├───────────────┤├───────┼───────┼───────┤├───────┼───────┼───────┤├──────┤├─────────┤├─────────┤
│quicksort      ││n      │n log n│n²     ││1      │1      │1      ││no    ││yes      ││no       │
└───────────────┘└───────┴───────┴───────┘└───────┴───────┴───────┘└──────┘└─────────┘└─────────┘

Benchmarks

The following benchmark was on WSL 2 gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04). The source code was compiled using gcc -O3 bench.c. Each test was ran 100 times and only the best run is reported. It's generated by running the benchmark using 100000 100 as the argument.

Graph

<details><summary><b>data table</b></summary>
NameItemsTypeBestAverageComparesSamplesDistribution
qsort100000320.0085080.0087791536367100random order
octosort100000320.0087920.0088891800800100random order
qsort100000320.0020240.002225815024100ascending order
octosort100000320.0003280.000345116524100ascending order
qsort100000320.0028310.003088915020100ascending saw
octosort100000320.0015370.001565370372100ascending saw
qsort100000320.0064260.0067221531997100generic order
octosort100000320.0064370.0065151633855100generic order
qsort100000320.0024560.002657853904100descending order
octosort100000320.0002210.00022799999100descending order
qsort100000320.0028320.0030011063907100descending saw
octosort100000320.0017380.001849693171100descending saw
qsort100000320.0037440.0039391012256100random tail
octosort100000320.0026840.002740630603100random tail
qsort100000320.0054640.0057321200738100random half
octosort100000320.0048590.0049111022394100random half
qsort100000320.0041470.0046851209200100ascending tiles
octosort100000320.0031460.003437790377100ascending tiles
</details>

The following benchmark was generated using 1000000 0 0 as the argument.

Graph

<details><summary><b>data table</b></summary>
NameItemsTypeBestAverageComparesSamplesDistribution
qsort4320.0013690.0014395100random 4
octosort4320.0007650.0007766100random 4
qsort8320.0015110.00155517100random 8
octosort8320.0008930.00093919100random 8
qsort16320.0015870.00195246100random 16
octosort16320.0012210.00128155100random 16
qsort32320.0017950.002612121100random 32
octosort32320.0013190.001602124100random 32
qsort64320.0020370.003018309100random 64
octosort64320.0014920.002195319100random 64
qsort128320.0023040.003754745100random 128
octosort128320.0016740.003189775100random 128
qsort256320.0032930.0050241738100random 256
octosort256320.0019090.0036131806100random 256
qsort512320.0052930.0062203968100random 512
octosort512320.0031130.0050864112100random 512
qsort1024320.0065300.0071288962100random 1024
octosort1024320.0052900.00649410031100random 1024
qsort2048320.0073410.00781019962100random 2048
octosort2048320.0069430.00744422885100random 2048
qsort4096320.0080860.00849943966100random 4096
octosort4096320.0082950.00844151035100random 4096
qsort8192320.0087400.00914296149100random 8192
octosort8192320.0091220.009198112238100random 8192
qsort16384320.0094050.009830208702100random 16384
octosort16384320.0098270.009949244511100random 16384
qsort32768320.0100390.010421450105100random 32768
octosort32768320.0105250.010680529041100random 32768
qsort65536320.0107080.011123965773100random 65536
octosort65536320.0112500.0114311138363100random 65536
qsort131072320.0113160.0116982062601100random 131072
octosort131072320.0119820.0121592437514100random 131072
</details>