{"id":22104185,"url":"https://github.com/brianpursley/sorting-network","last_synced_at":"2025-07-25T01:32:01.239Z","repository":{"id":144192724,"uuid":"42361707","full_name":"brianpursley/sorting-network","owner":"brianpursley","description":"Python script to check sorting networks and generate sorting network diagrams","archived":false,"fork":false,"pushed_at":"2024-04-26T00:13:15.000Z","size":72,"stargazers_count":17,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-27T00:41:39.656Z","etag":null,"topics":["comparison-network","python","sorting-algorithms","sorting-network","sorting-network-diagrams"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/brianpursley.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2015-09-12T15:21:19.000Z","updated_at":"2024-04-26T08:46:36.000Z","dependencies_parsed_at":null,"dependency_job_id":"f9280bfd-f433-4996-aab1-d0586a97316f","html_url":"https://github.com/brianpursley/sorting-network","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brianpursley%2Fsorting-network","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brianpursley%2Fsorting-network/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brianpursley%2Fsorting-network/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brianpursley%2Fsorting-network/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brianpursley","download_url":"https://codeload.github.com/brianpursley/sorting-network/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227500691,"owners_count":17781321,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["comparison-network","python","sorting-algorithms","sorting-network","sorting-network-diagrams"],"created_at":"2024-12-01T06:29:21.637Z","updated_at":"2025-07-25T01:32:01.221Z","avatar_url":"https://github.com/brianpursley.png","language":"Python","readme":"# sorting-network\nPython script to check sorting networks and generate sorting network diagrams\n\n## Requirements\n\n* Python 3.10 or later\n\n## Usage\n\n```text\nusage: sortingnetwork.py [-h] [--input [inputfile]] {check,print,sort,svg} ...\n\na tool for working with sorting networks\n\npositional arguments:\n  {check,print,sort,svg}\n    check               check whether it is a sorting network\n    print               print the comparison network definition\n    sort                sort a sequence using the input comparison network\n    svg                 generate an SVG\n\noptions:\n  -h, --help            show this help message and exit\n  --input [inputfile], -i [inputfile]\n                        file containing comparison network definition, or use stdin if not specified\n```\n\nComparison 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.\n\nMultiple 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:\n```text\n0:1,2:3\n0:2,1:3\n1:2\n```\n\n## Check Command\n```text\nusage: sortingnetwork.py check [-h] [--show-progress]\n\ncheck whether it is a sorting network\n\noptions:\n  -h, --help       show this help message and exit\n  --show-progress  show percent complete while checking\n```\n\n* If it is a sorting network, the output will be `It is a sorting network!` and the exit code will be 0.   \n* If it is not a sorting network, the output will be `It is not a sorting network.` and the exit code will be 1.\n\nYou can use the `--show-progress` option to see the percent complete while it is checking.\n\nFor better performance while checking large sorting networks, the use of [pypy](https://github.com/pypy/pypy?tab=readme-ov-file) is recommended.\n\n### Example: Check a comparison network from a file\n```shell\n./sortingnetwork.py --input example.cn check\n```\n\n### Example: Check a comparison network from stdin\n```shell\necho \"0:1,2:3,0:2,1:3,1:2\" | ./sortingnetwork.py check\n```\n\n## Print Command\n```text\nusage: sortingnetwork.py print [-h] [filename]\n\nprint the comparison network definition\n\npositional arguments:\n  filename    the file to save the output to\n\noptions:\n  -h, --help  show this help message and exit\n```    \n\n* If a filename is not specified, the comparison network will be output to stdout.\n\n### Example: Print the comparison network to a file\n```shell\n./sortingnetwork.py --input example.cn svg example.svg\n```\n\n### Example: Print the comparison network to stdout\n```shell\n./sortingnetwork.py --input example.cn svg\n```\n\n## Sort Command\n```text\nusage: sortingnetwork.py sort [-h] [sequence]\n\nsort a sequence using the input comparison network\n\npositional arguments:\n  sequence    the sequence to sort, e.g. '3,1,2'\n\noptions:\n  -h, --help  show this help message and exit\n```\n\n* 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.\n* The number of items in the sequence must match the number of inputs in the comparison network.\n\n### Example: Sort a sequence\n```shell\n./sortingnetwork.py --input example.cn sort 3,1,2\n```\n\n## SVG Command\n```text\nusage: sortingnetwork.py svg [-h] [filename]\n\ngenerate an SVG\n\npositional arguments:\n  filename    the file to save the SVG to\n\noptions:\n  -h, --help  show this help message and exit\n```\n\n* If a filename is not specified, the SVG will be output to stdout.\n\n### Example: Generate an SVG to a file\n```shell\n./sortingnetwork.py --input example.cn svg example.svg\n```\n\n### Example: Generate an SVG to stdout\n```shell\n./sortingnetwork.py --input example.cn svg\n```\n\n### Example pipe the output through rsvg-convert to generate a PNG (or other format) instead of SVG.\n(*rsvg-convert can be installed by using `sudo apt-get install librsvg2-bin` on Ubuntu.*)\n\n```shell    \n./sortingnetwork.py --input example.cn svg | rsvg-convert \u003e examples/4-input.png\n```\n\n## Example sorting networks\n\n### 4-Input\n```text\n0:1,2:3\n0:2,1:3\n1:2\n```\n\n![4-Input Sorting Network](examples/4-input.png)\n\n### 5-Input\n```text\n0:1,3:4\n2:4\n2:3,1:4\n0:3\n0:2,1:3\n1:2\n```\n\n![5-Input Sorting Network](examples/5-input.png)\n\n### 8-Input Bitonic\n```text\n0:1,2:3,4:5,6:7\n0:3,1:2,4:7,5:6\n0:1,2:3,4:5,6:7\n0:7,1:6,2:5,3:4\n0:2,1:3,4:6,5:7\n0:1,2:3,4:5,6:7\n```\n![8-Input Bitonic Sorting Network](examples/8-input-bitonic.png)\n\n### 16-Input\n```text\n0:1,2:3,4:5,6:7,8:9,10:11,12:13,14:15\n0:2,1:3,4:6,5:7,8:10,9:11,12:14,13:15\n0:4,1:5,2:6,3:7,8:12,9:13,10:14,11:15\n0:8,1:9,2:10,3:11,4:12,5:13,6:14,7:15\n5:10,6:9,3:12,13:14,7:11,1:2,4:8\n1:4,7:13,2:8,11:14\n2:4,5:6,9:10,11:13,3:8,7:12\n6:8,10:12,3:5,7:9\n3:4,5:6,7:8,9:10,11:12\n6:7,8:9\n```\n\n![16-Input Sorting Network](examples/16-input.png)\n\nSee the [examples](examples) directory for more examples.\n\nThe examples provided are not necessarily the most efficient sorting networks for the given number of inputs. They are provided for illustrative purposes and testing.\n\n## Contributors\n\n* [brianpursley](https://github.com/brianpursley) - Original author / maintainer\n* [mizar](https://github.com/mizar) - Various improvements, including an optimized `is_sorting_network()` function using a [three-valued-logic DFS approach](https://github.com/brianpursley/sorting-network/pull/9).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrianpursley%2Fsorting-network","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrianpursley%2Fsorting-network","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrianpursley%2Fsorting-network/lists"}