Java basic algorithm of quick sort (Quick Sort)

1. Algorithm introduction (recursive)

  1. First confirm a cutoff value (Use the middle number here), and then divide the array into two parts by the boundary value.
  2. Use the left and right pointers to traverse, the left pointer points to the data greater than the cutoff value, the right pointer points to the data less than the cutoff value, and then the left and right pointers exchange data. After the exchange, the left pointer moves forward and the right pointer moves backward
  3. Put the data smaller than the cut-off value to the left of the cut-off value, and place the data larger than the cut-off value to the right of the cut-off value.
  4. Then take a cutoff value on each of the left and right, and also put the data less than the cutoff value to the left of the cutoff value, and put the data greater than the cutoff value to the right of the cutoff value.

2. Example demonstration

Original array: [117, 101, 106, 100, 112, 60, 90, 110]

Cutoff value: 100

The left pointer points to: 117

Right pointer points: 60

Then exchange

After the exchange: [90, 101, 106, 100, 112, 60, 117, 110]

Repeat the execution, knowing that the left side of the cutoff value is less than the cutoff value and the right side of the cutoff value is greater than the cutoff value.

Execution result: [90, 60, 100, 106, 112, 101, 117, 110]

Now the left pointer points to the subscript: 3

Now the right pointer points to the subscript: 2

Then repeat the above process with the [0, left pointer-1] array

Then repeat the above process with the [right pointer + 1, 7] array

See diagram for details

3. Diagram

Quick sort diagram

4. Code implementation

package sort;

import java.util.Arrays;

/**
 * <p>
 *
 * </p>
 *
 * @author: D
 * @since: 2020/9/10
 * @version: 1
 */
public class QuickSortDemo {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = {
    
    117, 101, 106, 100, 112, 60, 90, 110};
        quickSort(arr, 0, arr.length - 1);
        System.out.println("排序后的结果: " + Arrays.toString(arr));
    }

    private static void quickSort(int[] arr, int left, int right) {
    
    
        System.out.println();
        System.out.printf("遍历的数组范围[%d, %d]", left, right);
        //左右下标
        int leftIndex = left;
        int rightIndex = right;
        int pivot = arr[(left + right) / 2];
        System.out.println();
        System.out.println("基准值 = " + pivot);
        int temp;
        //让比pivot小的放到左边,大的放到右边
        while (leftIndex < rightIndex) {
    
    
            //左边寻找大于等于pivot的值才推出
            while (arr[leftIndex] < pivot) {
    
    
                leftIndex++;
            }
            //右边边寻找小于等于pivot的值才推出
            while (arr[rightIndex] > pivot) {
    
    
                rightIndex--;
            }
            //如果成立 说明pivot左右两边的值已经是小于等于大于小于pivot的值
            if (leftIndex >= rightIndex) {
    
    
                break;
            }
            System.out.println("交换前结果: " + Arrays.toString(arr));
            System.out.println("当前左指针:" + leftIndex + ",指向的数据:" + arr[leftIndex]);
            System.out.println("当前右指针 = " + rightIndex+ ",指向的数据:" + arr[rightIndex]);
            // 左指针没有超过右指针 然后将左指针和右指针进行交换
            //交换
            temp = arr[leftIndex];
            arr[leftIndex] = arr[rightIndex];
            arr[rightIndex] = temp;

            //如果交换完后发现arr[leftIndex] == pivot的值 将右指针前移
            if (arr[leftIndex] == pivot) {
    
    
                System.out.println("交换完后发现arr[rightIndex] == pivot的值 将左指针后移");
                rightIndex--;
            }
            //如果交换完后发现arr[rightIndex] == pivot的值 将左指针后移
            if (arr[rightIndex] == pivot) {
    
    
                System.out.println("交换完后发现arr[rightIndex] == pivot的值 将左指针后移");
                leftIndex++;
            }

            System.out.println("交换的结果: " + Arrays.toString(arr));
            System.out.println("交换后左指针 = " + leftIndex);
            System.out.println("交换后右指针 = " + rightIndex);
        }
        System.out.println("之后的左右指针");

        //左右指针相同 将右指针前移 左指针后移
        if (leftIndex == rightIndex) {
    
    
            System.out.println("左右指针相同 将右指针前移 左指针后移");
            leftIndex++;
            rightIndex--;
        }
        System.out.println("leftIndex:" + leftIndex);
        System.out.println("rightIndex:" + rightIndex);
        System.out.println("left: " + left);
        System.out.println("right: " + right);

        // 两个条件都不满足则排序完成
        if (left < rightIndex) {
    
    
            quickSort(arr, left, rightIndex);
        }
        if (leftIndex < right) {
    
    
            quickSort(arr, leftIndex, right);
        }
    }
}

5. Execution results

image-20200911002655374

image-20200911002706490

image-20200911002717204

image-20200911002726689

6. Other sorting

Selection Sort (Selection Sort)
Insertion Sort (Insertion Sort)
Hill Sort (Shell Sort)
Bubble Sort (Bubble Sort)
Heap Sort (Heap Sort)

Guess you like

Origin blog.csdn.net/d875708765/article/details/108525947