Arrays.sort()排序原理

您所在的位置:网站首页 arrays提供了数组排序方法 Arrays.sort()排序原理

Arrays.sort()排序原理

2023-04-12 19:38| 来源: 网络整理| 查看: 265

原文地址:Arrays.sort()排序原理

java.util.Arrays有针对各种数据类型的重载的sort的方法,下面我们主要介绍其中的两种(Arrays.sort(整数数组))

在继续讲解之前我们先来看这种排序方法的整体组成,对这个方法现有一个大概的了解

使用Arrays.sort对int数组进行排序时,Arrays.sort方法会调用DualPivotQuicksort.sort方法

/** * Sorts the specified array into ascending numerical order. * *

Implementation note: The sorting algorithm is a Dual-Pivot Quicksort * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm * offers O(n log(n)) performance on many data sets that cause other * quicksorts to degrade to quadratic performance, and is typically * faster than traditional (one-pivot) Quicksort implementations. * * @param a the array to be sorted */ public static void sort(int[] a) { DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); }

DualPivotQuicksort中实现了所有基本数据类型的排序方法

/** * Sorts the specified range of the array using the given * workspace array slice if possible for merging * * @param a the array to be sorted * @param left the index of the first element, inclusive, to be sorted * @param right the index of the last element, inclusive, to be sorted * @param work a workspace array (slice) * @param workBase origin of usable space in work array * @param workLen usable size of work array */ static void sort(int[] a, int left, int right, int[] work, int workBase, int workLen) { // Use Quicksort on small arrays 使用快速排序对小数组排序 if (right - left


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3