快速排序的java代码实现

import java.util.Arrays;

/**
 * @author mccrea
 * @version 1.0
 * @description: 快速排序(递归方法)
 * @date 2020/9/18 22:20
 */
public class QuickSort {

    public static void main(String[] args) {
        int[] arr= new int[] {9, 3, 4, 5, 1, 3, 6, 2, 8, 4, 5, 7, -4};
        quickSort(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(arr));
    }

    /** 
     * @description: 快速排序
     * @param arr 待排序数组
     * @param startIndex 数组起始位置
     * @param endIndex 数组结束位置
     * @return: void 
     * @author mccrea
     * @date: 2020/9/18 22:48
     */ 
    public static void quickSort(int[] arr, int startIndex, int endIndex) {
        // 结束条件
        if (startIndex >= endIndex) {
            return;
        }
        // 获得基准元素索引
//        int pivotIndex = partitionFill(arr, startIndex, endIndex);
        int pivotIndex = partitionExchange(arr, startIndex, endIndex);
        // 处理左边数组
        quickSort(arr, startIndex, pivotIndex - 1);
        // 处理右边数组
        quickSort(arr, pivotIndex + 1, endIndex);
    }
    /**
     * @description:  填坑法
     * @param arr 待处理数组
     * @param startIndex 待处理的起始位置
     * @param endIndex 待处理的结束位置
     * @return: int 基准点索引
     * @author mccrea
     * @date: 2020/9/18 22:25
     */
    private static int partitionFill(int[] arr, int startIndex, int endIndex) {
        // 取左边第一个元素作为基准元素
        int pivot = arr[startIndex];
        // 左边指针
        int left = startIndex;
        // 右边指针
        int right = endIndex;
        // 基准元素指针
        int pivotIndex = startIndex;
        // 左右指针重合或者交错时循环结束
        while (left <= right) {
            // 先看右边部分
            while (left <= right) {
                // 右边数比基准元素小的时候填坑
                if (arr[right] < pivot) {
                    // 基准指针位置存于该小于基准元素的值
                    arr[pivotIndex] = arr[right];
                    // 基准指针指向右索引
                    pivotIndex = right;
                    // 左索引右移
                    left ++;
                    break;
                }
                right --;
            }
            // 再看左边部分
            while (left <= right) {
                // 左边数比基准元素大的时候填坑
                if (arr[left] > pivot) {
                    // 基准指针位置存于该大于基准元素的值
                    arr[pivotIndex] = arr[left];
                    // 基准指针指向左边索引
                    pivotIndex = left;
                    // 右索引左移
                    right --;
                    break;
                }
                left ++;
            }
        }
        // 找到基准位置并存入基准值
        arr[pivotIndex] = pivot;
        return pivotIndex;
    }

    /**
     * @description: 交换法
     * @param arr 待处理数组
     * @param startIndex 起始索引
     * @param endIndex 结束索引
     * @return: int 基准下标
     * @author mccrea
     * @date: 2020/9/18 22:57
     */
    private static int partitionExchange(int[] arr,int startIndex, int endIndex) {
        // 左边第一个数为基准元素
        int pivot = arr[startIndex];
        // 左边下标
        int left = startIndex;
        // 右边下标
        int right = endIndex;
        // 交换用临时变量
        int temp;
        while (left != right) {
            // 找到右边小于等于基准元素的下标
            while (left < right && arr[right] >= pivot) {
                right --;
            }
            // 找到左边大于等于基准元素的下标
            while (left < right && arr[left] <= pivot) {
                left ++;
            }
            // 左右元素交换
            if (left < right) {
                temp = arr[left];
                arr[left] = arr[right];
                arr[right] = temp;
            }
        }
        // 给基准元素定位
        arr[startIndex] = arr[left];
        arr[left] = pivot;
        return left;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值