快速排序---算法

实现代码:排序方法实现了从小到大的排序

/**
 * @author nami404
 * * @date 2025/3/5 23:21
 */
public class QuickSort {
    public static void quickSort(int[] nums, int low, int high) {
        if (low < high) {
            int pivot = partition(nums, low, high);
            quickSort(nums, low, pivot - 1);
            quickSort(nums, pivot + 1, high);
        }
    }

    private static int partition(int[] nums, int low, int high) {
        int pivot = nums[high];
        while (low < high) {
            while (low < high && nums[low] <= pivot) {
                low++;
            }
            nums[high] = nums[low];
            while (low < high && nums[high] >= pivot) {
                high--;
            }
            nums[low] = nums[high];
        }
        nums[low] = pivot;
        return low;
    }

    public static void main(String[] args) {
        int[] nums = {5, 1, 7, 3, 1, 6, 9, 4};
        quickSort(nums, 0, nums.length - 1);
        for (int i = 0; i < nums.length; i++) {
            System.out.println(nums[i] + " ");
        }
    }
}
posted @ 2025-03-06 00:00  AI未来10Y  阅读(24)  评论(0)    收藏  举报