ordinex

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2026 License: MIT Imports: 8 Imported by: 0

README

ordinex

n. divination by drawing lots. Also: making your slices behave.

One interface. No sacrifices required.

Go Reference CI Go 1.25 MIT License

Installation

go get github.com/danielriddell21/ordinex@latest

Quick start

import "github.com/danielriddell21/ordinex"

s := ordinex.MergeSorter{}
sorted := s.Sort([]int{5, 3, 1, 4, 2})
// sorted == [1 2 3 4 5], original is unchanged
fmt.Println(s.Name()) // "Merge Sort"

Every sorter satisfies the Sorter interface:

type Sorter interface {
    Sort(input []int) []int
    Name() string
}

Sort returns a sorted copy — the original slice is never modified. ThanosSorter and StalinSorter may return a shorter slice than the input.

Algorithms

Sorter Time (avg) Time (worst) Space Notes
BubbleSorter O(n²) O(n²) O(1) Early exit if no swaps
InsertionSorter O(n²) O(n²) O(1)
SelectionSorter O(n²) O(n²) O(1)
CocktailShakerSorter O(n²) O(n²) O(1) Bidirectional bubble sort
GnomeSorter O(n²) O(n²) O(1)
ShellSorter O(n²) O(n²) O(1) Gap sequence: n/2 halved each step
QuickSorter O(n log n) O(n²) O(log n) Lomuto partition, last-element pivot
MergeSorter O(n log n) O(n log n) O(n)
HeapSorter O(n log n) O(n log n) O(1) Max-heap
CountingSorter O(n+k) O(n+k) O(k) k = value range; supports negatives
RadixSorter O(d·(n+k)) O(d·(n+k)) O(n+k) LSD base-10; supports negatives
BucketSorter O(n+k) O(n²) O(n+k) Buckets sorted with insertion sort
PancakeSorter O(n²) O(n²) O(1) Sorts by reversing prefixes
BogoSorter O(n·n!) O(∞) O(1) Shuffle until sorted
SleepSorter O(max) O(max) O(n) Goroutine per element
MiracleSorter O(∞) O(∞) O(1) Waits for a cosmic ray
ThanosSorter O(n) O(n) O(n) Eliminates half until sorted
VibeSorter O($) O(☁) O(☁) Sends the array to an LLM. Prays.
StalinSorter O(n) O(n) O(n) Removes any element smaller than the running maximum

Configurable sorters

Most sorters are empty structs and need no configuration. A few have fields:

BogoSorter
s := ordinex.BogoSorter{
    MaxAttempts: 1000,                        // 0 = unlimited
    Rand:        rand.New(rand.NewPCG(42, 0)), // nil = seeded from time
}
SleepSorter
s := ordinex.SleepSorter{
    ScaleFactor: 10 * time.Millisecond, // sleep per unit of value; default 1ms
}
MiracleSorter
s := ordinex.MiracleSorter{
    MaxChecks: 1000, // 0 = no limit
}
VibeSorter
s := ordinex.VibeSorter{
    APIKey: "sk-...",     // if empty, uses OPENAI_API_KEY env var
    Model:  "gpt-4o",    // if empty, defaults to "gpt-4o-mini"
}
ThanosSorter
s := ordinex.ThanosSorter{
    Rand: rand.New(rand.NewPCG(42, 0)), // nil = seeded from time
}

Examples

Runnable examples for every algorithm are in the examples/ directory.

Docs

Documentation

Overview

Package ordinex provides a collection of sorting algorithm implementations.

All implementations satisfy the Sorter interface. Most return a slice of the same length as the input. ThanosSorter and StalinSorter are exceptions — they may return a shorter slice as elements are eliminated during sorting.

Example

Every sorter satisfies the Sorter interface, so algorithms are interchangeable.

package main

import (
	"fmt"
	"math/rand/v2"

	"github.com/danielriddell21/ordinex"
)

func copyForTest(s []int) []int {
	out := make([]int, len(s))
	copy(out, s)
	return out
}

