`
leonzhx
  • 浏览: 786082 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Why java Arrays use two different sort algorithms for different types?

    博客分类:
  • Java
阅读更多

Java 7 uses Dual-Pivot Quicksort for primitives and TimSort for objects.

 

According to the Java 7 API doc for primitives:

 

    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.

 

According to the Java 7 API doc for objects:

 

    The implementation was adapted from Tim Peters's list sort for Python ( TimSort). It uses techiques from Peter McIlroy's "Optimistic Sorting and Information Theoretic Complexity", in Proceedings of the Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, January 1993.

 

The most likely reason: quicksort is not stable, i.e. equal entries can change their relative position during the sort; among other things, this means that if you sort an already sorted array, it may not stay unchanged.

Since primitive types have no identity (there is no way to distinguish two ints with the same value), this does not matter for them. But for reference types, it could cause problems for some applications. Therefore, a stable merge sort is used for those.

OTOH, a reason not to use the (guaranteed n*log(n)) merge sort for primitive types might be that it requires making a clone of the array. For reference types, where the referred objects usually take up far more memory than the array of references, this generally does not matter. But for primitive types, cloning the array outright doubles the memory usage.

分享到:
评论

相关推荐

    Two Efficient Algorithms for Linear Suffix Array Construction

    ### 两种高效的线性后缀数组构建算法 #### 摘要与背景 本文提出两种高效的线性后缀数组构建算法。这两种算法通过分治法和递归技术实现了线性的性能。它们与其他线性时间复杂度的后缀数组构建算法的不同之处在于...

    Java Arrays.sort和Collections.sort排序实现原理解析

    Java中的`Arrays.sort()`和`Collections.sort()`是两个常用的排序函数,它们分别用于对数组和集合进行排序。这两个函数在内部实现上有所不同,但都基于高效的排序算法。 首先,`Collections.sort()`方法在处理列表...

    JAVA基于Arrays.sort()实现数组升序和降序

    "JAVA基于Arrays.sort()实现数组升序和降序" 在 Java 中,排序数组是非常常见的操作之一,而 Java 提供了多种方式来实现数组的排序,其中一种常用的方法是使用 Arrays.sort() 方法。今天,我们将详细介绍如何使用 ...

    Java 9 Data Structures and Algorithms

    Java 9 Data Structures and Algorithms by Debasish Ray Chawdhuri English | 28 Apr. 2017 | ASIN: B01KM5RLGS | 340 Pages | AZW3 | 4.28 MB Key Features This book provides complete coverage of reactive ...

    PHP 7 - Data Structures and Algorithms

    Make use of greedy, dynamic, and pattern matching algorithms ? Implement tree, heaps, and graph algorithms ? Apply PHP functional data structures and built-in data structures and algorithms

    PHP.Arrays.in.PHP.7

    Why do we need arrays? When do we need to use arrays? Are arrays efficient? Can arrays reduce coding time? When do you use multi-dimensional and associative arrays? What is an object array? What You'...

    java arrays类.docx

    Java中的Arrays类是Java.util包下提供的一个工具类,它包含了一系列静态方法,方便开发者对数组进行各种操作,包括但不限于排序、搜索、比较、复制、填充等。在深入讲解Arrays类的方法之前,我们先理解一下Java数组...

    C++ Data Structures and Algorithms

    Then, we will learn how to implement different sorting algorithms, such as quick sort and heap sort. Along with these, we will dive into searching algorithms such as linear search, binary search and ...

    LeetCode4 Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Java AC版本

    Introduction to Parallel Algorithms - Arrays Trees Hypercubes

    The contents itself is organized in three chapters according to the network architecture: arrays and trees for Chapter 1 (117 pages), meshes of trees for Chapter 2 (117 pages), and hypercubes and ...

    Java.9.Data.Structures.and.Algorithms.2017.4.pdf

    From here, we introduce you to concepts such as arrays, linked lists, as well as abstract data types such as stacks and queues. Next, we’ll take you through the basics of functional programming ...

    Java Arrays工具类用法详解

    "Java Arrays工具类用法详解" Java Arrays工具类是Java语言中的一种工具类,主要提供了数组元素的修改、复制、排序等操作。该类中的方法均为static修饰的,可以直接通过Arrays.xxx(xxx)的形式调用方法。 1. Arrays...

    Java in Two Semesters Featuring JavaFX, 4th Edition

    provides additional resources at its associated website (simply go to springer.com and search for “Java in Two Semesters”), including a guide on how to install and use the NetBeans™ Java IDE. ...

    Data Structures and Algorithms in Java, 5th Edition (Part 3/3)

    Java source code for all examples in the book Animations Library (net.datastructures) of Java constructs used in the book Problems database and search engine Student hints to all exercises in the...

    深入java虚拟机(inside the java virtual machine)

    java虚拟机的运行机理的详细介绍 Inside the Java Virtual Machine Bill Venners $39.95 0-07-913248-0 Inside the Java Virtual Machine Acknowledgments Introduction Part One: Java's Architecture 1 ...

    深入理解java中Arrays.sort()的用法

    "深入理解Java中Arrays.sort()的用法" 在Java中,Arrays.sort()是一个非常重要的方法,它可以对数组进行排序。该方法是Arrays类的静态方法,在需要对数组进行排序时,非常的好用。但是sort()的参数有好几种,基本上...

    java中的arrays.sort()代码详解

    "Java中的Arrays.sort()代码详解" Arrays.sort()是Java中的一种排序算法,该方法可以对数组进行排序,具体来说,它可以对基本类型数组和对象数组进行排序。在本文中,我们将详细介绍Arrays.sort()的使用方法,包括...

Global site tag (gtag.js) - Google Analytics