orst

package module
v0.0.0-...-2833060 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2022 License: MIT Imports: 3 Imported by: 0

README

orst

This is a package that has different sorting algorithms that can be applied to generic slices or used with your own comparator function.

Usage example

With a normal slice
slice := []int{3,2,1}  // slice = (3,2,1)
// with default comparator
orst.Sort(slice, orst.BubbleSort, nil) // slice = (1,2,3)
// or with custom comparator
orst.Sort(slice, orst.BubbleSort, func(i,j *int) bool { return *j < *i }) // slice = (3,2,1)
With a custom type
type example struct { val int }
slice := []example{{3},{2},{1}} // slice = (3,2,1)
// only with custom comparator
orst.SortAny(slice, orst.BubbleSort, func(i,j *example) bool { return i.val < j.val }) // slice = (1,2,3)

Currently implemented sorting algorithms

  1. Bubblesort
  2. Insertion sort
  3. Quicksort
  4. Selection sort

Documentation

Overview

Package orst is a package that has different sorting methods that can be applied to generic slices or with your own comparator function

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Sort

func Sort[T constraints.Ordered](s []T, algorithm Kind, cmp sorter.Comparator[T])

Sort sorts the given slice (by mutating it) with the specified Sorter. If Comparator[T] is nil, sorts in ascending order using the inbuilt comparison operator (<).

Example

slice := []int{3,2,1} // slice = (3,2,1)

with defalt comparator

orst.Sort(slice, orst.BubbleSort, nil) // slice = (1,2,3)

or with custom comparator

orst.Sort(slice, orst.BubbleSort, func(i,j *int) bool { return *j < *i }) // slice = (3,2,1)

func SortAny

func SortAny[T any](s []T, algorithm Kind, cmp sorter.Comparator[T])

Sort sorts the given slice (by mutating it) with the specified Sorter. If Comparator[T] is nil, the function immediately returns without any changes to the slice.

Example

type example struct { val int }
slice := []example{{3},{2},{1}} // slice = (3,2,1)

only with custom comparator

orst.SortAny(slice, orst.BubbleSort, func(i,j *example) bool { return i.val < j.val }) // slice = (1,2,3)

Types

type Kind

type Kind uint8

Describes which sorting algorithm to use.

const (
	BubbleSort Kind
	InsertionSort
	SelectionSort
	QuickSort
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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