Home  >  Article  >  Java  >  Implement and improve Java's quick sort algorithm

Implement and improve Java's quick sort algorithm

王林
王林Original
2024-02-18 21:37:081034browse

Implement and improve Javas quick sort algorithm

Java Quick Sort Algorithm Implementation and Optimization

Quick sort is a classic sorting algorithm that is widely used in practical applications. This article will introduce the implementation of the quick sort algorithm in Java and improve the efficiency of the algorithm through optimization.

  1. Quick sort algorithm principle
    Quick sort adopts the idea of ​​​​divide and conquer. The basic idea is to divide the sequence to be sorted into two parts through a "benchmark", one part is smaller than the benchmark, and the other part is greater than the baseline, and then the two parts are quickly sorted recursively, and finally the entire sequence is ordered.

The specific implementation process is as follows:

  • Select a reference element, usually the first element of the sequence.
  • Set two pointers low and high to point to the head and tail of the sequence respectively.
  • Start from high, search forward to find an element smaller than the baseline, and move it to the low position; then start from low, search backward to find an element larger than the baseline, and move it to the high position.
  • Repeat the above process until low and high meet.
  • Put the reference element into the meeting position. At this time, the elements to the left of the reference element are smaller than it, and the elements to the right are greater than it.
  • Recursively call quick sort on the left and right parts of the reference element.
  1. Java implementation of quick sort algorithm
    The following is a sample code for implementing the quick sort algorithm in Java:
public class QuickSort {

    public static void quickSort(int[] arr, int low, int high) {
        if (low < high) {
            int pivot = partition(arr, low, high); // 基准元素的位置
            quickSort(arr, low, pivot - 1); // 对基准元素左边的子序列进行快速排序
            quickSort(arr, pivot + 1, high); // 对基准元素右边的子序列进行快速排序
        }
    }

    public static int partition(int[] arr, int low, int high) {
        int pivot = arr[low]; // 选择第一个元素作为基准元素
        while (low < high) {
            while (low < high && arr[high] >= pivot) {
                high--;
            }
            arr[low] = arr[high]; // 将比基准小的元素移到低端

            while (low < high && arr[low] <= pivot) {
                low++;
            }
            arr[high] = arr[low]; // 将比基准大的元素移到高端
        }
        arr[low] = pivot; // 基准元素放入相遇的位置
        return low; // 返回基准元素的位置
    }

    public static void main(String[] args) {
        int[] arr = {6, 1, 9, 0, 4, 7, 8, 2, 5, 3};
        quickSort(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(arr));
    }
}

The above is the basic implementation of the quick sort algorithm , you can test the sorting results through the main method.

  1. Optimization of quick sort algorithm
    Although the quick sort algorithm has high efficiency on average, in some cases, its efficiency will decrease. In order to improve the efficiency of the quick sort algorithm, the following optimization measures can be taken:
  • Randomly select the reference element: In order to avoid that the selected reference element happens to be the maximum or minimum value in the sequence, you can randomly select Choose datum elements to reduce this possibility.
  • Three-number middle method: The selection of the reference element can be achieved through the middle value of the three numbers in the sequence. Select the head, middle and tail elements of the sequence, arrange them in order from small to large, and take the middle value as the base element.

Through the above optimization, the time complexity of the quick sort algorithm under special circumstances can be reduced and the efficiency of the quick sort algorithm can be improved.

Summary:
This article introduces the implementation and optimization of the quick sort algorithm in Java. The quick sort algorithm divides the sequence by selecting reference elements, and then recursively sorts the divided subsequences to finally obtain an ordered sequence. The efficiency of the quick sort algorithm can be further improved by randomly selecting benchmark elements or using optimization measures such as the three-number method.

The above is the detailed content of Implement and improve Java's quick sort algorithm. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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