sort

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 24, 2023 License: MIT Imports: 2 Imported by: 0

README

Go Reference

Data sorting

Library for sorting data

You can sort any data using these algorithms. In this library for help and as example we added type Slice (slice for sorting ordered elements) and type Person with implementation Element interface

BubbleSort
ordered slice

Use function Bubble for sorting slice with ordered elements:

  • first param is a slice of constraints.Ordered elements (string, int, float64...)
  • second param should be type direction (sort.Asc or sort.Desc)
example with ordered slice
sort.Bubble[int]([]int{4, 5, 8, 7}, sort.Asc)
slice of structures

Use function BubbleStruct for sorting slice with of structures:

  • first param is a slice of structure elements. This slice should implement Element interface
  • second param should be type direction (sort.Asc or sort.Desc)
example with slice of structures
persons := []sort.Person{
    {"John", 42},
    {"Bob", 31},
    {"Jenny", 26},
    {"Michael", 17},
}
sort.BubbleStruct[int](persons, sort.Asc)
InsertionSort
ordered slice

Use function Insertion for sorting slice with ordered elements:

  • first param is a slice of constraints.Ordered elements (string, int, float64...)
  • second param should be type direction (sort.Asc or sort.Desc)
example with ordered slice
sort.Insertion[int]([]int{4, 5, 8, 7}, sort.Asc)
slice of structures

Use function InsertionStruct for sorting slice with of structures:

  • first param is a slice of structure elements. This slice should implement Element interface
  • second param should be type direction (sort.Asc or sort.Desc)
example with slice of structures:
persons := []sort.Person{
    {"John", 42},
    {"Bob", 31},
    {"Jenny", 26},
    {"Michael", 17},
}
sort.InsertionStruct[int](persons, sort.Asc)
MergeSort

You can sort Slice data using this algorithm. Use function Merge for sort package:

  • first param is array (type Slice).
  • second param should be type direction (sort.Asc or sort.Desc)

Example:

data := sort.Slice[int]([]int{4, 5, 8, 7})
result := sort.Merge(data, sort.Asc)
fmt.Println("Merge sort: ", result)
QuickSort
ordered slice

Use function Quick for sorting slice with ordered elements:

  • first param is a slice of constraints.Ordered elements (string, int, float64...)
  • second param should be type direction (sort.Asc or sort.Desc)
example with ordered slice
sort.Quick[int]([]int{4, 5, 8, 7}, sort.Asc)
slice of structures

Use function QuickStruct for sorting slice with of structures:

  • first param is a slice of structure elements. This slice should implement Element interface
  • second param should be type direction (sort.Asc or sort.Desc)
example with slice of structures
persons := []sort.Person{
    {"John", 42},
    {"Bob", 31},
    {"Jenny", 26},
    {"Michael", 17},
}
sort.QuickStruct[int](persons, sort.Asc)

Performance

$ go test -bench=. -benchmem
BenchmarkBubbleInt-8                            84884545                14.13 ns/op            0 B/op          0 allocs/op
BenchmarkBubbleString-8                         58206878                20.93 ns/op            0 B/op          0 allocs/op
BenchmarkBubbleStruct/structure-8               33827671                36.03 ns/op            0 B/op          0 allocs/op
BenchmarkInsertionInt-8                         120945201                9.541 ns/op           0 B/op          0 allocs/op
BenchmarkInsertionString-8                      68478008                17.75 ns/op            0 B/op          0 allocs/op
BenchmarkInsertionStruct/structure-8            34708350                36.13 ns/op            0 B/op          0 allocs/op
BenchmarkMergeInt-8                             11594480                99.62 ns/op           64 B/op          3 allocs/op
BenchmarkMergeString-8                           9863658               116.4 ns/op            80 B/op          2 allocs/op
BenchmarkQuickInt-8                             37386302                32.31 ns/op            0 B/op          0 allocs/op
BenchmarkQuickString-8                          26831978                42.77 ns/op            0 B/op          0 allocs/op
BenchmarkQuickStruct/structure-8                13882881                88.05 ns/op            0 B/op          0 allocs/op

Documentation

Overview

Package sort is a package for sorting data.

Index

Constants

View Source
const (
	// Desc specifies the sort direction to be descending.
	Desc direction = "desc"
	// Asc specifies the sort direction to be ascending.
	Asc direction = "asc"
)

Variables

This section is empty.

Functions

func Bubble

func Bubble[T constraints.Ordered](data Slice[T], direction direction)

Bubble sorts data using bubble sort algorithm

  • first param is a slice of constraints.Ordered elements (string, int, float64...)
  • second param should be `type direction` (`sort.Asc` or `sort.Desc`)

func BubbleStruct

func BubbleStruct[T constraints.Ordered, V Element[T]](data Elements[T, V], direction direction)

BubbleStruct sorts data of structures using bubble sort algorithm

  • first param is a slice of structure elements. This slice should implement Element interface
  • second param should be `type direction` (`sort.Asc` or `sort.Desc`)

func Insertion

func Insertion[T constraints.Ordered](data Slice[T], direction direction)

Insertion sorts data using insertion sort algorithm

  • first param is a slice of constraints.Ordered elements (string, int, float64...)
  • second param should be `type direction` (`sort.Asc` or `sort.Desc`)

func InsertionStruct

func InsertionStruct[T constraints.Ordered, V Element[T]](data Elements[T, V], direction direction)

InsertionStruct sorts data of structures using insertion sort algorithm

  • first param is a slice of structure elements. This slice should implement Element interface
  • second param should be `type direction` (`sort.Asc` or `sort.Desc`)

func Quick added in v1.1.0

func Quick[T constraints.Ordered](data Slice[T], direction direction)

func QuickStruct added in v1.1.0

func QuickStruct[T constraints.Ordered, V Element[T]](data Elements[T, V], direction direction)

Types

type Element

type Element[V constraints.Ordered] interface {
	// Element returns value of element.
	// Type of this element should be constraints.Ordered (int, string, float64 etc)
	// This function will be used for comparing elements
	Element() V
}

Element is an interface which needs to implement for slice sorting

type Elements

type Elements[T constraints.Ordered, V Element[T]] []V

type Person

type Person struct {
	Name string
	Age  int
}

Person is an example of structure which you want to sort

func (Person) Element

func (p Person) Element() int

Element - example of implementation Element() function for sorting of structures

type Slice

type Slice[V constraints.Ordered] []V

Slice is a slice for sorting ordered elements

func Merge

func Merge[V constraints.Ordered](data Slice[V], direction direction) Slice[V]

Merge sorts data using merge sort algorithm

  • first param is array (`type Slice`).
  • second param should be `type direction` (`sort.Asc` or `sort.Desc`)

Jump to

Keyboard shortcuts

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