编程语言
首页 > 编程语言> > 基本排序算法——快速排序

基本排序算法——快速排序

作者:互联网

package com.example.demo.sort;

import java.util.Arrays;

/**
 * @Author: wangsj
 * @Date: 2021/7/20 13:51
 */
public class QuickSort {

    public static void quickSort(int[] arr, int low, int high) {
        if (low < high) {
            int pivot = arr[low];
            int p_pos = low;
            int temp;
            for (int i = low + 1; i <= high; i++) {
                if (arr[i] > pivot) {
                    p_pos++;
                    temp = arr[p_pos];
                    arr[p_pos] = arr[i];
                    arr[i] = temp;
                }
            }
            temp = arr[low];
            arr[low] = arr[p_pos];
            arr[p_pos] = temp;

            quickSort(arr, low, p_pos -1);
            quickSort(arr, p_pos + 1, high);

        }

    }

    public static void main(String[] args) {
        int[] arr = {12,39,87,15,89,15,48,76,14,98,12,74};
        quickSort(arr, 0, arr.length -1);
        System.out.println(Arrays.toString(arr));

    }

}

标签:arr,temp,int,quickSort,pos,算法,low,排序,快速
来源: https://blog.csdn.net/weixin_46235136/article/details/122461558