sort函数

帮助函数如下所示

 

sort(a, axis=-1, kind='quicksort', order=None)
    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 : {'quicksort', 'mergesort', 'heapsort'}, optional
       Sorting algorithm. Default is 'quicksort'.
   order : str or list of str, optional
       When `a` is an array with fields defined, this argument specifies
       which fields to compare first, second, etc.  A single field can
       be specified as a string, and not all fields need be specified,
       but unspecified fields will still be used, in the order in which
       they come up in the dtype, to break ties.

可以看出参数共有三种选项,a是要排序的数组(不过一般两种调用方式a.sort(),sort(a)),axis表示排序的维度(axis的详细含义举例说明),但是排序最快的是last axis。kind表示使用排序的类型,共有三种类型{'quicksort', 'mergesort', 'heapsort'},,默认为quicksort,具体的速度与时间如下所示

 =========== ======= ============= ============ =======
    kind      speed   worst case    work space  stable
 =========== ======= ============= ============ =======
 'quicksort'    1     O(n^2)            0          no
 'mergesort'    2     O(n*log(n))      ~n/2        yes
 'heapsort'     3     O(n*log(n))       0          no
 =========== ======= ============= ============ =======

排序实例

  >>> a = np.array([[1,4],[3,1]])
  >>> np.sort(a)                # sort along the last axis
  array([[1, 4],
         [1, 3]])
  >>> np.sort(a, axis=None)     # sort the flattened array
  array([1, 1, 3, 4])
  >>> np.sort(a, axis=0)        # sort along the first axis
  array([[1, 1],
         [3, 4]])

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值