• 简介
  • 开始
    • 资源
  • 变量
    • 可用的变量名
    • 命名规范
  • 整数和浮点数
    • 整数
    • 溢出
    • 浮点数
    • 浮点数类型的零
    • 精度
    • 任意精度的算术
    • 代数系数
    • 零和一
  • 数学运算和基本函数
    • 算术运算符
    • 位运算符
    • 复合赋值运算符
    • 数值比较
    • 链式比较
    • 基本函数
  • 复数和分数
    • 复数
    • 分数
  • 字符串
    • 字符
    • 字符串基础
    • Unicode 和 UTF-8
    • 内插
    • 一般操作
    • 非标准字符串文本
    • 字节数组文本
    • Version Number Literals
  • 函数
    • 参数传递行为
    • return 关键字
    • 函数运算符
    • 特殊名字的运算符
    • 匿名函数
    • 多返回值
    • 变参函数
    • 可选参数
    • 关键字参数
    • 默认值的求值作用域
    • 函数参数的块语法
  • 控制流
    • 复合表达式
    • 条件求值
    • 短路求值
    • 重复求值: 循环
    • 异常处理
    • 任务(也称为协程)
  • 变量的作用域
    • For 循环及 Comprehensions
    • 常量
  • 类型
    • 类型声明
    • 抽象类型
    • 位类型
    • 复合类型
    • 不可变复合类型
    • 被声明类型
    • 多元组类型
    • 类型共用体
    • 参数化类型
    • 类型别名
    • 类型运算
  • 方法
    • 定义方法
    • 方法歧义
    • 参数化方法
    • 关于可选参数和关键字参数
  • 构造函数
    • 外部构造方法
    • 内部构造方法
    • 部分初始化
    • 参数化构造方法
    • 案例:分数
  • 类型转换和类型提升
    • 类型转换
    • 类型提升
  • 模块
    • Summary of module usage
  • 元编程
    • 表达式和求值
    • 宏
    • 反射
  • 多维数组
    • 数组
    • 稀疏矩阵
  • 线性代数
    • 矩阵分解
    • 特殊矩阵
  • 网络和流
    • 基本流 I/O
    • Text I/O
    • 文本 I/O
    • Working with Files
    • 简单的 TCP 例子
    • 解析 IP 地址
  • 并行计算
    • 数据移动
    • 并行映射和循环
    • Synchronization With Remote References
    • Scheduling
    • 分布式数组
    • 构造分布式数组
    • 分布式数组运算
    • Shared Arrays (Experimental, UNIX-only feature)
    • ClusterManagers
  • 日期和时间
    • 构造函数
    • 时间间隔/比较
    • 访问函数
    • 查询函数
    • 时间间隔算术运算
    • 调整函数
    • 时间间隔
  • 可空类型
    • 创建 Nullable 对象
    • 检查 Nullabe 对象是否含有数据
    • 安全地访问 Nullable 对象的内部数据
  • Interacting With Julia
    • The different prompt modes
    • Key bindings
    • Tab 补全
  • 运行外部程序
    • 内插
    • 引用
    • 管道
  • 调用 C 和 Fortran 代码
    • 把 C 类型映射到 Julia
    • 通过指针读取数据
    • 用指针传递修改值
    • 垃圾回收机制的安全
    • 非常量函数说明
    • 间接调用
    • 调用方式
    • Accessing Global Variables
    • Passing Julia Callback Functions to C
    • C++
    • 处理不同平台
  • 嵌入式 Julia
    • 高级嵌入
    • 类型转换
    • 调用 Julia 的函数
    • 内存管理
    • 处理数组
    • 异常
  • 扩展包
    • 扩展包状态
    • 添加和删除扩展包
    • 安装未注册的扩展包
    • 更新扩展包
    • Checkout, Pin and Free
  • 开发扩展包
    • Initial Setup
    • Generating a New Package
    • Making Your Package Available
    • Publishing Your Package
    • Tagging Package Versions
    • Fixing Package Requirements
    • 依赖关系
  • 代码性能优化
    • 避免全局变量
    • 使用 @time 来衡量性能并且留心内存分配
    • Avoid containers with abstract type parameters
    • 类型声明
    • 把函数拆开
    • 写“类型稳定”的函数
    • 避免改变变量类型
    • 分离核心函数
    • Access arrays in memory order, along columns
    • Pre-allocating outputs
    • Avoid string interpolation for I/O
    • 处理有关舍弃的警告
    • 小技巧
    • Performance Annotations
  • 代码样式
    • 写成函数,别写成脚本
    • 避免类型过于严格
    • Handle excess argument diversity in the caller
    • 如果函数修改了它的参数,在函数名后加 !
    • 避免奇葩的类型集合
    • Try to avoid nullable fields
    • Avoid elaborate container types
    • 使用和 Julia base/ 相同的命名传统
    • 不要滥用 try-catch
    • 不要把条件表达式用圆括号括起来
    • 不要滥用 ...
    • Don’t use unnecessary static parameters
    • Avoid confusion about whether something is an instance or a type
    • 不要滥用 macros
    • Don’t expose unsafe operations at the interface level
    • Don’t overload methods of base container types
    • Be careful with type equality
    • 不要写 x->f(x)
  • 常见问题
    • 会话和 REPL
    • 函数
    • 类型,类型声明和构造方法
    • Nothingness and missing values
    • Julia 发行版
    • 开发 Julia
  • 与其它语言的区别
    • 与 MATLAB 的区别
    • 与 R 的区别
    • 与 Python 的区别
  • The Standard Library
    • Introduction
    • Getting Around
    • All Objects
    • Types
    • Generic Functions
    • Syntax
    • Iteration
    • General Collections
    • Iterable Collections
    • Indexable Collections
    • Associative Collections
    • Set-Like Collections
    • Dequeues
    • Strings
    • I/O
    • Network I/O
    • Text I/O
    • Multimedia I/O
    • Memory-mapped I/O
    • Standard Numeric Types
    • Mathematical Operators
    • Mathematical Functions
    • Data Formats
    • Numbers
    • BigFloats
    • Random Numbers
    • Arrays
    • Combinatorics
    • Statistics
    • Signal Processing
    • Numerical Integration
    • Parallel Computing
    • Distributed Arrays
    • Shared Arrays (Experimental, UNIX-only feature)
    • System
    • C Interface
    • Errors
    • Tasks
    • Events
    • Reflection
    • Internals
  • Sparse Matrices
  • Linear Algebra
  • BLAS Functions
  • Constants
  • Filesystem
  • Punctuation
  • Sorting and Related Functions
    • Sorting Functions
    • Order-Related Functions
    • Sorting Algorithms
  • Package Manager Functions
  • Collections and Data Structures
    • PriorityQueue
    • Heap Functions
  • Graphics
    • Geometry
  • Unit and Functional Testing
    • Overview
    • Handlers
    • Macros
    • Functions
  • Testing Base Julia
  • Profiling
    • Basic usage
    • Accumulation and clearing
    • Options for controlling the display of profile results
    • Configuration
    • Function reference
  • Base.Cartesian
    • Principles of usage
    • Basic syntax
  • 宏
    • @time
    • @which
    • @printf
    • @inbounds
    • @assert
    • @goto & @label
  • 心得
    • 数字分隔符
  • 优化代码
    • 使用宏 @inbounds 加速循环
    • 节约空间
    • 手动优化循环(manual loop unrolling)
    • 调用C或Fortran加速
    • 尽量使用一维数组
    • 良好的编程习惯
 
