- Notifications
You must be signed in to change notification settings - Fork5
Python script to check sorting networks and generate sorting network diagrams
License
brianpursley/sorting-network
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Python script to check sorting networks and generate sorting network diagrams
- Python 3.10 or later
usage: sortingnetwork.py [-h] [--input [inputfile]] {check,print,sort,svg} ...a tool for working with sorting networkspositional arguments: {check,print,sort,svg} check check whether it is a sorting network print print the comparison network definition sort sort a sequence using the input comparison network svg generate an SVGoptions: -h, --help show this help message and exit --input [inputfile], -i [inputfile] file containing comparison network definition, or use stdin if not specifiedComparison networks can be specified like this:0:1,2:3,0:2,1:3,1:2 and can either be loaded from a file using the--input argument or if no input file is specified, read from stdin.
Multiple lines can be used as well, to logically group the comparators at each depth.0:1,2:3,0:2,1:3,1:2 is the same as this:
0:1,2:30:2,1:31:2usage: sortingnetwork.py check [-h] [--show-progress]check whether it is a sorting networkoptions: -h, --help show this help message and exit --show-progress show percent complete while checking- If it is a sorting network, the output will be
It is a sorting network!and the exit code will be 0. - If it is not a sorting network, the output will be
It is not a sorting network.and the exit code will be 1.
You can use the--show-progress option to see the percent complete while it is checking.
For better performance while checking large sorting networks, the use ofpypy is recommended.
./sortingnetwork.py --input example.cn check
echo"0:1,2:3,0:2,1:3,1:2"| ./sortingnetwork.py check
usage: sortingnetwork.py print [-h] [filename]print the comparison network definitionpositional arguments: filename the file to save the output tooptions: -h, --help show this help message and exit- If a filename is not specified, the comparison network will be output to stdout.
./sortingnetwork.py --input example.cn svg example.svg
./sortingnetwork.py --input example.cn svg
usage: sortingnetwork.py sort [-h] [sequence]sort a sequence using the input comparison networkpositional arguments: sequence the sequence to sort, e.g. '3,1,2'options: -h, --help show this help message and exit- The sequence can be a list of any types that can be compared, such as integers or strings, as long as it is parse-able.
- The number of items in the sequence must match the number of inputs in the comparison network.
./sortingnetwork.py --input example.cn sort 3,1,2
usage: sortingnetwork.py svg [-h] [filename]generate an SVGpositional arguments: filename the file to save the SVG tooptions: -h, --help show this help message and exit- If a filename is not specified, the SVG will be output to stdout.
./sortingnetwork.py --input example.cn svg example.svg
./sortingnetwork.py --input example.cn svg
(rsvg-convert can be installed by usingsudo apt-get install librsvg2-bin on Ubuntu.)
./sortingnetwork.py --input example.cn svg| rsvg-convert> examples/4-input.png
0:1,2:30:2,1:31:20:1,3:42:42:3,1:40:30:2,1:31:20:1,2:3,4:5,6:70:3,1:2,4:7,5:60:1,2:3,4:5,6:70:7,1:6,2:5,3:40:2,1:3,4:6,5:70:1,2:3,4:5,6:70:1,2:3,4:5,6:7,8:9,10:11,12:13,14:150:2,1:3,4:6,5:7,8:10,9:11,12:14,13:150:4,1:5,2:6,3:7,8:12,9:13,10:14,11:150:8,1:9,2:10,3:11,4:12,5:13,6:14,7:155:10,6:9,3:12,13:14,7:11,1:2,4:81:4,7:13,2:8,11:142:4,5:6,9:10,11:13,3:8,7:126:8,10:12,3:5,7:93:4,5:6,7:8,9:10,11:126:7,8:9See theexamples directory for more examples.
The examples provided are not necessarily the most efficient sorting networks for the given number of inputs. They are provided for illustrative purposes and testing.
- brianpursley - Original author / maintainer
- mizar - Various improvements, including an optimized
is_sorting_network()function using athree-valued-logic DFS approach.
About
Python script to check sorting networks and generate sorting network diagrams
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Contributors2
Uh oh!
There was an error while loading.Please reload this page.