func isSortedTest(s []int) bool {
	for i := 1; i < len(s); i++ {
		if s[i] < s[i-1] {
			return false
		}
	}
	return true
}

func randomSlice(n int) []int {
	s := make([]int, n)
	r := rand.New(rand.NewPCG(42, 0))
	for i := range s {
		s[i] = r.IntN(10000) - 5000
	}
	return s
}

// Every sorter satisfies the Sorter interface, so algorithms are interchangeable.
func main() {
	sorters := []ordinex.Sorter{
		ordinex.QuickSorter{},
		ordinex.MergeSorter{},
		ordinex.HeapSorter{},
	}
	input := []int{5, 3, 1, 4, 2}
	for _, s := range sorters {
		fmt.Printf("%s: %v\n", s.Name(), s.Sort(input))
	}
}
Output:
Quick Sort: [1 2 3 4 5]
Merge Sort: [1 2 3 4 5]
Heap Sort: [1 2 3 4 5]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BogoSorter

type BogoSorter struct {
	MaxAttempts int
	Rand        *rand.Rand
}

BogoSorter implements Bogo Sort (also known as Permutation Sort or Stupid Sort). Repeatedly shuffles the slice at random until it happens to be sorted. This is highly inefficient and is included for educational/humour purposes only.

MaxAttempts caps the number of shuffle attempts (0 = unlimited, dangerous). Rand is the random source; if nil, one is seeded from the current time. Time: O(n × n!) Space: O(1)

func (BogoSorter) Name

func (b BogoSorter) Name() string

func (BogoSorter) Sort

func (b BogoSorter) Sort(input []int) []int

type BubbleSorter

type BubbleSorter struct{}

BubbleSorter implements Bubble Sort. Repeatedly swaps adjacent elements that are out of order. Stops early if no swaps occur in a full pass. Time: O(n²) Space: O(1)

Example
package main

import (
	"fmt"

	"github.com/danielriddell21/ordinex"
)

func main() {
	s := ordinex.BubbleSorter{}
	fmt.Println(s.Sort([]int{5, 3, 1, 4, 2}))
}
Output:
[1 2 3 4 5]

func (BubbleSorter) Name

func (BubbleSorter) Name() string

func (BubbleSorter) Sort

func (BubbleSorter) Sort(input []int) []int

type BucketSorter

type BucketSorter struct{}

BucketSorter implements Bucket Sort. Distributes elements into buckets based on value range, sorts each bucket with insertion sort, then concatenates the results. Time: O(n+k) Space: O(n+k)

func (BucketSorter) Name

func (BucketSorter) Name() string

func (BucketSorter) Sort

func (BucketSorter) Sort(input []int) []int

type CocktailShakerSorter

type CocktailShakerSorter struct{}

CocktailShakerSorter implements Cocktail Shaker Sort (bidirectional Bubble Sort). Each pass alternates direction, shrinking the unsorted region from both ends. Time: O(n²) Space: O(1)

func (CocktailShakerSorter) Name

func (CocktailShakerSorter) Sort

func (CocktailShakerSorter) Sort(input []int) []int

type CountingSorter

type CountingSorter struct{}

CountingSorter implements Counting Sort. Counts the frequency of each value and reconstructs the sorted slice. Supports negative integers via a min-value offset. Time: O(n+k) Space: O(k) where k = max-min+1

Example
package main

import (
	"fmt"

	"github.com/danielriddell21/ordinex"
)

func main() {
	s := ordinex.CountingSorter{}
	fmt.Println(s.Sort([]int{3, 1, 4, 1, 5, 9, 2, 6}))
}
Output:
[1 1 2 3 4 5 6 9]

func (CountingSorter) Name

func (CountingSorter) Name() string

func (CountingSorter) Sort

func (CountingSorter) Sort(input []int) []int

type GnomeSorter

type GnomeSorter struct{}

GnomeSorter implements Gnome Sort (also known as Stupid Sort). Moves each element to its correct position by a series of adjacent swaps, similar to a garden gnome rearranging flower pots. Time: O(n²) Space: O(1)

