Home > Java > javaTutorial > Detailed explanation of Java's method of using Arrays class for array sorting

Detailed explanation of Java's method of using Arrays class for array sorting

PHPz
Release: 2023-07-25 10:13:34
Original
2419 people have browsed it

Detailed explanation of how Java uses the Arrays class to sort arrays

In Java programming, it is often necessary to sort arrays. In order to simplify the sorting process, Java provides the Arrays class, which contains some commonly used sorting methods. This article will introduce the sorting method of the Arrays class in detail and demonstrate its use through code examples.

  1. Sort methods of Arrays class

The Arrays class provides two overloaded sort methods, namely sort and parallelSort. The former is used to sort arrays serially, while the latter is used to sort arrays in parallel.

1.1 sort method

The sort method is used to serially sort the array. It has multiple overloaded methods, and different methods can be selected based on sorting requirements.

The sample code is as follows:

import java.util.Arrays;

public class ArraySortExample {
    public static void main(String[] args) {
        int[] arr = {5, 2, 9, 1, 3};
        System.out.println("排序前:" + Arrays.toString(arr));
        Arrays.sort(arr);
        System.out.println("排序后:" + Arrays.toString(arr));
    }
}
Copy after login

The running results are as follows:

排序前:[5, 2, 9, 1, 3]
排序后:[1, 2, 3, 5, 9]
Copy after login
Copy after login

1.2 parallelSort method

The parallelSort method is used to sort the array in parallel. Compared with the sort method, it can complete the sorting operation faster and is suitable for larger-scale arrays.

The sample code is as follows:

import java.util.Arrays;

public class ArrayParallelSortExample {
    public static void main(String[] args) {
        int[] arr = {5, 2, 9, 1, 3};
        System.out.println("排序前:" + Arrays.toString(arr));
        Arrays.parallelSort(arr);
        System.out.println("排序后:" + Arrays.toString(arr));
    }
}
Copy after login

The running results are as follows:

排序前:[5, 2, 9, 1, 3]
排序后:[1, 2, 3, 5, 9]
Copy after login
Copy after login
  1. Sort algorithm

The sorting method in the Arrays class is used Optimized quick sort algorithm (Dual-Pivot Quicksort), providing high performance in most cases. This algorithm is based on the idea of ​​divide and conquer, by selecting two pivot elements to divide the array into three parts: the part smaller than the pivot element, the part equal to the pivot element, and the part larger than the pivot element. Then the sorting operation is performed recursively on the divided two parts.

The time complexity of the quick sort algorithm is O(nlogn), where n is the length of the array.

  1. Notes

When using the Arrays class for array sorting, you need to pay attention to the following points:

3.1 The Comparable interface needs to be implemented

If you want to sort the object array of a custom class, the class must implement the Comparable interface and override the compareTo method. This allows for correct comparison of object sizes during sorting.

The sample code is as follows:

import java.util.Arrays;

class Student implements Comparable<Student> {
    private String name;
    private int score;

    public Student(String name, int score) {
        this.name = name;
        this.score = score;
    }

    @Override
    public int compareTo(Student o) {
        return this.score - o.score;
    }

    @Override
    public String toString() {
        return name + ": " + score;
    }
}

public class StudentSortExample {
    public static void main(String[] args) {
        Student[] students = {new Student("Tom", 90), new Student("Jerry", 80)};
        System.out.println("排序前:" + Arrays.toString(students));
        Arrays.sort(students);
        System.out.println("排序后:" + Arrays.toString(students));
    }
}
Copy after login

The running results are as follows:

排序前:[Tom: 90, Jerry: 80]
排序后:[Jerry: 80, Tom: 90]
Copy after login

3.2 Performance optimization of parallel sorting

When parallel sorting is applicable, if the array If the length is less than the default threshold (8192 used in the Arrays class), the insertion sort algorithm will be used for sorting. The insertion sort algorithm has better performance on small-scale data.

In order to further improve the performance of parallel sorting, you can force the use of the traditional merge sort algorithm by setting the system property java.util.Arrays.useLegacyMergeSort to true. This avoids the use of insertion sort in the case of parallel sorting.

The sample code is as follows:

import java.util.Arrays;

public class ArrayParallelSortPerformanceExample {
    public static void main(String[] args) {
        int[] arr = {5, 2, 9, 1, 3};
        System.out.println("排序前:" + Arrays.toString(arr));
        System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
        Arrays.parallelSort(arr);
        System.out.println("排序后:" + Arrays.toString(arr));
    }
}
Copy after login
  1. Summary

This article details the method of using the Arrays class for array sorting in Java. Arrays can be sorted serially by calling the sort method, and arrays can be sorted in parallel by calling the parallelSort method. Parallel sorting has better performance than serial sorting and is suitable for large-scale data. When using an object array of a custom class for sorting, you need to implement the Comparable interface. At the same time, by setting the java.util.Arrays.useLegacyMergeSort property to true, the performance of parallel sorting can be further optimized. Mastering the sorting method of the Arrays class can handle array sorting problems more efficiently in programming.

The above is the detailed content of Detailed explanation of Java's method of using Arrays class for array sorting. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template