sorting

package
v0.0.0-...-f42b77b Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package sorting provides sorting algorithms that operate on generic slices.

Index

Examples

Constants

This section is empty.

Variables

View Source
var SliceLength = flag.Int("slice_length", 1000, "length of slices to test sorting on")

Functions

func BubbleSort

func BubbleSort[T constraints.Ordered](d []T)

BubbleSort performs an in-place bubble sort on the slice d

Example

ExampleBubbleSort calls BubbleSort on the slice [5, 4, 3, 2, 1] and expects output of [1 2 3 4 5]

s := []int{5, 4, 3, 2, 1}
BubbleSort(s)
fmt.Println(s)
Output:

[1 2 3 4 5]

func InsertionSort

func InsertionSort[T constraints.Ordered](d []T)

InsertionSort performs an in-place insertion sort on the slice d

Example

ExampleInsertionSort calls InsertionSort on the slice [5, 4, 3, 2, 1] and expects output of [1 2 3 4 5]

s := []int{5, 4, 3, 2, 1}
InsertionSort(s)
fmt.Println(s)
Output:

[1 2 3 4 5]

func MergeSort

func MergeSort[T constraints.Ordered](d []T)

MergeSort performs an in-place merge sort on the slice d

Example

ExampleMergeSort calls MergeSort on the slice [5, 4, 3, 2, 1] and expects output of [1 2 3 4 5]

s := []int{5, 4, 3, 2, 1}
MergeSort(s)
fmt.Println(s)
Output:

[1 2 3 4 5]

func QuickSort

func QuickSort[T constraints.Ordered](d []T)

QuickSort performs an in-place quick sort on the slice d

Example

ExampleQuickSort calls QuickSort on the slice [5, 4, 3, 2, 1] and expects output of [1 2 3 4 5]

s := []int{5, 4, 3, 2, 1}
QuickSort(s)
fmt.Println(s)
Output:

[1 2 3 4 5]

func RandomFloat64Slice

func RandomFloat64Slice(n int) []float64

func RandomIntSlice

func RandomIntSlice(n int) []int

func RandomStringSlice

func RandomStringSlice(n int) []string

func SelectionSort

func SelectionSort[T constraints.Ordered](d []T)

SelectionSort performs an in-place selection sort on the slice d

Example

ExampleSelectionSort calls SelectionSort on the slice [5, 4, 3, 2, 1] and expects output of [1 2 3 4 5]

s := []int{5, 4, 3, 2, 1}
SelectionSort(s)
fmt.Println(s)
Output:

[1 2 3 4 5]

Types

This section is empty.

Jump to

Keyboard shortcuts

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