Home > Java > javaTutorial > How to quickly sort functions in java

How to quickly sort functions in java

小老鼠
Release: 2024-01-18 17:26:18
Original
1317 people have browsed it

Quick sorting method: 1. Create a Java sample file; 2. Implement the quick sorting algorithm through the quickSort method; 3. Select an element in the array as the pivot (pivot) and divide the array into two sub- Array, one contains elements smaller than the pivot element, and the other contains elements larger than the pivot element, and then recursively apply the quick sort algorithm to the two sub-arrays; 4. Sort the array in the main method and output the result. .

How to quickly sort functions in java

Operating system for this tutorial: Windows 10 system, Dell G3 computer.

The following is the sample code to implement the quick sort algorithm in Java:

public class QuickSort {
    public static void quickSort(int[] arr, int low, int high) {
        if (arr == null || arr.length == 0) {
            return;
        }
        if (low >= high) {
            return;
        }
        int middle = low + (high - low) / 2;
        int pivot = arr[middle];
        int i = low, j = high;
        while (i <= j) {
            while (arr[i] < pivot) {
                i++;
            }
            while (arr[j] > pivot) {
                j--;
            }
            if (i <= j) {
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
                i++;
                j--;
            }
        }
        if (low < j) {
            quickSort(arr, low, j);
        }
        if (high > i) {
            quickSort(arr, i, high);
        }
    }
    public static void main(String[] args) {
        int[] arr = {5, 2, 8, 3, 9, 1};
        quickSort(arr, 0, arr.length - 1);
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}
Copy after login

In the above In the example, the quickSort method implements the quick sort algorithm. This algorithm selects an element in the array as the pivot and divides the array into two subarrays, one containing elements smaller than the pivot and the other containing elements larger than the pivot. The quicksort algorithm is then applied recursively to the two subarrays. Finally, the array is sorted and the results are output in the main method.

The above is the detailed content of How to quickly sort functions in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template