Skip to content

Commit

Permalink
initial update
Browse files Browse the repository at this point in the history
  • Loading branch information
putianyi889 committed Feb 23, 2024
1 parent 389b8d0 commit 5b961ca
Show file tree
Hide file tree
Showing 171 changed files with 3,930 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/Manifest.toml
test/debug.jl
9 changes: 8 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ uuid = "518c31a9-72dd-41b9-826f-606c4258a582"
authors = ["Tianyi Pu <[email protected]> and contributors"]
version = "1.0.0-DEV"

[deps]
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Scanf = "6ef1bc8b-493b-44e1-8d40-549aa65c4b41"
Unroll = "36e64239-2ab9-4580-84fb-56737a503d36"

[compat]
julia = "1"

[extras]
BeepBeep = "97dd0070-d6c0-43ac-b134-ca84cdbd343e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
test = ["BeepBeep", "Random", "Test"]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# SwapSort

[![Build Status](https://github.com/putianyi889/SwapSort.jl/actions/workflows/CI.yml/badge.svg?branch=master)](https://github.com/putianyi889/SwapSort.jl/actions/workflows/CI.yml?query=branch%3Amaster)

This package is intended to be a successor of [SortingNetworks.jl](https://github.com/JeffreySarnoff/SortingNetworks.jl). This package relies on the existing [sorters](./srv/Sorters) which are currently copied from [SorterHunter](https://github.com/bertdobbelaere/SorterHunter), a C++ project. For a visualization of the algorithms, see [here](https://bertdobbelaere.github.io/sorting_networks.html).
2 changes: 2 additions & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/
site/
2 changes: 2 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
15 changes: 15 additions & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Documenter
using SwapSort

makedocs(
sitename = "SwapSort",
format = Documenter.HTML(),
modules = [SwapSort]
)

# Documenter can also automatically deploy documentation to gh-pages.
# See "Hosting Documentation" and deploydocs() in the Documenter manual
# for more information.
#=deploydocs(
repo = "<repository url>"
)=#
8 changes: 8 additions & 0 deletions docs/src/benchmark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Benchmarks
The benchmarks are generated by the GitHub Actions.
```@setup benchmark1
using InteractiveUtils
```
```@repl benchmark1
versioninfo()
```
5 changes: 5 additions & 0 deletions docs/src/docstrings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Docstrings

```@autodocs
Modules = [SwapSort]
```
31 changes: 31 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# SwapSort.jl

This package is intended to be a successor of [SortingNetworks.jl](https://github.com/JeffreySarnoff/SortingNetworks.jl). This package relies on the existing sorters which are currently copied from [SorterHunter](https://github.com/bertdobbelaere/SorterHunter), a C++ project. For a visualization of the algorithms, see [here](https://bertdobbelaere.github.io/sorting_networks.html).

- **PROS**
- Supports `lt` and `by` keywords.
- Easier to to add new algorithms as all codes are generated.
- Supports sorting across different types. Note that type stability will become a problem in this case.
- **CONS**
- Very long precompilation (~55s on my machine). The current bottleneck is `JSON.parsefile`. The importing is fine (~30ms).
- Harder to troubleshoot since the most important things are written in macros.
- Does not support sorting a vector. The reason is that `Base.sort` is efficient enough for a vector.
- Does not support sorting a tuple at present.

## Internals
There are many internal functions in the form of `swapsortN_L_D(::Vararg{Any,N})`. `N` refers to the data size, `L` refers to the number of comparisons/swaps. Although currently unsupported, it's possible to execute some swaps in parallel and `D` refers to the number of steps when computing in parallel. All available `(N,L,D)` triples are listed at `SwapSort.SORTERS`.
```@repl index1
show(SwapSort.SORTERS)
```

The exported `swapsort` at present chooses the least available `L` for each `N`. For example, `swapsort(a,b) = swapsort2_1_1(a,b)`. The choices are listed at `SwapSort.BESTSIZE`.
```@repl index1
show(SwapSort.BESTSIZE)
```

## Performance
The sorting algorithm is called [sorting network](https://en.wikipedia.org/wiki/Sorting_network). It's not the most efficient algorithm for general purpose, but in Julia, it excels at sorting a few discrete variables, as collecting/splatting is very slow.

`StaticArrays.jl` implements `BitonicSort` which is a special case of sorting network. It is more flexible, but has two drawbacks:
- The import time of `StaticArrays` is 10x slower than `SwapSort`.
- Sorting a few variables needs to pack them into a static vector. The packing in Julia is slow.
17 changes: 17 additions & 0 deletions scripts/consts.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# to generate results to src/consts.jl

using Scanf
list = readdir("./src/Sorters")
sorters = [@scanf(s, "Sort_%d_%d_%d.json", Int, Int, Int)[2:end] for s in list]
sort!(sorters, by=x->x[3])
sort!(sorters, by=x->x[2])
sort!(sorters, by=x->x[1])
print(sorters)

bestsize = copy(sorters)
for i in length(bestsize):-1:2
if bestsize[i][1] == bestsize[i-1][1]
popat!(bestsize, i)
end
end
print(bestsize)
16 changes: 16 additions & 0 deletions src/Sorters/Sort_10_29_8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"N": 10,
"L": 29,
"D": 8,
"symmetric": true,
"nw": [
[0,8], [1,9], [2,7], [3,5], [4,6],
[0,2], [1,4], [5,8], [7,9],
[0,3], [2,4], [5,7], [6,9],
[0,1], [3,6], [8,9],
[1,5], [2,3], [4,8], [6,7],
[1,2], [3,5], [4,6], [7,8],
[2,3], [4,5], [6,7],
[3,4], [5,6]
]
}
15 changes: 15 additions & 0 deletions src/Sorters/Sort_10_31_7.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"N": 10,
"L": 31,
"D": 7,
"symmetric": true,
"nw": [
[0,1], [2,5], [3,6], [4,7], [8,9],
[0,6], [1,8], [2,4], [3,9], [5,7],
[0,2], [1,3], [4,5], [6,8], [7,9],
[0,1], [2,7], [3,5], [4,6], [8,9],
[1,2], [3,4], [5,6], [7,8],
[1,3], [2,4], [5,7], [6,8],
[2,3], [4,5], [6,7]
]
}
16 changes: 16 additions & 0 deletions src/Sorters/Sort_11_35_8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"N": 11,
"L": 35,
"D": 8,
"symmetric": false,
"nw": [
[0,9], [1,6], [2,4], [3,7], [5,8],
[0,1], [3,5], [4,10], [6,9], [7,8],
[1,3], [2,5], [4,7], [8,10],
[0,4], [1,2], [3,7], [5,9], [6,8],
[0,1], [2,6], [4,5], [7,8], [9,10],
[2,4], [3,6], [5,7], [8,9],
[1,2], [3,4], [5,6], [7,8],
[2,3], [4,5], [6,7]
]
}
17 changes: 17 additions & 0 deletions src/Sorters/Sort_12_39_9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"N": 12,
"L": 39,
"D": 9,
"symmetric": true,
"nw": [
[0,8], [1,7], [2,6], [3,11], [4,10], [5,9],
[0,1], [2,5], [3,4], [6,9], [7,8], [10,11],
[0,2], [1,6], [5,10], [9,11],
[0,3], [1,2], [4,6], [5,7], [8,11], [9,10],
[1,4], [3,5], [6,8], [7,10],
[1,3], [2,5], [6,9], [8,10],
[2,3], [4,5], [6,7], [8,9],
[4,6], [5,7],
[3,4], [5,6], [7,8]
]
}
16 changes: 16 additions & 0 deletions src/Sorters/Sort_12_40_8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"N": 12,
"L": 40,
"D": 8,
"symmetric": true,
"nw": [
[0,8], [1,7], [2,6], [3,11], [4,10], [5,9],
[0,2], [1,4], [3,5], [6,8], [7,10], [9,11],
[0,1], [2,9], [4,7], [5,6], [10,11],
[1,3], [2,7], [4,9], [8,10],
[0,1], [2,3], [4,5], [6,7], [8,9], [10,11],
[1,2], [3,5], [6,8], [9,10],
[2,4], [3,6], [5,8], [7,9],
[1,2], [3,4], [5,6], [7,8], [9,10]
]
}
18 changes: 18 additions & 0 deletions src/Sorters/Sort_13_45_10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"N": 13,
"L": 45,
"D": 10,
"symmetric": false,
"nw": [
[0,12], [1,10], [2,9], [3,7], [5,11], [6,8],
[1,6], [2,3], [4,11], [7,9], [8,10],
[0,4], [1,2], [3,6], [7,8], [9,10], [11,12],
[4,6], [5,9], [8,11], [10,12],
[0,5], [3,8], [4,7], [6,11], [9,10],
[0,1], [2,5], [6,9], [7,8], [10,11],
[1,3], [2,4], [5,6], [9,10],
[1,2], [3,4], [5,7], [6,8],
[2,3], [4,5], [6,7], [8,9],
[3,4], [5,6]
]
}
17 changes: 17 additions & 0 deletions src/Sorters/Sort_13_46_9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"N": 13,
"L": 46,
"D": 9,
"symmetric": false,
"nw": [
[0,11], [1,7], [2,4], [3,5], [8,9], [10,12],
[0,2], [3,6], [4,12], [5,7], [8,10],
[0,8], [1,3], [2,5], [4,9], [6,11], [7,12],
[0,1], [2,10], [3,8], [4,6], [9,11],
[1,3], [2,4], [5,10], [6,8], [7,9], [11,12],
[1,2], [3,4], [5,8], [6,9], [7,10],
[2,3], [4,7], [5,6], [8,11], [9,10],
[4,5], [6,7], [8,9], [10,11],
[3,4], [5,6], [7,8], [9,10]
]
}
18 changes: 18 additions & 0 deletions src/Sorters/Sort_14_51_10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"N": 14,
"L": 51,
"D": 10,
"symmetric": true,
"nw": [
[0,1], [2,3], [4,5], [6,7], [8,9], [10,11], [12,13],
[0,2], [1,3], [4,8], [5,9], [10,12], [11,13],
[0,4], [1,2], [3,7], [5,8], [6,10], [9,13], [11,12],
[0,6], [1,5], [3,9], [4,10], [7,13], [8,12],
[2,10], [3,11], [4,6], [7,9],
[1,3], [2,8], [5,11], [6,7], [10,12],
[1,4], [2,6], [3,5], [7,11], [8,10], [9,12],
[2,4], [3,6], [5,8], [7,10], [9,11],
[3,4], [5,6], [7,8], [9,10],
[6,7]
]
}
17 changes: 17 additions & 0 deletions src/Sorters/Sort_14_52_9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"N": 14,
"L": 52,
"D": 9,
"symmetric": true,
"nw": [
[0,1], [2,3], [4,5], [6,7], [8,9], [10,11], [12,13],
[0,2], [1,3], [4,8], [5,9], [10,12], [11,13],
[0,10], [1,6], [2,11], [3,13], [5,8], [7,12],
[1,4], [2,8], [3,6], [5,11], [7,10], [9,12],
[0,1], [3,9], [4,10], [5,7], [6,8], [12,13],
[1,5], [2,4], [3,7], [6,10], [8,12], [9,11],
[1,2], [3,5], [4,6], [7,9], [8,10], [11,12],
[2,3], [4,5], [6,7], [8,9], [10,11],
[3,4], [5,6], [7,8], [9,10]
]
}
18 changes: 18 additions & 0 deletions src/Sorters/Sort_15_56_10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"N": 15,
"L": 56,
"D": 10,
"symmetric": false,
"nw": [
[1,2], [3,10], [4,14], [5,8], [6,13], [7,12], [9,11],
[0,14], [1,5], [2,8], [3,7], [6,9], [10,12], [11,13],
[0,7], [1,6], [2,9], [4,10], [5,11], [8,13], [12,14],
[0,6], [2,4], [3,5], [7,11], [8,10], [9,12], [13,14],
[0,3], [1,2], [4,7], [5,9], [6,8], [10,11], [12,13],
[0,1], [2,3], [4,6], [7,9], [10,12], [11,13],
[1,2], [3,5], [8,10], [11,12],
[3,4], [5,6], [7,8], [9,10],
[2,3], [4,5], [6,7], [8,9], [10,11],
[5,6], [7,8]
]
}
17 changes: 17 additions & 0 deletions src/Sorters/Sort_15_57_9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"N": 15,
"L": 57,
"D": 9,
"symmetric": false,
"nw": [
[0,6], [1,10], [2,14], [3,9], [4,12], [5,13], [7,11],
[0,7], [2,5], [3,4], [6,11], [8,10], [9,12], [13,14],
[1,13], [2,3], [4,6], [5,9], [7,8], [10,14], [11,12],
[0,3], [1,4], [5,7], [6,13], [8,9], [10,11], [12,14],
[0,2], [1,5], [3,8], [4,6], [7,10], [9,11], [12,13],
[0,1], [2,5], [3,10], [4,8], [6,7], [9,12], [11,13],
[1,2], [3,4], [5,6], [7,9], [8,10], [11,12],
[3,5], [4,6], [7,8], [9,10],
[2,3], [4,5], [6,7], [8,9], [10,11]
]
}
18 changes: 18 additions & 0 deletions src/Sorters/Sort_16_60_10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"N": 16,
"L": 60,
"D": 10,
"symmetric": true,
"nw": [
[0,13], [1,12], [2,15], [3,14], [4,8], [5,6], [7,11], [9,10],
[0,5], [1,7], [2,9], [3,4], [6,13], [8,14], [10,15], [11,12],
[0,1], [2,3], [4,5], [6,8], [7,9], [10,11], [12,13], [14,15],
[0,2], [1,3], [4,10], [5,11], [6,7], [8,9], [12,14], [13,15],
[1,2], [3,12], [4,6], [5,7], [8,10], [9,11], [13,14],
[1,4], [2,6], [5,8], [7,10], [9,13], [11,14],
[2,4], [3,6], [9,12], [11,13],
[3,5], [6,8], [7,9], [10,12],
[3,4], [5,6], [7,8], [9,10], [11,12],
[6,7], [8,9]
]
}
17 changes: 17 additions & 0 deletions src/Sorters/Sort_16_61_9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"N": 16,
"L": 61,
"D": 9,
"symmetric": true,
"nw": [
[0,5], [1,4], [2,12], [3,13], [6,7], [8,9], [10,15], [11,14],
[0,2], [1,10], [3,6], [4,7], [5,14], [8,11], [9,12], [13,15],
[0,8], [1,3], [2,11], [4,13], [5,9], [6,10], [7,15], [12,14],
[0,1], [2,4], [3,8], [5,6], [7,12], [9,10], [11,13], [14,15],
[1,3], [2,5], [4,8], [6,9], [7,11], [10,13], [12,14],
[1,2], [3,5], [4,11], [6,8], [7,9], [10,12], [13,14],
[2,3], [4,5], [6,7], [8,9], [10,11], [12,13],
[4,6], [5,7], [8,10], [9,11],
[3,4], [5,6], [7,8], [9,10], [11,12]
]
}
20 changes: 20 additions & 0 deletions src/Sorters/Sort_17_71_12.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"N": 17,
"L": 71,
"D": 12,
"symmetric": false,
"nw": [
[0,11], [1,15], [2,10], [3,5], [4,6], [8,12], [9,16], [13,14],
[0,6], [1,13], [2,8], [4,14], [5,15], [7,11],
[0,8], [3,7], [4,9], [6,16], [10,11], [12,14],
[0,2], [1,4], [5,6], [7,13], [8,9], [10,12], [11,14], [15,16],
[0,3], [2,5], [6,11], [7,10], [9,13], [12,15], [14,16],
[0,1], [3,4], [5,10], [6,9], [7,8], [11,15], [13,14],
[1,2], [3,7], [4,8], [6,12], [11,13], [14,15],
[1,3], [2,7], [4,5], [9,11], [10,12], [13,14],
[2,3], [4,6], [5,7], [8,10],
[3,4], [6,8], [7,9], [10,12],
[5,6], [7,8], [9,10], [11,12],
[4,5], [6,7], [8,9], [10,11], [12,13]
]
}
19 changes: 19 additions & 0 deletions src/Sorters/Sort_17_72_11.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"N": 17,
"L": 72,
"D": 11,
"symmetric": false,
"nw": [
[0,8], [1,3], [2,6], [4,15], [5,13], [9,16], [10,11], [12,14],
[0,2], [1,4], [3,15], [5,8], [6,14], [7,9], [13,16],
[2,13], [3,11], [5,10], [6,9], [7,12], [8,15], [14,16],
[0,7], [1,5], [3,6], [4,10], [8,14], [9,11], [12,13], [15,16],
[0,1], [2,6], [4,12], [5,7], [9,14], [10,13], [11,15],
[1,4], [2,3], [6,11], [7,12], [8,9], [13,14], [15,16],
[2,5], [3,8], [7,10], [9,12], [11,13], [14,15],
[1,2], [3,7], [4,5], [6,9], [8,10], [13,14],
[3,4], [5,7], [6,8], [9,11], [10,12],
[2,4], [5,6], [7,8], [9,10], [11,12],
[2,3], [4,5], [6,7], [8,9], [10,11], [12,13]
]
}
Loading

0 comments on commit 5b961ca

Please sign in to comment.