Quick sort [Java algorithm]

1. Concept

Quick sort is a relatively efficient sorting algorithm. It adopts the idea of ​​​​"divide and conquer" to achieve sorting through multiple comparisons and exchanges. In one sorting pass, the data to be sorted is divided into two independent parts, and the two parts are processed. Sorting makes all the data in one part smaller than the other part, and then continues to recursively sort the two parts, finally achieving all data in order.

2. Ideas

① Given a set of sequences to be sorted,We take the first element of the sequence as the base value. What is the baseline value? It's just like a reference. Then take two more sentinels. What are sentinels? Let’s think of it as a pointer;

② So what is the order of this arrangement? First, we place a sentry at the far left and right of the sequence, we call them left and right respectively,left goes from left to right to find a number that is larger than the base value, right goes from right to left to find a number that is smaller than the base value

It is stipulated that the first step should be taken by right first., we let it go all the way to the left, how far will it go? It stops until it finds a number smaller than the base value. After right stops, it is left's turn to go. In the same way, when left finds a number larger than the reference value, it also stops. Both left and right stop. Next, the values ​​of these two positions are exchanged. , the first round of mutual journey ends;

④ After the first round, there is the next round. Now they are both stopped halfway, without seeing each other, and continue walking on a long journey. In the same way, left continues++ to go right, right- - goes left. When the conditions are met, wait for both parties to stop, exchange values, and continue walking after the exchange;

⑤ All the above rushing processes are implemented through while loops. in,The outer while loop controls the two parties to continue running after exchanging values. The exit condition is the moment when left and right collide. The two inner while loops control their respective walking processes from one stopping point to another. The exit condition is when the next stopping point is found.. It is equivalent to the outer loop giving an order to start walking, so the inner loop hears the instruction and lets left and right each go to their next stop position. After exchanging values, they wait for the instruction from the outer loop before returning. next target point;

⑥ When left == right, the loop ends. We exchange the values ​​where left and right meet with the reference value. At this time, overall the numbers on the left of the reference value are smaller than the numbers on the right of the reference value, but the values ​​on the left and right sides are The ordering is messed up;

⑦ The principle is the same. At this time, we can divide the sequence into two parts from the base, and thenSort these two parts recursively, and finally achieve all data in order.

3. Code implementation

import java.util.Arrays;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = {
    
    3, 2, 9, 11, 17, 4, 13, 7, 5, 1, 12};
        int[] newArr = quickSort(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(newArr));
    }

    public static int[] quickSort(int[] arr, int low, int high) {
    
    
        if (low > high) {
    
    
            return arr;
        }
        int base = arr[low];
        int left = low;
        int right = high;
        while (left != right) {
    
    
            while (arr[right] >= base && left < right) {
    
    
                right--;
            }
            while (arr[left] <= base && left < right) {
    
    
                left++;
            }
            int temp;
            temp = arr[right];
            arr[right] = arr[left];
            arr[left] = temp;
        }
        arr[low] = arr[left];
        arr[left] = base;
        quickSort(arr, low, left - 1);
        quickSort(arr, right + 1, high);
        return arr;
    }
}

Insert image description here

Guess you like

Origin blog.csdn.net/m0_52861684/article/details/132134957