func (GnomeSorter) Name

func (GnomeSorter) Name() string

func (GnomeSorter) Sort

func (GnomeSorter) Sort(input []int) []int

type HeapSorter

type HeapSorter struct{}

HeapSorter implements Heap Sort. Builds a max-heap, then repeatedly extracts the root to produce a sorted slice. Time: O(n log n) Space: O(1)

Example
package main

import (
	"fmt"

	"github.com/danielriddell21/ordinex"
)

func main() {
	s := ordinex.HeapSorter{}
	fmt.Println(s.Sort([]int{5, 3, 1, 4, 2}))
}
Output:
[1 2 3 4 5]

func (HeapSorter) Name

func (HeapSorter) Name() string

func (HeapSorter) Sort

func (HeapSorter) Sort(input []int) []int

type InsertionSorter

type InsertionSorter struct{}

InsertionSorter implements Insertion Sort. Picks each element and inserts it into its correct position in the sorted portion. Time: O(n²) Space: O(1)

Example
package main

import (
	"fmt"

	"github.com/danielriddell21/ordinex"
)

func main() {
	s := ordinex.InsertionSorter{}
	fmt.Println(s.Sort([]int{5, 3, 1, 4, 2}))
}
Output:
[1 2 3 4 5]

func (InsertionSorter) Name

func (InsertionSorter) Name() string

func (InsertionSorter) Sort

func (InsertionSorter) Sort(input []int) []int

type MergeSorter

type MergeSorter struct{}

MergeSorter implements Merge Sort. Recursively divides the slice in half, sorts each half, then merges them. Time: O(n log n) Space: O(n)

Example
package main

import (
	"fmt"

	"github.com/danielriddell21/ordinex"
)

func main() {
	s := ordinex.MergeSorter{}
	fmt.Println(s.Sort([]int{5, 3, 1, 4, 2}))
}
Output:
[1 2 3 4 5]

func (MergeSorter) Name

func (MergeSorter) Name() string

func (MergeSorter) Sort

func (MergeSorter) Sort(input []int) []int

type MiracleSorter

type MiracleSorter struct {
	MaxChecks int // 0 = no limit
}

MiracleSorter implements Miracle Sort. Checks if the slice is sorted. If not, it waits for a cosmic ray to flip a bit in memory and sorts the array. Repeats until a miracle happens.

Time: O(∞) Space: O(1)

Example

MiracleSorter returns immediately when the input is already sorted.

package main

import (
	"fmt"

	"github.com/danielriddell21/ordinex"
)

func main() {
	s := ordinex.MiracleSorter{}
	fmt.Println(s.Sort([]int{1, 2, 3, 4, 5}))
}
Output:
[1 2 3 4 5]

func (MiracleSorter) Name

func (MiracleSorter) Name() string

func (MiracleSorter) Sort

func (m MiracleSorter) Sort(input []int) []int

type PancakeSorter

type PancakeSorter struct{}

PancakeSorter implements Pancake Sort. Repeatedly finds the maximum element and uses flips to move it to its correct position, similar to sorting pancakes with a spatula. Time: O(n²) Space: O(1)

func (PancakeSorter) Name

func (PancakeSorter) Name() string

func (PancakeSorter) Sort

func (PancakeSorter) Sort(input []int) []int

type QuickSorter

type QuickSorter struct{}

QuickSorter implements Quick Sort using the Lomuto partition scheme. Selects the last element as pivot and partitions around it. Time: O(n log n) average, O(n²) worst Space: O(log n)

Example
package main

import (
	"fmt"

	"github.com/danielriddell21/ordinex"
)

func main() {
	s := ordinex.QuickSorter{}
	fmt.Println(s.Sort([]int{5, 3, 1, 4, 2}))
}
Output:
[1 2 3 4 5]

func (QuickSorter) Name

func (QuickSorter) Name() string

func (QuickSorter) Sort

