heap_sort: Heap Sort Algorithm

View source: R/sorting_2_heap.R

heap_sortR Documentation

Heap Sort Algorithm

Description

Implements the heap sort algorithm, which sorts a numeric vector by first building a max heap and then repeatedly extracting the largest element to place it in its correct position in the sorted array.

Usage

heap_sort(v)

Arguments

v

A numeric vector to be sorted.

Details

Heap sort works by transforming the input vector into a max heap, a binary tree where each parent node is greater than or equal to its child nodes. After constructing the heap, the largest element (the root) is swapped with the last element, and the heap is rebuilt for the remaining elements. This process repeats until all elements are sorted.

Value

A numeric vector, sorted in ascending order.

Examples

heap_sort(rnorm(100))
heap_sort(sample(100))

vrunge/M2algorithmique documentation built on April 5, 2025, 4:06 a.m.