ARRAYS class learning

xiaoxiao2021-03-06  169

Always encounter non-stop in the forum and life

Java

Sort and find the algorithm, ask where there is a source code, huh, huh. in fact

Java

It itself provides a good class, that is

Arrays

.

Arrays

Affiliated book

The Collections Framework

. This class provides an array of fills, lookups, comparisons, and sorting a series of operations.

One fill:

Arrays.Fill (Type [] A, Type Val series method is to give, array filled. It is simple to fill in all or a certain data of a number of data into a special value.

?

Second look:

Binarysearch (Type [] A, Type Key series method is to find a specific Key element with 2 points in a type of array, return element numbers. The premise is that this array is sorted. If there are multiple elements and keys values, it cannot be expected, which one is returned. For the return value, there is the following rules, return value <0 means not found, return value> = 0 shows it.

For insertion, arrays does not have a special algorithm, all of the inserts of the array are converted to collection, but binarysearch can help you find an array of insert points, the insert point position is the absolute value of the return value minus 1 (| Return Value | - 1).

For the array of sorts, the algorithm is quite fast with 2 points, huh, huh.

?

Three comparison:

Equals (Type [] A, Type [] B) The series method is to do two arrays, equally return to TRUE. When this method is used, some places should pay attention. When comparing two float arrays, the program is not used for each element, and the program is not used to determine, but use new float (f1) .Equals (new float (f2)), this method thinks Nan is equal to itself, 0.0F is not equal to -0.0F. The same is true for Double arrays.

For Object [] arrays, it is used (E1 == NULL? E2 == NULL: E1.Equals (E2)).

?

Four sort:

The Sort (Type [] A) series is sorted to the array. The sorting algorithm for SUN is to adjust the rapid sorting method. It is Jon L. Bentley and M. Douglas MCILROY's "Engineering A Sort Function", Software-Practice and Experience, Vol. 23 (11) P. 1249-1265 (November 1993) ). This algorithm is sorted with a large amount of data to be n * o (n), and only the O (N1OGN) time is required.

About rapid sorting method Reference: (http://algorithm.myrice.com/algorithm/commonalg/sort/internal_sorting/quick_sort/quick_sort.htm Quick Sort

We already know that under the decision tree calculation model, any of the sorting algorithms based on comparison to determine the relative position of the two elements require Ω (nlogn) calculation time. If we can design a sorting algorithm that requires an O (N1OGN) time, this sorting algorithm is optimal in a gradual sense. Many sort algorithms are pursuing this goal. Rapid Sort Algorithm It requires an O (nlogn) time in average. This algorithm is invented by c.a.r.hoare. )

?

The conversion of the five arrays:

Use arrays.aslist (Object [] a) to implement array to ArrayList conversion. At the same time, collect.toArray () can make the data of some collection types into arrays.

?

Six talks:

View the discovery of the source code: When comparison of Double types, in equal cases, the source code species of Sun makes further comparison. Example 1:

??? IF (a [i] == 0 && double.douBletolongbits (a [i]) == NEG_ZERO_BITS) {

Example 2: ????? IF (MIDVAL

??????????????? cmp = -1; ?? // neither Val IS

Nan

, Thissal Is Smaller

???????????} else if (midVal> key) {

??????????????? CMP = 1; ??? // neither Val IS

Nan

Thissal is larger

???????????} else {

??????????????? long midbits = double.douBletolongBits (MIDVAL);

??????????????? long keybits = double.douBletolongBits (key);

??????????????? CMP = (midbits == keybits ?? 0: // VALUES ARE Equal

?????????????????????? (MidBits

Nan

)

??????????????????????? 1));???????????????????? // ( 0.0, -0.0) OR (nan,!

Nan

)

???????????}

It seems that if you use == ratio from precision, there is a problem.

In addition, more than one method is more than the ordinary type arrays for finding and sorting of the target array: public static int binarysearch (object [] a, object key, comparator C)

Public Static Void Sort (Object [] A, Comparator C)

This place comparator is the algorithm you define for objects. When running, the comparison agent to compare the object to the COMPARATOR PUBLIC INT COMPARE (Object? O1, Object? O2) is an interface, the specific implementation is your own The algorithm is.

?

[references]

Javatm 2 Platform, Standard Edition, V 1.3.1 API Specification

JDK source code

http://algorithm.myrice.com/algorithm/commonalg/sort/internal_sorting/quick_sort/quick_sort.htm

转载请注明原文地址:https://www.9cbs.com/read-128335.html

New Post(0)