sort

package
v0.0.0-...-038bce5 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bubble

func Bubble[V constraints.Ordered](s []V, asc bool)

Bubble sorts a slice of orderable elements by bubbling the smallest or largest element to the end of a slice. The time complexity of the bubble sort is O(n^2), as it requires two nested loops. The space complexity is O(1), as the sort is done in place.

func Count

func Count[V constraints.Integer](s []V, asc bool)

Count sorts a slice of integer elements by counting the number of elements and positioning them in the correct order. The time complexity is O(n + k) where n is the number of elements and k is the maximum value. The space complexity is O(k) as we need to create a slice of length k to store the count of each elements.

func Insertion

func Insertion[V constraints.Ordered](s []V, asc bool)

Insertion sorts a slice of orderable elements by picking an element and inserting it into the correct position in the sorted slice. The time complexity of the insertion sort is O(n^2), as it compares each element with every other element in the slice. The space complexity is O(1), as the sort is done in place.

func Merge

func Merge[V constraints.Ordered](s []V, asc bool)

Merge sorts a slice of orderable elements by dividing the slice into smaller slices, sorting each slice, and then merging them back together (divide and conquer). The time complexity of the merge sort is O(n log n), as it is always dividing the slices in half for processing. The space complexity is O(n), as it is creating a new slice for each merge.

func Quick

func Quick[V constraints.Ordered](s []V, asc bool)

Quick sorts a slice of orderable elements by dividing the problem into smaller slices and sorting them recursively (divide and conquer). It uses a pivot element to partition the slice into two sub-slices. The elements smaller than the pivot are placed to the left of the pivot and the elements greater than the pivot are placed to the right of the pivot. The pivot element is then placed in its correct position in the sorted slice. The sub-slices are then sorted recursively. The time complexity of the quick sort algorithm is O(n log n) in the average case and O(n^2) in the worst case. The space complexity is O(log n) in the average case and O(n) in the worst case.

func Selection

func Selection[V constraints.Ordered](s []V, asc bool)

Selection sorts a slice of orderable elements by repeatedly finding the smallest or largest element and moving it to the beginning or end of the slice. The time complexity of the selection sort is O(n^2), as it makes n-1 passes through the slice and performs n-1 comparisons per pass. The space complexity is O(1), as it only requires a single temporary variable.

Types

This section is empty.

Jump to

Keyboard shortcuts

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