【基础排序】快速排序: 递归与非递归

算法原理

选定一个枢轴值Pivot,设定两个指针Low和High,Low从数组左端开始向右扫描,High从数组右端开始向左扫描,Low跳过所有比Pivot小的元素值,High跳过所有比Pivot大的值,如果二者无法执行上述操作时(Low遇到比Pivot大的值,High遇到比Pivot小的值)交换两指针指向的元素位置,然后继续上述操作,这样就会把数组分为两个部分,一部分比枢轴值大、一部分比枢轴值小。

Coding实现

递归方式:

/**
 * @author ChengXX
 */
public class QuickSort implements Sort<Integer> {

    @Override
    public void sort(Integer[] array) {
        quickSort(array,0,array.length-1);
    }

    public static void quickSort(Integer[] array,int start,int end){
        if(start>=end){
            return;
        }
        if(null==array){
            return;
        }
        if(start<0||start>=array.length){
            return;
        }
        if(end<0||end>=array.length){
            return;
        }
        int pivot=partion(array,start,end);
        quickSort(array,start,pivot-1);
        quickSort(array,pivot+1,end);
    }

    public static int partion(Integer[] array,int left,int right){
        int tmp=array[left];
        int i=left;
        int j=right;
        while (i<j){
            while (i<j&&array[j]>=tmp){
                j--;
            }
            array[i]=array[j];

            while (i<j&&array[i]<tmp){
                i++;
            }
            array[j]=array[i];
        }
        array[i]=tmp;
        return i;
    }
}

非递归实现:

import java.util.Stack;

public class SortUtils {

    public static void qucikSort(Integer[] data) {
        QuickSort.sort(data);
    }

    static class QuickSort {
        public static void sort(Integer[] data) {
            if (data == null) {
                return;
            }
            Stack<Integer> stack = new Stack<>();
            stack.push(0);
            stack.push(data.length);
            while (!stack.isEmpty()) {
                int right = stack.pop();
                int left = stack.pop();
                // 至少需要两个元素
                if (right - left > 1) {
                    int p = left + (right - left) / 2;
                    p = partition(data, p, left, right);
                    stack.push(left);
                    stack.push(p);
                    stack.push(p + 1);
                    stack.push(right);
                }
            }
        }

        public static Integer partition(Integer[] data, int p, int left, int right) {
            int l = left;
            int h = right - 2;
            // 枢轴值
            int pivot = data[p];
            // 枢轴位置
            swap(data, p, right - 1);
            while (l < h) {
                // 跳过所有比枢轴值小的元素
                if (data[l] <= pivot) {
                    l++;
                }
                // 跳过所有比枢轴值大的元素
                else if (data[h] >= pivot) {
                    h--;
                }
                // 需要交换元素值
                else {
                    swap(data, l, h);
                }
            }
            // 最后枢轴元素位置
            int idx = h;
            if (data[h] < pivot) {
                idx = h + 1;
            }
            // 枢轴元素归位
            swap(data, idx, right - 1);
            return idx;
        }
    }

    public static void swap(Integer[] data, int i, int j) {
        int tmp = data[i];
        data[i] = data[j];
        data[j] = tmp;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Teamo.Q

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值