display | more...
In the k programming language, grade up is the monadic case of <.

<x returns all of the indices of elements in x so that the index of the smallest element is first and the index of the largest element is last. (The mnemonic device here is that the small elements are on the left and the large elements are on the right, just as the point of the symbol is on the left and the wide open part is on the right.)

Grade up (and its reverse, grade down) are frequently used to sort an array; x@ (x at grade up x) returns the elements of x in sorted order.

However, the grade operators are more versatile than a by-element sort because the indices can be applied elsewhere. For example, assume that x and y are parallel arrays, where x contains keys and y contains parallel values. You would like them to be sorted the same so that a binary search for the key in x leads to a value in y; however, if you sort x, you lose the connection between x and y. But if you obtain the grade up of x and apply it to both arrays, x will be in sorted order and y's elements will have been permuted to match x. Illustrated at the k command prompt (user input indented 2 spaces, results flush left):

  x:3 5 0 2 1
3 5 0 2 1
`three `five `zero `two `one
0 1 2 3 5
`five `one `three `two `zero
`zero `one `two `three `five

Log in or register to write something here or to contact authors.