Java beauty [from cainiao to expert drill] Arrays class and its method analysis, javaarrays

Source: Internet
Author: User

Java beauty [from cainiao to expert drill] Arrays class and its method analysis, javaarrays

Author: erqing

Personal site: zhangerqing.cn mail: xtfggef@gmail.com Weibo: http://weibo.com/xtfggef

This chapter describes the key methods of the java. util. Arrays class, including how to use and implementation principles. This is an algorithm class. It mainly assists the array class in sorting, searching, and other functions. It also supports array-to-List conversion. This chapter describes the Arrays class in the Java [from cainiao to expert drill] series and its method analysis. If you have any questions, please contact me through any of the above methods!

Sort

This article uses JDK 1.8.0 _ 25 for testing. Please pay attention to the differences in versions, because different JDK implementations are slightly different. Arrays. the sort () method sorts the basic data types by dualcomputquicksort (Multiple-way fast sorting), and uses MergeSort (Merge Sorting) to sort the arrays of reference types, next we will discuss these two types of sorting algorithms respectively.

Sorting of basic types of Arrays

Eight basic data types in Java, except boolean, can be sorted with other seven types. If an array is of a single basic type, such as int [] data, long data [] can directly use Arrays. sort by sort. For all basic types that can be sorted, dualpolictquicksort is used for sorting. First, let's look at an example:

package com.adam.java.algo.arrays;import java.util.Arrays;import org.junit.Test;public class ArraysBasicTest {@Testpublic void testSortInteger() {int data[] = { 10, 8, 9, 1, 2, 5, 98, 3, 7, 66 };Arrays.sort(data);for (int i : data) {System.out.print(i + " ");}}@Testpublic void testSortChar() {char data[] = { 'D', 'B', 'E', 'C', 'H', 'A', 'Y', 'G', 'I', 'O' };Arrays.sort(data);for (char i : data) {System.out.print(i + " ");}}}

Output:

1 2 3 5 7 8 9 10 66 98

A B C D E G H I O Y

Here we should look at the algorithm used by Arrays. sort (). Let's look at the JDK source code.

/**     * Sorts the specified array into ascending numerical order.     *     * <p>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.     *     * @param a the array to be sorted     */    public static void sort(int[] a) {        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);    }

It is found that all the basic sorting operations are directly performed using dualcomputquicksort. sort (). Therefore, we need to understand the principles of dualcomputquicksort. The basic idea is:

If the number of elements is from 1 to 47, insert sorting is directly used for sorting.

If the number of elements ranges from 47 to 286, multiple methods are used for fast sorting.

If the number of elements is greater than 286, Merge Sorting is used.

Here we will study the principle of multi-path fast sorting. First, let's review how the classic fast sorting is implemented.


Implementation of classic kubernetes:

1. Find a central axis. Generally, select the first element of the array.

2. define two pointers, pointing to the leftmost and rightmost, respectively, and traverse from the right. If the value is greater than the central axis, the right pointer is shifted to the left. If the value is smaller than the central axis, the position is swapped; similarly, when compared from the left and the middle axis, if the value is smaller than the middle axis, the pointer moves to the right. If the value is greater than the middle axis, the position is swapped.

3. After sorting, the left side of the central axis is smaller than the central axis, and the right side is greater than the central axis.

4. Recursive Sub-arrays are subjected to the above operations, which are unstable, with the optimum time complexity O (nlogn), and the worst case is O (n2) in basic order ). For more information, see another blog post.


How to implement multiple routes:

1. Select two central axes P1 and P2.

2. Assume P1 <P2; otherwise, it is exchanged.

3. during the process, the original array is divided into four parts: smaller than the central axis 1, greater than the central axis 2, between the two central axes, unordered parts (except the first two central axes, other elements belong to this Part ).

4. After the start, select a number from the unsorted part, compare it with the two central axes, and place it in the appropriate position until there is no data in the unsorted part, and end the sorting.

5. Recursive processing of sub-arrays, stable sorting, and stable time complexity as O (nlogn ).


Sorts arrays of reference types.

For example, sort the array of User type by age. The Comparator interface is used here,

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.