当前位置:   article > 正文

数据结构-排序算法之快速排序_数据结构中快速排序算法

数据结构中快速排序算法

    快速排序属于交换排序的一种,其基本思想是基于分治法的。其算法时间复杂度为O(log2N),堪称是所有内部排序算法中平均性能最优的排序算法。


1-快速排序的基本思想

    从待排序表L:【0…n】中任取一个元素pivot作为枢轴/分界元素(通常取的是下标为0的首元素),通过一趟排序可将待排序列表划分为两部分——【0…k-1】和【k+1…n】,其中:【0…k-1】的元素都是小于等于pivot的元素子列表,【k+1…n】都是大于等于pivot的子列表,显然pivot枢轴元素会被放置到下标为k的位置上,这个过程被视为一次快速排序过程。然后就是通过相同的排序过程,递归式的对子列表进行排序操作,直到每部分只有一个元素或者为空时终止,此时:也得到了最终排序好的列表。

2-快速排序的过程图解

    假设现在有待排序列表:
        【49 38 65 97 76 13 27 49】,则快速排序的过程如下:

第一次排序过程

    可将待排序列表:
        【49 38 65 97 76 13 27 49】划分为两个子列表:

在这里插入图片描述
在这里插入图片描述

第二次分治排序过程

    可将第一次排序得到的两个子列表分别进行排序:

子列表1的排序过程

在这里插入图片描述

子列表2的排序过程

在这里插入图片描述

子列表2-1的分治排序过程

    由于第二次排序结果的子列表1中包含大于1个的数据元素,因此还要对第2次排序结果的子列表2-1进行排序。
在这里插入图片描述

排序结果

在这里插入图片描述

3-Java代码实现

package com.xwd;


/**
 * @ClassName Main
 * @Description: com.xwd
 * @Auther: xiwd
 * @Date: 2022/2/8 - 02 - 08 - 17:52
 * @version: 1.0
 */
public class Main {

    //methods
    /*快速排序 */
    public static int[] quickSort(int[] nums,int low,int high){
        if(low<high){
            //递归划分操作
            int pivotPos=partition(nums,low,high);
            quickSort(nums,low,pivotPos-1);
            quickSort(nums,pivotPos+1,high);
        }
        return nums;
    }

    /*分治法排序 */
    public static int partition(int[] nums,int low,int high){
        //将表中的第一个元素设为枢轴,对表进行划分
        int pivot=nums[low];
        //循环-进行数据检索和划分,当low==hight时终止
        while(low<high){
            //从后向前检索小于pivot的元素
            while(low<high&&nums[high]>=pivot) --high;
            //将小于pivot的元素移动到pivot的左端
            nums[low]=nums[high];
            //从前向后检索大于pivot的元素
            while(low<high&&nums[low]<=pivot) ++low;
            //将大于pivot的元素移动到pivot的右端
            nums[high]=nums[low];
        }
        //将pivot元素放置到low==high的位置
        nums[low]=pivot;
        return low;//返回pivot最终的分治位置
    }

    public static void main(String[] args) {
        int[] nums=new int[1000];
        //随机生成1000个数字
        for (int i = 0; i < nums.length; i++) {
            nums[i]= (int) ((-1)*Math.random()*10000+Math.random()*10000);
        }
        quickSort(nums,0,nums.length-1);
        for (int num : nums) {
            System.out.println(num);
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

4-例题

题目描述

一个整数数组 nums 。如果任一值在数组中出现 至少两次 ,返回 true ;如果数组中每个元素互不相同,返回 false

结题思路

①利用快速排序算法对原始数组进行排序,得到有序数组;
②通过双指针法,从前向后遍历有序数组,如果连续两个数据元素相同,那么就直接终止遍历,返回true;否则继续向后遍历直至达到数组末尾,最终返回false。

代码编写

class Solution {
	/*判断:数组nums中是否存在两个连续相等的数据元素,返回值为boolean类型*/
    public boolean containsDuplicate(int[] nums) {
        //快速排序
        int[] array = quickSort(nums,0,nums.length-1);
        int i=0,j=i+1;
        boolean flag=false;//标志位
        //双指针遍历排序后数组
        for(;j<array.length;i++,j++){
            if(array[i]==array[j]){
                flag=true;
                break;
            }
        }
        //返回结果
        return flag;
    }

    /*快速排序 */
    public static int[] quickSort(int[] nums,int low,int high){
        if(low<high){
            //递归划分操作
            int pivotPos=partition(nums,low,high);
            quickSort(nums,low,pivotPos-1);
            quickSort(nums,pivotPos+1,high);
        }
        return nums;
    }

    /*分治法排序 */
    public static int partition(int[] nums,int low,int high){
        //将表中的第一个元素设为枢轴,对表进行划分
        int pivot=nums[low];
        //循环-进行数据检索和划分,当low==hight时终止
        while(low<high){
            //从后向前检索小于pivot的元素
            while(low<high&&nums[high]>=pivot) --high;
            //将小于pivot的元素移动到pivot的左端
            nums[low]=nums[high];
            //从前向后检索大于pivot的元素
            while(low<high&&nums[low]<=pivot) ++low;
            //将大于pivot的元素移动到pivot的右端
            nums[high]=nums[low];
        }
        //将pivot元素放置到low==high的位置
        nums[low]=pivot;
        return low;//返回pivot最终的分治位置
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号