首页 > 其他分享 >快速排序及其优化

快速排序及其优化

时间:2023-04-02 16:24:01浏览次数:42  
标签:end int 优化 partition start pivot array 排序 快速

package leetcode.mySort;

import java.util.Random;

public class QuickSort {

private final static Random random = new Random(System.currentTimeMillis());

// 快速排序的不同类型的写法,差别在于partition 下面的partition是大学时候老师教的方法 partition2是
// 类似三色国旗的写法,为后续的优化做准备 partition和partition2本质上都是二路快排

public static int partition(int[] array,int start,int end){
    int base = array[start];
    while (start<end){
        while (start<end&&array[end]>=base)  end--;
        array[start] = array[end];

        while (start<end&&array[start]<=base)  start++;
        array[end] = array[start];
    }
    array[start] = base;
    return start;
}

public static int partition2(int[] array,int start,int end){
    int pivot = array[start];

    // j代表较小区间的最后一个
    // all in array[left+1,j] <= pivot
    // all in array(j,i) > pivot

    int j = start;

    // 大方过,小交换
    for (int i = start+1; i <= end; i++) {
        if (array[i]<pivot){
            j++;
            swap(array,i,j);
        }
    }
    swap(array,start,j);
    return j;
}

// 随机初始化pivot
public static int partition3(int[] array,int start,int end){
int randomIndex =start + random.nextInt(end - start + 1);
swap(array,start,randomIndex);
int pivot = array[start];
int j = start;

    for (int i = start+1; i <= end; i++) {
        if(pivot>array[i]){
            j++;
            swap(array,j,i);
        }

    }
    swap(array,start,j);
    return j;
}

// 三路快排
public static void quickSort3(int[] array, int start, int end){

    if (start>=end){
        return;
    }

    int randomIndex = start + random.nextInt(end - start + 1);
    swap(array,start,randomIndex);
    int pivot = array[start];

    int lt = start + 1;     // lt:less than
    int gt = end;           // gt:greater than

    // all in nums[left+1,lt) < pivot
    // all in nums[lt,i) = pivot
    // all in nums(gt,right] > pivot
    int i = start + 1;

    while (i <= gt){
        if(array[i]<pivot){
            swap(array,i,lt);
            i++;
            lt++;
        }else if (array[i]==pivot){
            i++;
        }else{
            swap(array,i,gt);
            gt--;
        }
    }
    swap(array,start,lt-1);
    quickSort3(array,start,lt-1);
    quickSort3(array,gt+1,end);
}


private static void swap(int[] array, int start, int end) {
    int temp = array[start];
    array[start] = array[end];
    array[end] = temp;
}

public static void quickSort(int[] array,int start,int end){

    if (start<end){
        int index = partition3(array,start,end);
        quickSort(array,start,index-1);
        quickSort(array,index+1,end);
    }
}


public static void main(String[] args) {

// int[] array = {2,4,6,1,3,5};
int[] array = {3,2,3,1,2,4,5,5,6};
quickSort3(array,0,array.length-1);
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}

参考:https://www.bilibili.com/video/BV1Kr4y187VP/?spm_id_from=333.788&vd_source=46d50b5d646b50dcb2a208d3946b1598

标签:end,int,优化,partition,start,pivot,array,排序,快速
From: https://www.cnblogs.com/chenyi502/p/17280685.html

相关文章

  • Elasticsearch 学习-Elasticsearch优化,硬件选择,分片策略,写入优化,内存设置,重要配置
    Elasticsearch学习-Elasticsearch优化,硬件选择,分片策略,写入优化,内存设置,重要配置6.1硬件选择Elasticsearch的基础是Lucene,所有的索引和文档数据是存储在本地的磁盘中,具体的路径可在ES的配置文件../config/elasticsearch.yml中配置,如下:#----------------------------......
  • PowerJob 快速上手 Ruoyi接入PowerJob
    一、引入依赖1、pom.xml(ruoyi)<!--快速集成PowerJob执行器--><dependency><groupId>tech.powerjob</groupId><artifactId>powerjob-worker-spring-boot-starter</artifactId>......
  • 杭电oj Realtime Status(利用快速幂)
    今天这个题我又又又是看大佬的题解。原因是我的暴力想法超时了…………大家可以先搜索一下什么是快速幂。(我看完之后了解的快速幂,就是通过放大底数以达到减小指数从而大幅减少运算次数的方法)这里就不赘述了,题目是这样的:对了,再啰嗦几句,由于这个题的数据量很大并且他只需要......
  • 牛客SQL-非技术快速入门
    01基础查询SQL1查询所有列select*fromuser_profileSQL2查询多列selectdevice_id,gender,age,universityfromuser_profileSQL3查询结果去重selectdistinct(university)fromuser_profileSQL4查询结果限制返回行数top不适用于所有的数据库语言。SQLSERVER......
  • C++ Primer Plus基础知识部分快速通关
    目录第二章第三章第四章数组字符串结构体共用体枚举指针指针、数组与指针算术变量存储方式数组替代第五章递增递减运算符指针与递增递减逗号运算符循环循环与文本输入文件尾(EOF)条件重要性实现第六章逻辑运算符相关字符函数库第七章基础知识函数与数组使用数组区间的函数指针与con......
  • 总结所有的排序方式
    一、插入排序就是从左到右遍历,然后看看这个数是否比前面的数小,如果比前面的小就插入到这个数的前面。publicstaticvoidinsertionSort(int[]arr){if(arr!=null&&arr.length>=2){for(inti=1;i<arr.length;++i){for......
  • 记一个C#排序
    usingSystem;namespacePX;publicclassPXTest{publicstaticvoidShow(){ScoreInfoscoreInfo=newScoreInfo(){ID=1,Name="张三",CSharp=12,DataStruct=24,......
  • 通过 docker-compose 快速部署 Hadoop 集群详细教程
    目录一、概述二、安装docker和docker-compose1)安装docker2)安装docker-compose三、docker-composedeploy1)设置副本数2)资源隔离四、docker-composenetwork五、docker-compose项目六、Hadoop部署(非高可用)1)安装JDK2)下载hadoop相关的软件3)构建镜像Dockerfile4)配置1、Hadoo......
  • 基于开源的 ChatGPT Web UI 项目,快速构建属于自己的 ChatGPT 站点
    作为一个技术博主,了不起比较喜欢各种折腾,之前给大家介绍过ChatGPT接入微信,钉钉和知识星球(如果没看过的可以翻翻前面的文章),最近再看开源项目的时候,发现了一个ChatGPTWebUI项目。想着刚好之前没有将ChatGPT接入过WebUI,有了这个开源项目可以拿来使用,真是不错,下面是实操的......
  • 外贸官方网站优化的核心要点与技巧
    作为一个从事外贸行业多年的业内人士,我深知优化官方网站在提升业务竞争力和吸引国际客户方面的重要性。在这篇文章中,我将分享一些关于外贸官方网站谷歌SEO优化的核心要点与技巧,帮助您更好地理解如何在Google搜索引擎中取得优势。首先,链接建设是谷歌SEO优化的关键一环。GPB外链作为......