code: def median(input, dim=None, keepdim=False, *, out=None):
      if dim is None:
          input = ivy.reshape(input, (-1,))
          sorted_input = ivy.sort(input)
          return sorted_input[(sorted_input.shape[0] - 1) // 2]
  
      median_tuple = namedtuple("median", ["values", "indices"])
  
      if input.ndim == 0:
          result = median_tuple(input, ivy.array(0))
      else:
          sorted_indices = ivy.argsort(input, axis=dim)
          median_indices = ivy.gather(
              sorted_indices,
              (sorted_indices.shape[dim] - 1) // 2,
              axis=dim,
          )
          median_values = ivy.take_along_axis(
              input, ivy.expand_dims(median_indices, axis=dim), dim
          ).squeeze(axis=dim)
  
          if keepdim:
              median_values = ivy.expand_dims(median_values, axis=dim)
              median_indices = ivy.expand_dims(median_indices, axis=dim)
  
          result = median_tuple(median_values, median_indices)
      if out is not None:
          ivy.inplace_update(out[0], result.values)
          ivy.inplace_update(out[1], result.indices)
          return out
      return result
  
imports: import ivy
  from ivy.func_wrapper import with_unsupported_dtypes, with_supported_dtypes
  from ivy.functional.frontends.torch.func_wrapper import to_ivy_arrays_and_back, numpy_to_torch_style_args
  from collections import namedtuple
  import ivy.functional.frontends.torch
label_desc: Use image node_id 13 for calling a global function with example usage: median(input, dim, keepdim) and returns: result, sorted_input, out