sort

package
v1.32.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 0 Imported by: 0

README

Sort

  • Bubble Sort: Simple and easy to understand, but not very efficient for large lists.

  • Selection Sort: Selects the smallest (or largest) element and moves it to the beginning (or end) of the list.

  • Insertion Sort: Builds the final sorted array one item at a time, efficient for small datasets.

  • Merge Sort: Divides the array into two halves, sorts them, and then merges them back together. It's very efficient with a time complexity of O(n log n).

  • Quick Sort: Picks an element as a pivot and partitions the array around it. It's efficient with an average time complexity of O(n log n), but can be slow in the worst case.

  • Heap Sort: Uses a binary heap data structure to sort the array. It has a time complexity of O(n log n).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BubbleSort

func BubbleSort[T number](arr []T) []T

BubbleSort performs an in-place bubble sort on a slice of numeric values. It implements the bubble sort algorithm which repeatedly steps through the slice, compares adjacent elements and swaps them if they are in the wrong order. The function doesn't return new slice, but it sorts the slice in-place. Time Complexity: O(n²) where n is the length of the slice Space Complexity: O(1) as it sorts in-place

func HeapSort

func HeapSort[T number](arr []T) []T

HeapSort performs heap sort on a slice of numbers. It takes a slice of any numeric type T and returns a sorted slice. Time Complexity: O(n log n) Space Complexity: O(1)

func InsertionSort

func InsertionSort[T number](arr []T) []T

InsertionSort performs an in-place insertion sort on a slice of numeric values. It works by building a sorted array one element at a time by repeatedly taking the next unsorted element and inserting it into its correct position in the sorted portion. Time Complexity:

  • Best Case: O(n) when array is already sorted
  • Average Case: O(n²)
  • Worst Case: O(n²) when array is reverse sorted

Space Complexity: O(1) as it sorts in-place

func MergeSort

func MergeSort[T number](arr []T) []T

MergeSort performs a merge sort on a slice of numeric values. It uses the divide-and-conquer strategy by recursively dividing the input array into two halves, sorting them, and then merging the sorted halves. Time Complexity: O(n log n) for all cases Space Complexity: O(n) for temporary arrays during merging

func QuickSort

func QuickSort[T number](arr []T) []T

QuickSort performs a quick sort on a slice of numeric values. It uses the divide-and-conquer strategy by selecting a 'pivot' element and partitioning the surrounding array such that smaller elements are on the left and larger elements are on the right. Time Complexity:

  • Average Case: O(n log n)
  • Worst Case: O(n²) when array is already sorted
  • Best Case: O(n log n)

Space Complexity: O(log n) due to recursive call stack

func SelectionSort

func SelectionSort[T number](arr []T) []T

SelectionSort performs an in-place selection sort on a slice of numeric values. It works by dividing the input array into a sorted and an unsorted region. In each iteration, it finds the minimum element from the unsorted region and places it at the beginning of the sorted region. Time Complexity:

  • Best Case: O(n²)
  • Average Case: O(n²)
  • Worst Case: O(n²)

Space Complexity: O(1) as it sorts in-place

Types

This section is empty.

Jump to

Keyboard shortcuts

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