Return a sorted copy of an array. Parameters ---------- a : array_like Array to be sorted. axis : int or None, optional Axis along which to sort. If None, the array is flattened before sorting. The default is -1, which sorts along the last axis. kind
(a, axis=-1, kind=None, order=None)
| 864 | |
| 865 | @array_function_dispatch(_sort_dispatcher) |
| 866 | def sort(a, axis=-1, kind=None, order=None): |
| 867 | """ |
| 868 | Return a sorted copy of an array. |
| 869 | |
| 870 | Parameters |
| 871 | ---------- |
| 872 | a : array_like |
| 873 | Array to be sorted. |
| 874 | axis : int or None, optional |
| 875 | Axis along which to sort. If None, the array is flattened before |
| 876 | sorting. The default is -1, which sorts along the last axis. |
| 877 | kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, optional |
| 878 | Sorting algorithm. The default is 'quicksort'. Note that both 'stable' |
| 879 | and 'mergesort' use timsort or radix sort under the covers and, in general, |
| 880 | the actual implementation will vary with data type. The 'mergesort' option |
| 881 | is retained for backwards compatibility. |
| 882 | |
| 883 | .. versionchanged:: 1.15.0. |
| 884 | The 'stable' option was added. |
| 885 | |
| 886 | order : str or list of str, optional |
| 887 | When `a` is an array with fields defined, this argument specifies |
| 888 | which fields to compare first, second, etc. A single field can |
| 889 | be specified as a string, and not all fields need be specified, |
| 890 | but unspecified fields will still be used, in the order in which |
| 891 | they come up in the dtype, to break ties. |
| 892 | |
| 893 | Returns |
| 894 | ------- |
| 895 | sorted_array : ndarray |
| 896 | Array of the same type and shape as `a`. |
| 897 | |
| 898 | See Also |
| 899 | -------- |
| 900 | ndarray.sort : Method to sort an array in-place. |
| 901 | argsort : Indirect sort. |
| 902 | lexsort : Indirect stable sort on multiple keys. |
| 903 | searchsorted : Find elements in a sorted array. |
| 904 | partition : Partial sort. |
| 905 | |
| 906 | Notes |
| 907 | ----- |
| 908 | The various sorting algorithms are characterized by their average speed, |
| 909 | worst case performance, work space size, and whether they are stable. A |
| 910 | stable sort keeps items with the same key in the same relative |
| 911 | order. The four algorithms implemented in NumPy have the following |
| 912 | properties: |
| 913 | |
| 914 | =========== ======= ============= ============ ======== |
| 915 | kind speed worst case work space stable |
| 916 | =========== ======= ============= ============ ======== |
| 917 | 'quicksort' 1 O(n^2) 0 no |
| 918 | 'heapsort' 3 O(n*log(n)) 0 no |
| 919 | 'mergesort' 2 O(n*log(n)) ~n/2 yes |
| 920 | 'timsort' 2 O(n*log(n)) ~n/2 yes |
| 921 | =========== ======= ============= ============ ======== |
| 922 | |
| 923 | .. note:: The datatype determines which of 'mergesort' or 'timsort' |
no test coverage detected