func (QuickSorter) Sort(input []int) []int

type RadixSorter

type RadixSorter struct{}

RadixSorter implements Radix Sort (LSD, base-10). Sorts numbers by processing digits from least to most significant. Handles negative integers by sorting negatives and non-negatives separately. Time: O(d*(n+k)) Space: O(n+k) where d = digits, k = 10

Example
package main

import (
	"fmt"

	"github.com/danielriddell21/ordinex"
)

func main() {
	s := ordinex.RadixSorter{}
	fmt.Println(s.Sort([]int{170, 45, 75, 90, 802, 24, 2, 66}))
}
Output:
[2 24 45 66 75 90 170 802]

func (RadixSorter) Name

func (RadixSorter) Name() string

func (RadixSorter) Sort

func (RadixSorter) Sort(input []int) []int

type SelectionSorter

type SelectionSorter struct{}

SelectionSorter implements Selection Sort. Repeatedly finds the minimum element in the unsorted portion and moves it to the front. Time: O(n²) Space: O(1)

func (SelectionSorter) Name

func (SelectionSorter) Name() string

func (SelectionSorter) Sort

func (SelectionSorter) Sort(input []int) []int

type ShellSorter

type ShellSorter struct{}

ShellSorter implements Shell Sort. An optimisation of Insertion Sort that exchanges far-apart elements first. Uses a gap sequence starting at n/2, halved each iteration. Time: O(n²) Space: O(1)

func (ShellSorter) Name

func (ShellSorter) Name() string

func (ShellSorter) Sort

func (ShellSorter) Sort(input []int) []int

type SleepSorter

type SleepSorter struct {
	ScaleFactor time.Duration
}

SleepSorter implements Sleep Sort. Launches a goroutine per element; each goroutine sleeps for a duration proportional to its value, then appends to the result. Elements with smaller values wake earlier and appear first.

ScaleFactor controls how long each unit of value sleeps. Defaults to 1ms. Only works correctly with non-negative integer inputs. Time: O(max(input)) Space: O(n)

func (SleepSorter) Name

func (s SleepSorter) Name() string

func (SleepSorter) Sort

func (s SleepSorter) Sort(input []int) []int

type Sorter

type Sorter interface {
	// Sort returns a sorted copy of input. The original slice is never modified.
	// Most implementations preserve all elements; ThanosSorter and StalinSorter
	// may return fewer.
	Sort(input []int) []int

	// Name returns the human-readable name of the algorithm.
	Name() string
}

Sorter is implemented by every sorting algorithm in this package.

type StalinSorter

type StalinSorter struct{}

StalinSorter implements Stalin Sort. Scans the slice once and keeps only elements that are greater than or equal to the current maximum. Any element smaller than the running maximum is removed from the dataset. Permanently. No appeal process.

The returned slice is guaranteed to be sorted, but may be shorter than the input.

Time: O(n) Space: O(n)

Example

StalinSorter removes any element smaller than the running maximum. The returned slice is sorted but may be shorter than the input.

package main

import (
	"fmt"

	"github.com/danielriddell21/ordinex"
)

func main() {
	s := ordinex.StalinSorter{}
	fmt.Println(s.Sort([]int{3, 1, 4, 1, 5, 9, 2, 6}))
}
Output:
[3 4 5 9]

func (StalinSorter) Name

func (StalinSorter) Name() string

func (StalinSorter) Sort

func (StalinSorter) Sort(input []int) []int

type ThanosSorter

type ThanosSorter struct {
	Rand *rand.Rand
}

ThanosSorter implements Thanos Sort. Checks if the slice is sorted. If not, it randomly eliminates half the elements and checks again. Repeats until the remaining elements happen to be sorted. The returned slice may be shorter than the input.

Rand is the random source; if nil, one is seeded from the current time.

func (ThanosSorter) Name

func (t ThanosSorter) Name() string

func (ThanosSorter) Sort

func (t ThanosSorter) Sort(input []int) []int

type VibeSorter

