Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Java uses the Arrays.sort () method to sort objects

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how Java uses the Arrays.sort () method to sort objects, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

Sort objects using the Arrays.sort () method

When we sort an array such as an integer array or a floating-point array, it is very simple to achieve our sorting goal, which is nothing more than a problem with the sorting algorithm. So, what if we now want to sort an array of objects based on a property value of the object?

Suppose we now have a Car type, and there is a speed property of type speed in the car class that describes the speed of the vehicle. Now we want to sort the vehicles in a Car array by speed. What should we do?

Public class Car {Speed attribute of private double speed;// vehicle public Car (double speed) {this.speed = speed;} public double getSpeed () {return speed;} public void setSpeed (double speed) {this.speed = speed;}} troublesome method

We can sort the array of objects according to the comparison results by comparing the property values of the objects. for example, if we are using bubble sorting, we need to write as follows:

For (int iVot2), and then recurs the three segments respectively. This algorithm is usually more efficient than traditional fast sorting, so it is used as a concrete implementation of sorting basic types of data in Arrays.java.

Let's take the sorting of int arrays by Arrays in JDK1.8 as an example to introduce the two-axis fast arrangement used in it:

1. Determine whether the length of the array is greater than 286.If it is larger, merge sort (merge sort) is used, otherwise 2 is executed.

/ / Use Quicksort on small arrays if (right-left

< QUICKSORT_THRESHOLD) { sort(a, left, right, true); return; } // Merge sort ...... 2.判断数组长度是否小于47,小于则直接采用插入排序(insertion sort),否则执行3。 // Use insertion sort on tiny arrays if (length < INSERTION_SORT_THRESHOLD) { // Insertion sort ...... } 3.用公式length/8+length/64+1近似计算出数组长度的1/7。 // Inexpensive approximation of length / 7 int seventh = (length >

> 3) + (length > > 6) + 1

4. Take 5 equidistant points based on experience.

/ * Sort five evenly spaced elements around (and including) the * center element in the range. These elements will be used for * pivot selection as described below. The choice for spacing * these elements was empirically determined to work well on * a wide variety of inputs. * / int e3 = (left + right) > 1; / / The midpoint int e2 = e3-seventh; int e1 = e2-seventh; int e4 = e3 + seventh; int e5 = e4 + seventh

5. Insert and sort the five elements

/ / Sort these elements using insertion sort if (a [e 2] < a [e 1]) {int t = a [e 2]; a [e 2] = a [e 1]; a [e 1] = t;} if (a [e 3] < a [e 2]) {int t = a [e 3]; a [e 3] = a [e 2]; a [e 2] = t; if (t < a [e 1]) {a [e 2] = a [E1]; a [e 1] = t }} if (a [e4] < a [e3]) {int t = a [e4]; a [e4] = a [e3]; a [e3] = t; if (t < a [e2]) {a [e3] = a [e2]; a [e2] = t; if (t < a [E1]) {a [e2] = a [E1]; a [E1] = t } if (a [E5] < a [E4]) {int t = a [E5]; a [E5] = a [E4]; a [E4] = t; if (t < a [E3]) {a [E4] = a [E3]; a [E3] = t; if (t < a [e2]) {a [e3] = a [e2]; a [e2] = t If (t < a [E1]) {a [e2] = a [E1]; a [E1] = t;}}

6. Select a [e2] and a [e4] as pivot1,pivot2 respectively. Because step 5 is sorted, there must be a pivot1

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development