`
leonzhx
  • 浏览: 767992 次
  • 性别: 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

    Two Efficient Algorithms for Linear Suffix Array Construction 生物信息论文

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

    主要介绍了Java Arrays.sort和Collections.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'...

    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 ...

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

    主要介绍了JAVA基于Arrays.sort()实现数组升序和降序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    javaarrays的使用.pdf

    javaarrays的使用.pdf

    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 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. ...

    java arrays类.docx

    在Java中,Arrays类是一个实用工具类,用于在数组上执行各种操作,包括排序、搜索、比较等。它提供了一组静态方法,以便在数组中进行常见的操作。下面是一个超级详细的介绍Java中Arrays类的常用方法和功能。

    Java Arrays工具类用法详解

    主要介绍了Java Arrays工具类用法,结合实例形式分析了java Arrays工具类针对数组元素修改、复制、排序等操作使用技巧与相关注意事项,需要的朋友可以参考下

    Java中Arrays类详解.docx

    java arrays类

    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 ...

    Data Structures and Algorithms in Java, 5th Edition (Part 2/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...

Global site tag (gtag.js) - Google Analytics