type VibeSorter struct {
	APIKey string // if empty, uses OPENAI_API_KEY env var
	Model  string // if empty, defaults to "gpt-4o-mini"
}

VibeSorter implements Vibe Sort. Sends the slice to a Large Language Model and prays it doesn't hallucinate the output. If the API call fails or the model returns nonsense, the original slice is returned unsorted.

Time: O($) Space: O(☁)

func (VibeSorter) Name

func (VibeSorter) Name() string

func (VibeSorter) Sort

func (v VibeSorter) Sort(input []int) []int

Directories

Path Synopsis
examples
bubble_leaderboard command
Package main sorts a small game leaderboard using Bubble Sort.
Package main sorts a small game leaderboard using Bubble Sort.
bucket_exam_scores command
Package main grades a class of exam results using Bucket Sort.
Package main grades a class of exam results using Bucket Sort.
cocktail_network_latency command
Package main sorts network round-trip times using Cocktail Shaker Sort.
Package main sorts network round-trip times using Cocktail Shaker Sort.
concurrent_temporal_sort command
Package main demonstrates SleepSort as a high-throughput concurrent sorting solution built on Go's goroutine scheduler.
Package main demonstrates SleepSort as a high-throughput concurrent sorting solution built on Go's goroutine scheduler.
counting_vote_tally command
Package main tallies a survey using Counting Sort.
Package main tallies a survey using Counting Sort.
enterprise_data_pipeline command
Package main demonstrates the ThanosSort algorithm in a production data pipeline context.
Package main demonstrates the ThanosSort algorithm in a production data pipeline context.
gnome_playlist command
Package main sorts a music playlist by track duration using Gnome Sort.
Package main sorts a music playlist by track duration using Gnome Sort.
heap_triage command
Package main demonstrates Heap Sort applied to A&E triage prioritisation.
Package main demonstrates Heap Sort applied to A&E triage prioritisation.
insertion_card_hand command
Package main sorts a poker hand using Insertion Sort.
Package main sorts a poker hand using Insertion Sort.
merge_sales_reports command
Package main combines two pre-sorted regional sales reports using Merge Sort.
Package main combines two pre-sorted regional sales reports using Merge Sort.
non_conformance_elimination command
Package main demonstrates StalinSorter as an enterprise-grade zero-tolerance data quality enforcement pipeline for non-conformance elimination.
Package main demonstrates StalinSorter as an enterprise-grade zero-tolerance data quality enforcement pipeline for non-conformance elimination.
pancake_stack command
Package main sorts a stack of pancakes by diameter using Pancake Sort.
Package main sorts a stack of pancakes by diameter using Pancake Sort.
passive_resilience_framework command
Package main demonstrates MiracleSorter as a zero-compute passive resilience framework for ambient data ordering.
Package main demonstrates MiracleSorter as a zero-compute passive resilience framework for ambient data ordering.
quick_product_catalogue command
Package main sorts a product catalogue by price using Quick Sort.
Package main sorts a product catalogue by price using Quick Sort.
radix_employee_ids command
Package main sorts employee IDs using Radix Sort.
Package main sorts employee IDs using Radix Sort.
selection_bargain_finder command
Package main uses Selection Sort to find the cheapest items in a basket.
Package main uses Selection Sort to find the cheapest items in a basket.
shell_log_sorter command
Package main sorts application log entries by timestamp using Shell Sort.
Package main sorts application log entries by timestamp using Shell Sort.
sort_as_a_service command
Package main demonstrates VibeSorter as an enterprise-grade Sort-as-a-Service (LSaaS) platform for intelligent, AI-augmented data sequencing.
Package main demonstrates VibeSorter as an enterprise-grade Sort-as-a-Service (LSaaS) platform for intelligent, AI-augmented data sequencing.
stochastic_optimizer command
Package main demonstrates BogoSort as an enterprise stochastic optimisation framework for financial data processing pipelines.
Package main demonstrates BogoSort as an enterprise stochastic optimisation framework for financial data processing pipelines.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL