~mrms/sortum

dca195eda49ebbb49aa260c8d3252f560f09a6cb — Marek Ma┼íkarinec 7 months ago
Init commit
4 files changed, 114 insertions(+), 0 deletions(-)

A .build.yml
A README.md
A pak.json
A sort.um
A  => .build.yml +27 -0
@@ 1,27 @@
image: debian/bullseye
packages:
- python3
- python3-pip
sources:
- https://git.sr.ht/~mrms/sortum
- https://git.sr.ht/~mrms/pak
environment:
  project: sort
secrets:
- df5c0de2-c685-40dd-aa0a-368994b3c32b
shell: false
tasks:
- setup: |
    cd pak
    python3 -m pip install -r requirements.txt
    ./setup.py install --user
- build: |
    cd sortum
    python3 -m pak update
    python3 -m pak build
- deploy: |
    cd sortum
    set +x
    python3 -m pak upload -t `cat ../.secret` pak.tar
    python3 -m pak upload -t `cat ../.secret` pak.json
    set -x

A  => README.md +7 -0
@@ 1,7 @@
# sort

Generic array sort implementation

## license

Unlicense/MIT

A  => pak.json +13 -0
@@ 1,13 @@
{
    "name": "sort",
    "version": "v0.1.0",
    "author": "Marek Maškarinec <marek@mrms.cz>",
    "license": "Unlicense/MIT",
    "description": "Generic array sort implementation",
    "readme": "README.md",
    "link": "",
    "dependencies": [],
    "include": ["sort.um"],
    "run_posix": "./pak/umka/linux/umka sort.um",
    "run_windows": ".\\pak\\umka\\windows\\umka.exe sort.um"
}

A  => sort.um +67 -0
@@ 1,67 @@

//~~
type Arr* = interface {
	len(): int
	swap(i, j: int)
}

type CmpFn* = fn(a: Arr, i, j: int): bool
//~~

fn partition(a: Arr, f: CmpFn, s, e: int): int {
	i := s - 1
	for j:=s; j < e; j++ {
		if !f(a, j, e - 1) { continue }

		i++
		a.swap(i, j)
	}

	i++
	a.swap(i, e - 1)

	return i
}

fn quicksort(a: Arr, f: CmpFn, s, e: int) {
	if s >= e || s < 0 { return }

	pivot := partition(a, f, s, e)

	quicksort(a, f, s, pivot)
	quicksort(a, f, pivot + 1, e)
}

//~~fn sort
fn sort*(a: Arr, f: CmpFn) {
//~~
	quicksort(a, f, 0, a.len())
}

//~~
type IntArr* = []int

fn (this: ^IntArr) len(): int {
	return len(this^)
}

fn (this: ^IntArr) swap(i, j: int) {
	t := this[i]
	this[i] = this[j]
	this[j] = t
}

fn sortInt*(a: []int) {
	sort(IntArr(a), CmpFn{
		ia := []int(a)
		return ia[i] < ia[j]
	})
}

fn main() {
	nums := []int{ 3, 1, 4, 1, 5, 9, 2 }
	printf("nums: %s\n", repr(nums))
	sortInt(nums)
	printf("nums: %s\n", repr(nums))
}
//~~