美文网首页
面试题10-快速排序

面试题10-快速排序

作者: 小庄bb | 来源:发表于2017-09-03 18:39 被阅读18次

题目要求

快速排序

题目解析

思路一:

  • 分析

首先选择一个枢轴值,和两个下标值分别为low和high。
首先比较是否枢纽值与low下标所代表的值的大小,如果大于,那么检查low+1的值。
如果小于的话将low的值移动至数组右侧。
然后比较是否枢纽值与high下标所代表的值的大小,如果小于,那么检查high-1的值。
如果小于的话将low的值移动至数组左侧。
再分别在分开的两个区间进行快速排序

  • 代码段
public static int[] order(int[] data) {
        
        if(data == null || data.length == 0) {
            return null ;
        }
        
        //确定中间值为枢轴值
        int tempId = (data.length - 1 + 0 ) / 2 ;
        int temp = data[(data.length - 1 + 0 ) / 2] ;
        
        int low = 0 ;
        int high = data.length-1 ;
        
        //移动元素完成一波快速排序
        while(low < high) {
            while(low < tempId && data[low] <= temp) {
                low ++ ;}
            data[tempId] = data[low] ;
            tempId = low ;
            low++ ;
            while(tempId < high && data[high] >= temp) {
                high -- ;}
            data[tempId] = data[high] ;
            tempId = high ;
            high-- ;
        }
        
        data[tempId] = temp ;
        
        //处理枢轴左边的数组区间
        if(tempId > 0) {
            int[] leftData = order(Arrays.copyOfRange(data, 0 , tempId)) ;
            
            for(int i = 0 ; i < tempId ; i++ ) {
                data[i] = leftData[i] ;
            }
        }
        //处理枢轴右边的数组区间
        if(tempId+1 < data.length ) {
            int[] rightData = order(Arrays.copyOfRange(data, tempId+1 , data.length)) ;
            
            for(int i = tempId+1 ; i < data.length ; i++) {
                data[i] = rightData[i - (tempId+1)] ;
            }
        }
        
        return data ;
        
    }

测试代码

public static void main(String[] args) {
        
        int[] data = {46,68,12,25,33,80,19,33} ;
        
        data = order(data) ;
        if(data != null)
        for(int i = 0 ; i < data.length ; i++) {
            System.out.print( data[i] + ((i==data.length-1) ? "":",") );
        }
        
    }

运行结果

t排序前
46,68,12,25,33,80,19,33

>>>>>>>>>>>>>>>>>>>>>>>>>>
排序后
12,19,25,33,33,46,68,80


看完整源码戳源码地址

相关文章

  • 面试题10-快速排序

    题目要求 快速排序 题目解析 思路一: 分析 首先选择一个枢轴值,和两个下标值分别为low和high。首先比较是否...

  • 快速排序和归并排序

    1、快速排序 快速排序是冒泡排序的改进版,也是最好的一种内排序,在很多面试题中都会出现,也是作为程序员必须掌握的一...

  • 算法

    十大经典排序算法(动图演示) 【数据结构】链表的原理及与其相关的常见面试题总结 一、排序算法 交换排序冒泡排序快速...

  • Go使用goroutine并发的快速排序

    经典面试题:快速排序。一般都使用递归,但golang中利用goroutine的并发可以加快。

  • 快速排序

    快速排序是冒泡排序的改进版,也是最好的一种内排序,在很多面试题中都会出现,也是作为程序员必须掌握的一种排序方法。 ...

  • LeetCode | 面试题10- II. 青蛙跳台阶问题【剑指

    LeetCode 面试题10- II. 青蛙跳台阶问题【剑指Offer】【Easy】【Python】【动态规划】 ...

  • 面试题

    面试题 二叉树 非递归实现二叉树遍历 节点: 前序遍历 中序遍历 后序遍历 排序 快速排序 其他问题 算法题 给一...

  • LeetCode | 面试题10- I. 斐波那契数列【剑指Of

    LeetCode 面试题10- I. 斐波那契数列【剑指Offer】【Easy】【Python】【动态规划】 问题...

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

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

  • 面试准备--排序

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

网友评论

      本文标题:面试题10-快速排序

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