Quick sort algorithm, written like this, beats 95% of programmers

In 1960 , British computer scientist Hall proposed an efficient sorting algorithm-quick sort. The core idea is to select a reference element and split the array to be sorted into two parts. Some of them are smaller than the base element, and some are larger than the base element. Then the two parts are quickly sorted respectively, and finally the entire sorting process is completed through recursion. This algorithm is highly efficient and widely used.

The main steps of this algorithm include:

1. Before selecting a pivot element, a decision needs to be made.

2. Divide the array into two parts: one part contains all elements that are less than the pivot, and the other part contains all elements that are greater than or 3. equal to the pivot. This step is called partitioning.

Sort the two subarrays recursively.

The following is a basic example of using Java to implement quick sort using FuncGPT (intelligent function) generated by the full-stack, fully-automatic software development tool soflu software robot :

// 类名:QuickSort

// 函数名:quickSort

// 函数功能:使用快速排序算法对数组进行排序

// POM依赖包:无



public class QuickSort {



    /**

     * 使用快速排序算法对数组进行排序

     * @param arr 待排序的数组

     */

    public static void quickSort(int[] arr) {

        if (arr == null || arr.length <= 1) {

            return;

        }

        sort(arr, 0, arr.length - 1);

    }



    /**

     * 快速排序的递归实现

     * @param arr 待排序的数组

     * @param low 左边界

     * @param high 右边界

     */

    private static void sort(int[] arr, int low, int high) {

        if (low < high) {

            int pivot = partition(arr, low, high); // 获取基准值的索引

            sort(arr, low, pivot - 1); // 对基准值左侧的子数组进行排序

            sort(arr, pivot + 1, high); // 对基准值右侧的子数组进行排序

        }

    }



    /**

     * 快速排序的分区实现

     * @param arr 待排序的数组

     * @param low 左边界

     * @param high 右边界

     * @return 选择的基准值的索引

     */

    private static int partition(int[] arr, int low, int high) {

        int pivot = arr[low]; // 选择第一个元素作为基准值

        int i = low + 1;

        int j = high;



        while (i <= j) {

            while (i <= j && arr[i] <= pivot) {

                i++;

            }



            while (i <= j && arr[j] > pivot) {

                j--;

            }



            if (i < j) {

                swap(arr, i, j);

            }

        }



        swap(arr, low, j); // 将基准值放到正确的位置上



        return j;

    }



    /**

     * 交换数组中两个位置的元素

     * @param arr 数组

     * @param i 位置1

     * @param j 位置2

     */

    private static void swap(int[] arr, int i, int j) {

        int temp = arr[i];

        arr[i] = arr[j];

        arr[j] = temp;

    }



    public static void main(String[] args) {

        int[] arr = new int[]{9, 5, 1, 8, 3, 7, 4, 2, 6};

        quickSort(arr);

        for (int num : arr) {

            System.out.print(num + " ");

        }

    }

}

```



// 函数示例

// 使用快速排序算法对数组进行排序示例

// 入参:arr,待排序的数组

// 出参:无,直接对输入的数组进行排序

// 调用示例:

// int[] arr = new int[]{9, 5, 1, 8, 3, 7, 4, 2, 6};

// QuickSort.quickSort(arr);

// 输出结果:例如,对数组[9, 5, 1, 8, 3, 7, 4, 2, 6]进行排序

// 排序后的数组为[1, 2, 3, 4, 5, 6, 7, 8, 9]

// 则输出结果为:1 2 3 4 5 6 7 8 9

```

// 温馨提示:以上代码由 FuncGPT 生成,编译打包请使用 QuickSort.java 文件。

The main functions in this code include:

  1. quickSort(int[ ] arr):  This is the entry function of quick sort. It accepts an integer array as a parameter and sorts it. If the input array is empty or contains only one element, this function will return directly.
  2. sort(int[ ] arr, int low, int high):  This is a recursive function used to sort subranges of an array. If the left boundary low  is smaller than the right boundary high, it selects a pivot element and then recursively sorts the subarrays to the left and right of the pivot element.
  3. partition(int[ ] arr, int low, int high):  This function is used to implement the partition operation in quick sort. It selects a base element in the array and then moves other elements in the array to the left or right of the base element. In this process, elements smaller than the base element will be moved to the left of the base element, and elements larger than the base element will be moved to the right of the base element. This function returns the position of the reference element in the sorted array.
  4. swap(int[ ] arr, int i, int j):  This function is used to swap elements at two positions in the array. In the main function, an array to be sorted is created, then the quickSort function is called to sort it, and finally the sorted array is printed. If you want to know what the sorted result is, you can run this code and look at the console output. In this example, the input array is [9, 5, 1, 8, 3, 7, 4, 2, 6]. After quick sorting, the output result is [1, 2, 3, 4, 5, 6, 7, 8, 9].

The above is the basic process of a quick sort algorithm written in Java through FuncGPT (Hui function). We put the above code into Wen Xinyiyan, and the evaluation we got was: This Java code implements a quick sorting algorithm with a clear structure, easy to understand and use (see screenshot for details).

https://oscimg.oschina.net/oscnet/up-f9ad9cc02d252071b9ba7f764af41457041.png

 

As an important part of the full-stack, fully-automatic software development tool SoFlu software robot, FuncGPT supports the creation of all types of functions. Use natural language to describe Java function requirements and generate high-quality, highly readable Java function code in real time. The generated code can be directly copied to IDEA, or imported into the Java fully automatic development tool function library with one click. Follow the [SoFlu software robot] public account to apply for a free trial.

Spring Boot 3.2.0 is officially released. The most serious service failure in Didi’s history. Is the culprit the underlying software or “reducing costs and increasing laughter”? Programmers tampered with ETC balances and embezzled more than 2.6 million yuan a year. Google employees criticized the big boss after leaving their jobs. They were deeply involved in the Flutter project and formulated HTML-related standards. Microsoft Copilot Web AI will be officially launched on December 1, supporting Chinese PHP 8.3 GA Firefox in 2023 Rust Web framework Rocket has become faster and released v0.5: supports asynchronous, SSE, WebSockets, etc. Loongson 3A6000 desktop processor is officially released, the light of domestic production! Broadcom announces successful acquisition of VMware
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4868096/blog/10295403