美文网首页
快速排序

快速排序

作者: 江北晓白 | 来源:发表于2019-10-07 19:13 被阅读0次

八大排序(有没有想起八大派围战光明顶的视角)中,要说面试中问到最多的排序,非快排莫属,快排的思想这里不再重复多说,作为基础中的基础,具体原理出门左转,见度娘!
具体代码实现​:

public class QuictSort {

    public void sort(int [] s, int left, int right){
        int temp = s[left];
        if(left>=right){
            return;
        }
        int tempLeft = left+1;
        int tempRight = right;
        while(true){
           while(s[tempRight]>temp && tempLeft < tempRight){
               tempRight--;
           }
           if(tempLeft >= tempRight){
               break;
           }
           int t = s[tempRight];
           s[tempRight] = temp;
           temp = t;

           while(s[tempLeft] < temp && tempLeft < tempRight){
               tempLeft++;
           }
           if(tempLeft >= tempRight){
               break;
           }
           t = s[tempLeft];
           s[tempLeft] = temp;
           temp = t;
        }
        if(s[tempRight] < temp){
            int t= s[tempRight];
            s[tempRight] = temp;
            temp =t;
            tempRight--;
        }
        s[left] = temp;
        if(left+1>=right){
            return;
        }
        sort(s,left, tempRight);
        sort(s,tempRight+1, right);
    }

    public static void main(String[] args){
        int [] s={5,4,3,2,1};
        QuictSort quictSort = new QuictSort();
        quictSort.sort(s, 0, 4);
        for(int s1:s){
            System.out.println(s1+" ");
        }
    }
}

相关文章

  • 七大排序算法之快速排序

    七大排序算法之快速排序 @(算法笔记)[排序算法, 快速排序, C++实现] [TOC] 快速排序的介绍: 快速排...

  • 面试准备--排序

    堆排序 快速排序(simple) 快速排序(regular) 归并排序 Shell排序 插入排序 选择排序 冒泡排序

  • 排序

    插入排序 选择排序 冒泡排序 归并排序 快速排序* 三路快速排序

  • 算法笔记01-排序#2

    快速排序敢叫快速排序,那它一定得快。 快速排序 概述 快速排序也是分治排序的典型,它快,而且是原地排序。不过,要防...

  • PHP 实现快速排序

    导语 这篇了解下快速排序。 快速排序 快速排序(英语:Quicksort),又称划分交换排序(partition-...

  • 快速排序的Python实现

    目录 快速排序的介绍 快速排序的Python实现 快速排序的介绍 快速排序(quick sort)的采用了分治的策...

  • 数据结构与算法 快速排序

    起因:快速排序,又称分区交换排序,简称快排,之前没有了解过,抽空学习一下。 快速排序 1 快速排序 快速排序的定义...

  • 数组-快速排序

    采用快速方式对数组进行排序 快速排序百科:快速排序(Quicksort)是对冒泡排序算法的一种改进.快速排序是通过...

  • OC数据结构&算法

    更多整理资料尽在?一平米小站 目录 选择排序 冒泡排序 插入排序 快速排序 双路快速排序 三路快速排序 堆排序 选...

  • 要成功就做一百题-94

    题目名称 今天来几个排序,都是经典题目,包括带拆分的快速排序,堆排序,归并排序。 描述 快速排序快速排序核心就是分...

网友评论

      本文标题:快速排序

      本文链接:https://www.haomeiwen.com/subject/hjgmpctx.html