Julia Language
  • Docs »
  • Sorting and Related Functions
  • Edit on GitHub

Sorting and Related Functions¶

Julia has an extensive, flexible API for sorting and interacting with already-sorted arrays of values. For many users, sorting in standard ascending order, letting Julia pick reasonable default algorithms will be sufficient:

julia> sort([2,3,1])
3-element Array{Int64,1}:
 1
 2
 3

You can easily sort in reverse order as well:

julia> sort([2,3,1], rev=true)
3-element Array{Int64,1}:
 3
 2
 1

To sort an array in-place, use the “bang” version of the sort function:

julia> a = [2,3,1];

julia> sort!(a);

julia> a
3-element Array{Int64,1}:
 1
 2
 3

Instead of directly sorting an array, you can compute a permutation of the array’s indices that puts the array into sorted order:

julia> v = randn(5)
5-element Array{Float64,1}:
  0.297288
  0.382396
 -0.597634
 -0.0104452
 -0.839027

julia> p = sortperm(v)
5-element Array{Int64,1}:
 5
 3
 4
 1
 2

julia> v[p]
5-element Array{Float64,1}:
 -0.839027
 -0.597634
 -0.0104452
  0.297288
  0.382396

Arrays can easily be sorted acording to an arbitrary transformation of their values:

julia> sort(v, by=abs)
5-element Array{Float64,1}:
 -0.0104452
  0.297288
  0.382396
 -0.597634
 -0.839027

Or in reverse order by a transformation:

julia> sort(v, by=abs, rev=true)
5-element Array{Float64,1}:
 -0.839027
 -0.597634
  0.382396
  0.297288
 -0.0104452

Reasonable sorting algorithms are used by default, but you can choose other algorithms as well:

