https://github.com/valentinarho/go-dutch-flag
Simple implementation of the Dutch national flag problem in Golang
https://github.com/valentinarho/go-dutch-flag
dutch-flag golang partitioning partitioning-algorithms
Last synced: 5 months ago
JSON representation
Simple implementation of the Dutch national flag problem in Golang
- Host: GitHub
- URL: https://github.com/valentinarho/go-dutch-flag
- Owner: valentinarho
- License: gpl-3.0
- Created: 2017-07-30T15:09:05.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-07-31T07:05:58.000Z (over 8 years ago)
- Last Synced: 2025-03-03T00:44:30.812Z (9 months ago)
- Topics: dutch-flag, golang, partitioning, partitioning-algorithms
- Language: Go
- Size: 16.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-dutch-flag
Simple implementation of the Dutch national flag problem in Golang.
## The problem
_source: [Wikipedia](https://en.wikipedia.org/wiki/Dutch_national_flag_problem)_
The Dutch national flag problem is a computer science programming problem proposed by Edsger Dijkstra. The flag of the Netherlands consists of three colors: red, white and blue. Given balls of these three colors arranged randomly in a line (the actual number of balls does not matter), the task is to arrange them such that all balls of the same color are together and their collective color groups are in the correct order.
This problem can also be viewed in terms of rearranging elements of an array. Suppose each of the possible elements could be classified into exactly one of three categories (bottom, middle, and top). The problem is then to produce an array such that all "bottom" elements come before (have an index less than the index of) all "middle" elements, which come before all "top" elements.
## Usage
Run `main.go` for a usage example.
DutchFlag function takes in input the array to sort and the partitioning function.
This function should be a function from int to int and return, given an integer item
the corresponding partition (-1, 0, 1).
The first example in `main.go` uses the builtin partitioning function that works on
arrays containing only elements in {1,2,3}.
The second example implements a custom partitioning function that split an array in
even, zeros and odd items.
It is possible to tailor the code in order to handle different data types in the array.
It is only necessary to modify in `dutch.go`:
* line 5: the type of the `elem` parameter in partitioningFunction
* line 8: the type of the `a` parameter
* line 19: the type of the `a` parameter
And then, create your `main.go` and your partitioning function!