sort

package module
v0.0.0-...-40b7f2c Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2019 License: BSL-1.0 Imports: 1 Imported by: 0

README

WARNING There have been some breaking changes: All algorithms now operate on the fixed 64bit versions of the supported number types. ([]int64 instead of []int)

Sort

This package contains implementations of Radix Sort, Quicksort, Merge Sort and Insertion Sort in Golang. They are licensed under the Boost Software License 1.0. To run the benchmarks just download the package and run go test -bench .. Old benchmark results can be found in benchmark.md but might be inaccurate due to a sample size of 1. All sorting algorithms in this package might directly operate on the byte slice passed to them so please make a copy in case you still need the original order.

Radix Sort

Radix sort is an extremely fast algorithm implemented here specifically to sort unsigned integers. It is 2-3 times faster than Quicksort for large sets of data. Please make sure you have at least enough RAM available to fit your application and 4 times the size of the set.

sort.RadixSort(set []uint64) []uint64

This version of Radix sort uses bytewise sorting with 256 buckets which increases memory usage due to the additional type conversions but improves performance. The implementation is based on the design by Austin G. Walters described in Radix Sort in Go (Golang)

Quicksort

Quicksort is a very versatile and fast sorting algorithm implemented here to sort integers. It is 2-3 times faster than Golang's internal sort package for large sets of data. It uses only one copy of the set and is therefore preferred in constrained environments.

sort.QuickSort(set []int64) []int64

This implementation is based on the Hoare partition scheme and has been adapted from the pseudocode on Wikipedia Quicksort

Merge Sort

Merge sort is an algorithm that sorts sets by dividing them and sorting them while merging them together. It is slower than Quicksort but can be especially useful when merging multiple sets of sorted data which may occur when distributing sorting across devices.

sort.MergeSort(set []int64) []int64
sort.MergeSortedSets(set1 []int64, set2 []int64) []int64

Merge sort is a stable sorting algorithm that handles negative values and should therefore be preferred over Quicksort when you need to preserve the order of equal elements. Due to keeping up to 2 copies of the full set in memory, it is not recommended to use Merge sort in constrained environments.

Merge sorted sets is intended to be used to merge two sorted sets together. It is used by Merge sort internally but can also be used to merge the results of multi-threaded or distributed sorting algorithms.

Insertion Sort

Insertion sort is a sorting algorithm that works by inserting individual items at the right position, keeping the existing set sorted. It can be used to sort a set by starting with only one value in the sorted set but is much better suited for inserting a single new element into a sorted set.

sort.InsertionSort(set []int64) []int64
sort.InsertSorted(sortedSet []int64, insert int64) []int64

Due to inferior speed compared to all other algorithms in this package I would not recommend using Insertion sort for normal applications. If you don't know a clear advantage of using it in your application you probably shouldn't.

Insert is intended to be used when adding one new element as may occur when adding a new item to a database.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InsertSorted

func InsertSorted(sorted []int64, insert int64) []int64

InsertSorted inserts a single element into a sorted set. This can be useful when inserting a new element into a database.

func InsertionSort

func InsertionSort(sort []int64) []int64

InsertionSort is an implementation of the Insertion sort algorithm. It is extremely slow compared to other algorithms like Quicksort, Merge sort and Radix sort and should therefore only be used for very small sets.

func MergeSort

func MergeSort(set []int64) []int64

MergeSort is an implementation of the merge sort algorithm

func MergeSortedSets

func MergeSortedSets(a, b []int64) []int64

MergeSortedSets is the merging part of the merge sort algorithm and may be used to combine two sorted sets. You might find it useful when combining the results of a distributed sort.

func QuickSort

func QuickSort(s []int64) []int64

QuickSort sorts the supplied integer array using quicksort

func RadixSort

func RadixSort(items []uint64) []uint64

RadixSort is radix sort for 64 bit unsigned integers implemented using binary data. This algorithm does not compare the data and due to the binary implementation also does not perform any calculations with it. It is faster than quicksort but cannot handle negative values.

Types

This section is empty.

Jump to

Keyboard shortcuts

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