partial_sort: Partial sort an lvec

View source: R/partial_sort.R

partial_sortR Documentation

Partial sort an lvec

Description

Partial sort an lvec

Usage

partial_sort(x, pivots, clone = TRUE)

partial_order(x, pivots)

Arguments

x

an object of type lvec

pivots

a numeric vector with indices at which the vector will be sorted. See details for more information.

clone

clone the vector first before sorting; or sort (and therefore modify) the input vector directly.

Details

After partial sorting the vector values at the pivots are the same as the vector values of a completely sorted vector. Furthermore, for each pivot i all elements x[j]; j < i are smaller or equal to than x[i] and all elements x[j]; j > i are larger than or equal to x[i].

The speed of this operation should be O(n, k) with n the size of the lvec and k the number of pivots.

Examples

x <- as_lvec(rnorm(100))
y <- partial_sort(x, c(10, 50, 90))
x_sorted <- sort(x)
stopifnot(all(y[c(10, 50, 90)] == x_sorted[c(10, 50, 90)]))
stopifnot(max(y[1:9]) <= min(y[11:100]))
stopifnot(max(y[1:49]) <= min(y[51:100]))
stopifnot(max(y[1:89]) <= min(y[91:100]))


djvanderlaan/lvec_stats documentation built on Oct. 4, 2022, 7:02 p.m.