Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Arrays.sort is quicksort algorithm, why not another sort algorithm?

Tags:

Why? Is it faster or more efficient?

For systems with one core, we can use quicksort. What should we use on systems with two cores, four cores, or eight cores?

like image 269
mr. Vachovsky Avatar asked Nov 29 '10 15:11

mr. Vachovsky


People also ask

Why is Quicksort better than other sorting algorithms?

Quick sort is an in-place sorting algorithm. In-place sorting means no additional storage space is needed to perform sorting. Merge sort requires a temporary array to merge the sorted arrays and hence it is not in-place giving Quick sort the advantage of space.

Is arrays sort () a Quicksort?

Arrays. sort() does not use quick-sort. Java 7 used TimSort which is a combination of Merge Sort and Insertion Sort. Java 8 uses parallel-sort when there are more number of elements, and uses multiple threads for sorting.

Why is Quicksort better for arrays?

No other sorting algorithm performs better than Quick sort on Arrays because of the following reasons: Quick sort is an in-place sorting algorithm, i.e. which means it does not require any additional space, whereas Merge sort does, which can be rather costly.

Which sorting algorithm is used in arrays sort?

As mentioned in the official JavaDoc, Arrays. sort uses dual-pivot Quicksort on primitives. It offers O(n log(n)) performance and is typically faster than traditional (one-pivot) Quicksort implementations. However, it uses a stable, adaptive, iterative implementation of mergesort algorithm for Array of Objects.


1 Answers

Quicksort has the advantage of being completely in place, so it does not require any additional storage, while mergesort (which is actually used by Arrays.sort() for object arrays) and other (all?) guaranteed O(n*log n) algorithm require at least one full copy of the array. For programs that sort very large primitive arrays, that means potentially doubling the overall memory usage.

like image 200
Michael Borgwardt Avatar answered Sep 22 '22 01:09

Michael Borgwardt