<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/ops/array_ops.py#L6111-L6191) |

Searches for where a value would go in a sorted sequence.

#### View aliases


**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.searchsorted`](https://www.tensorflow.org/api_docs/python/tf/searchsorted)

<br />

    tf.searchsorted(
        sorted_sequence,
        values,
        side='left',
        out_type=https://www.tensorflow.org/api_docs/python/tf/dtypes#int32,
        name=None
    )

This is not a method for checking containment (like python `in`).

The typical use case for this operation is "binning", "bucketing", or
"discretizing". The `values` are assigned to bucket-indices based on the
**edges** listed in `sorted_sequence`. This operation
returns the bucket-index for each value.

    edges = [-1, 3.3, 9.1, 10.0]
    values = [0.0, 4.1, 12.0]
    tf.searchsorted(edges, values).numpy()
    array([1, 2, 4], dtype=int32)

The `side` argument controls which index is returned if a value lands exactly
on an edge:

    seq = [0, 3, 9, 10, 10]
    values = [0, 4, 10]
    tf.searchsorted(seq, values).numpy()
    array([0, 2, 3], dtype=int32)
    tf.searchsorted(seq, values, side="right").numpy()
    array([1, 2, 5], dtype=int32)

The `axis` is not settable for this operation. It always operates on the
innermost dimension (`axis=-1`). The operation will accept any number of
outer dimensions. Here it is applied to the rows of a matrix:

    sorted_sequence = [[0., 3., 8., 9., 10.],
                       [1., 2., 3., 4., 5.]]
    values = [[9.8, 2.1, 4.3],
              [0.1, 6.6, 4.5, ]]
    tf.searchsorted(sorted_sequence, values).numpy()
    array([[4, 1, 2],
           [0, 5, 4]], dtype=int32)

> [!NOTE]
> **Note:** This operation assumes that `sorted_sequence` **is sorted** along the innermost axis, maybe using `tf.sort(..., axis=-1)`. **If the sequence is not
> sorted, no error is raised** and the content of the returned tensor is not well defined.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `sorted_sequence` | N-D `Tensor` containing a sorted sequence. |
| `values` | N-D `Tensor` containing the search values. |
| `side` | 'left' or 'right'; 'left' corresponds to lower_bound and 'right' to upper_bound. |
| `out_type` | The output type (`int32` or `int64`). Default is [`tf.int32`](https://www.tensorflow.org/api_docs/python/tf#int32). |
| `name` | Optional name for the operation. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| An N-D `Tensor` the size of `values` containing the result of applying either lower_bound or upper_bound (depending on side) to each value. The result is not a global index to the entire `Tensor`, but the index in the last dimension. ||

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `ValueError` | If the last dimension of `sorted_sequence >= 2^31-1` elements. If the total size of `values` exceeds `2^31 - 1` elements. If the first `N-1` dimensions of the two tensors don't match. |

<br />