Sort multidimensional data

kd_sort(x, ...)

kd_order(x, ...)

kd_is_sorted(x)

Arguments

x

a matrix or arrayvec object

...

other arguments

Details

The algorithm used is a divide-and-conquer quicksort variant that recursively partions an range of tuples using the median of each successive dimension. Ties are resolved by cycling over successive dimensions. The result is an ordering of tuples matching their order if they were inserted into a kd-tree.

kd_order returns permutation vector that will order the rows of the original matrix, exactly as order.

Note

The matrix version will be slower because of data structure conversions.

See also

Examples

x = matrix(runif(200), 100) y = kd_sort(x) kd_is_sorted(y)
#> [1] TRUE
kd_order(x)
#> [1] 73 13 31 90 39 4 92 99 5 27 59 16 57 84 96 88 52 72 #> [19] 17 75 64 8 19 87 36 32 76 18 1 53 70 62 83 12 45 66 #> [37] 40 24 21 77 80 50 6 51 33 14 60 15 71 74 100 68 54 69 #> [55] 82 41 44 93 55 35 7 3 78 30 46 29 86 48 38 26 58 9 #> [73] 23 56 79 22 81 28 65 43 89 37 95 85 42 97 63 34 47 98 #> [91] 2 11 20 67 10 94 61 49 25 91
plot(y, type = "o", pch = 19, col = "steelblue", asp = 1)