Write a quick sort algorithm in Java in 5 seconds? I can do this

Quick sort is a very efficient sorting algorithm proposed by British computer scientist Hall in 1960. Its basic idea is to select a benchmark element to divide the array to be sorted into two parts. All elements in one part are smaller than the benchmark element, and all elements in the other part are larger than the benchmark element. Then the two parts are quickly sorted separately. The entire sorting process can be performed recursively.

The main steps of this algorithm are:

  1. Select an element as the pivot.
  2. Divide the array into two parts: one containing all elements less than the pivot, and the other containing all elements greater than or equal to the pivot. This process is called a partition operation.
  3. Recursively sort these two subarrays

Here is a basic example of quick sort in Java generated by FuncGPT (FuncGPT):

// 类名: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 will select a base element and then recursively sort the subarrays to the left and right of the base 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). Of course, in actual use, we also need to consider some other factors, such as how to choose a good basis element (this usually affects the efficiency of sorting), and how to handle large arrays when memory is limited (this usually affects the program stability). And FuncGPT (FuncGPT), which was born using best practices in coding and large-scale machine joint training, has already helped you achieve this. We put the above code into Wenxinyiyan, which is comparable to ChatGPT-4, and the evaluation we received is: This Java code implements a quick sorting algorithm with a clear structure, easy to understand and use (see screenshot for details).

 

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. It is currently developed and used for free, download link: https://suo.im/76V9w

Lei Jun: The official version of Xiaomi's new operating system ThePaper OS has been packaged. The pop-up window on the lottery page of Gome App insults its founder. Ubuntu 23.10 is officially released. You might as well take advantage of Friday to upgrade! Ubuntu 23.10 release episode: The ISO image was urgently "recalled" due to containing hate speech. A 23-year-old PhD student fixed the 22-year-old "ghost bug" in Firefox. RustDesk remote desktop 1.2.3 was released, enhanced Wayland to support TiDB 7.4 Release: Official Compatible with MySQL 8.0. After unplugging the Logitech USB receiver, the Linux kernel crashed. The master used Scratch to rub the RISC-V simulator and successfully ran the Linux kernel. JetBrains launched Writerside, a tool for creating technical documents.
{{o.name}}
{{m.name}}

Guess you like

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