julia> sort(v, alg=InsertionSort)
5-element Array{Float64,1}:
 -0.839027
 -0.597634
 -0.0104452
  0.297288
  0.382396

Sorting Functions¶

sort!(v, [alg=<algorithm>,] [by=<transform>,] [lt=<comparison>,] [rev=false])¶

Sort the vector v in place. QuickSort is used by default for numeric arrays while MergeSort is used for other arrays. You can specify an algorithm to use via the alg keyword (see Sorting Algorithms for available algorithms). The by keyword lets you provide a function that will be applied to each element before comparison; the lt keyword allows providing a custom “less than” function; use rev=true to reverse the sorting order. These options are independent and can be used together in all possible combinations: if both by and lt are specified, the lt function is applied to the result of the by function; rev=true reverses whatever ordering specified via the by and lt keywords.

sort(v, [alg=<algorithm>,] [by=<transform>,] [lt=<comparison>,] [rev=false])¶

Variant of sort! that returns a sorted copy of v leaving v itself unmodified.

sort(A, dim, [alg=<algorithm>,] [by=<transform>,] [lt=<comparison>,] [rev=false])

Sort a multidimensional array A along the given dimension.

sortperm(v, [alg=<algorithm>,] [by=<transform>,] [lt=<comparison>,] [rev=false])¶

Return a permutation vector of indices of v that puts it in sorted order. Specify alg to choose a particular sorting algorithm (see Sorting Algorithms). MergeSort is used by default, and since it is stable, the resulting permutation will be the lexicographically first one that puts the input array into sorted order – i.e. indices of equal elements appear in ascending order. If you choose a non-stable sorting algorithm such as QuickSort, a different permutation that puts the array into order may be returned. The order is specified using the same keywords as sort!.

sortrows(A, [alg=<algorithm>,] [by=<transform>,] [lt=<comparison>,] [rev=false])¶

Sort the rows of matrix A lexicographically.

sortcols(A, [alg=<algorithm>,] [by=<transform>,] [lt=<comparison>,] [rev=false])¶

Sort the columns of matrix A lexicographically.

Order-Related Functions¶

issorted(v, [by=<transform>,] [lt=<comparison>,] [rev=false])¶

Test whether a vector is in sorted order. The by, lt and rev keywords modify what order is considered to be sorted just as they do for sort.

searchsorted(a, x, [by=<transform>,] [lt=<comparison>,] [rev=false])¶

Returns the range of indices of a which compare as equal to x according to the order specified by the by, lt and rev keywords, assuming that a is already sorted in that order. Returns an empty range located at the insertion point if a does not contain values equal to x.

searchsortedfirst(a, x, [by=<transform>,] [lt=<comparison>,] [rev=false])¶

Returns the index of the first value in a greater than or equal to x, according to the specified order. Returns length(a)+1 if x is greater than all values in a.

searchsortedlast(a, x, [by=<transform>,] [lt=<comparison>,] [rev=false])¶

Returns the index of the last value in a less than or equal to x, according to the specified order. Returns 0 if x is less than all values in a.

select!(v, k, [by=<transform>,] [lt=<comparison>,] [rev=false])¶

Partially sort the vector v in place, according to the order specified by by, lt and rev so that the value at index k (or range of adjacent values if k is a range) occurs at the position where it would appear if the array were fully sorted. If k is a single index, that values is returned; if k is a range, an array of values at those indices is returned. Note that select! does not fully sort the input array, but does leave the returned elements where they would be if the array were fully sorted.

select(v, k, [by=<transform>,] [lt=<comparison>,] [rev=false])¶

Variant of select! which copies v before partially sorting it, thereby returning the same thing as select! but leaving v unmodified.

Sorting Algorithms¶

There are currently three sorting algorithms available in base Julia:

  • InsertionSort
  • QuickSort
  • MergeSort

InsertionSort is an O(n^2) stable sorting algorithm. It is efficient for very small n, and is used internally by QuickSort.

QuickSort is an O(n log n) sorting algorithm which is in-place, very fast, but not stable – i.e. elements which are considered equal will not remain in the same order in which they originally appeared in the array to be sorted. QuickSort is the default algorithm for numeric values, including integers and floats.

MergeSort is an O(n log n) stable sorting algorithm but is not in-place – it requires a temporary array of equal size to the input array – and is typically not quite as fast as QuickSort. It is the default algorithm for non-numeric data.

The sort functions select a reasonable default algorithm, depending on the type of the array to be sorted. To force a specific algorithm to be used for sort or other soring functions, supply alg=<algorithm> as a keyword argument after the array to be sorted.

Next Previous

Sphinx theme provided by Read the Docs