C++ 关于const和函数
2012-11-03 19:54
337 查看
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.
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.
相关文章推荐
- c++关于const和函数
- 关于C++的右值、const引用、右值引用、const成员函数及相关扩展
- C++关于参数为const引用类型的函数调用参数成员函数的一个问题
- C++关于类成员函数在参数列表后加const的作用,以及使用条件
- 关于c++中的const ,包括void fool() const;这个函数中的const的作用
- c++ 函数形参关于const和引用
- 关于c++ const 函数参数
- C++ 关于static_cast reinterpret_cast dynamic_cast 以及 const_cast 用于类型转换的函数使用 20180315 day7
- 【C/C++学院】(4)c++开篇/类和对象/命名空间/类型增强/三目运算符/const专题/引用专题/函数增强
- c++ 深拷贝 函数后加const
- 【C++基础】const成员函数
- 关于c++中字符串的系统函数
- c++ const: const 变量, const 输入参数, const 返回值, const 成员函数
- 关于C++ const 的全面总结(转载)
- 关于C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性。
- C++关于函数声明和定义中参数赋值为0调用问题演示
- 关于“C++ vs2010中声明一个类的函数,提示不兼容”问题
- 关于c++的虚函数的一些总结
- 关于const 与 引用 在函数传参的时候 区别
- 关于c++和C的getXXX系列函数