我最近经历了一些排序算法,包括冒泡排序,选择排序,插入排序,合并排序,堆排序,快速排序等,当我突然出现一个问题,当我们使用函数sort()时Java或Sorting算法遵循的任何其他语言?并且排序函数的算法对所有其他语言都相同吗?
例如,这是我在Java中的代码:
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
int i;
int A[] = {2, 1, 9, 6, 4};
for (i = 0; i < A.length ; i++)
{
System.out.println("Number = " + A[i]);
}
// sorting array
Arrays.sort(A);
System.out.println("The sorted int array is:");
for (i = 0; i < A.length ; i++)
{
System.out.println("Number = " + A[i]);
}
}
}
并且,我想知道Arrays.sort()使用哪种排序算法对数组A进行排序。
谢谢
答案 0 :(得分:1)
正如您所见here,他们使用DualPivotQuicksort算法。
/**
65 * Sorts the specified array into ascending numerical order.
66 *
67 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
68 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
69 * offers O(n log(n)) performance on many data sets that cause other
70 * quicksorts to degrade to quadratic performance, and is typically
71 * faster than traditional (one-pivot) Quicksort implementations.
72 *
73 * @param a the array to be sorted
74 */
75 public static void sort(int[] a) {
76 DualPivotQuicksort.sort(a);
77 }