当前位置:网站首页>彩虹排序 | 荷兰旗问题
彩虹排序 | 荷兰旗问题
2020-11-09 12:28:00 【码农田小齐】
微信搜索「码农田小齐」,关注这个在纽约的程序媛,回复「01-05」可以获取计算机精选书籍、个人刷题笔记、大厂面经、面试资料等资源,么么哒~
荷兰旗问题又称三色排序,或者彩虹排序,
因为荷兰旗就三种颜色嘛,那这道题的问题就是给你三种颜色,按照给定的顺序排好。
当然了,题目的问法各种各样,有的给数字,有的给字母,但本质都是一样的。
比如给你一个只含有三个数字的数组:312312312231111122113,
要求按照 1 2 3 的顺序排好,即: 111111111222222222223333333333
(请不要真的去数数,认真你就输了)
还是用我们经典的「挡板法」。
在快排中,我们用了两个挡板把数组分成三个区域:
<= pivot;未排序区间;> pivot
那么这里就要三个挡板,分成四个区域:
1, 2, 3, 未排序区间
Partition
具体说来,用 i, j, k 这三个指针分一下:
[0, i): 存 1
[i, j): 存 2
(k, array.length-1]: 存 3
这里 j 放在未排序区间的左边和右边都行,但基本上大家都是放左边,所以我们也没必要“标新立异”。
初始化:
i = 0;
j = 0;
k = array.length - 1;
这样才能保证 1,2,3 的每个区间都为空。
我们通过观察指针 j 指向的元素来不断缩小未排序区间,直到为空。
例子:1232312
为了好看,排好序的元素我们用 RGB 三原色标示一下。
Step1.
指针 j 指向 1,而 1 应该放在 [0, i) 区间内, 这里应该把指针 i 和指针 j 所指的元素交换一下,并且俩指针往前走。
虽然在这步看来是否交换没什么区别,但是如果 i 和 j 之间有元素,就有区别了,比如 Step7.
Step2.
指针 j 指向 2,而 2 应该放在 [i, j) 区间内,所以 j++.
Step3.
指针 j 指向 3,而 3 应该放在
(k, array.length-1] 区间内,所以这里
j 和 k 指向的元素交换一下,并且 k--.
注意这里就不能 j-- 了,因为新换回来的元素还没瞧呢,不知道它是几。
Step4.
指针 j 指向 2,同 Step2,直接移动指针 j 即可。
Step5.
继续移动指针 j。
Step6.
同 Step3.
Step7.
这一步就很明显看出来了,
由于 1 应该放在 [0,i) 区间,所以这里把指针 i,j 所指向的元素交换一下,并且 i++, j++.
这样未排序区间为空,我们就排好了~
时间复杂度
这个算法的 bottle neck 就在这个 while loop 里了,每次循环是 O(1),总共是 O(n).
空间复杂度
O(1)
如果你喜欢这篇文章,记得给我点赞留言哦~你们的支持和认可,就是我创作的最大动力,我们下篇文章见!
我是小齐,纽约程序媛,终生学习者,每天晚上 9 点,云自习室里不见不散!
更多干货文章见我的 Github: https://github.com/xiaoqi6666/NYCSDE
版权声明
本文为[码农田小齐]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4612374/blog/4708570
边栏推荐
- El table dynamic header
- Android权限大全
- The choice of domain name of foreign trade self built website
- Python zero basics tutorial (01)
- Handwriting Koa.js Source code
- Understanding task and async await
- 走进京东 | 中国空间技术研究院青年创新联盟成员莅临参观京东总部
- How to ensure that messages are not consumed repeatedly? (how to ensure the idempotent of message consumption)
- TiDB x 微众银行 | 耗时降低 58%,分布式架构助力实现普惠金融
- Tidb x micro banking reduces time consumption by 58%, and distributed architecture helps to realize inclusive finance
猜你喜欢
Flink 系例 之 Reduce
Configure switch trunk interface traffic local priority forwarding (cluster / stack)
未来中国电信将把云计算服务打造成为中国电信的主业
Detailed explanation of [golang] GC
JVM学习(五) -执行子系统
Android Studio Avd「真·小白食用方法」
Android NDK 开发实战 - 微信公众号二维码检测
List of wechat video Number broadcasters October 2020
On the calculation of non interaction polarizability
El table dynamic header
随机推荐
Configure switch trunk interface traffic local priority forwarding (cluster / stack)
Solve the problem of idea shortcut key Alt + insert invalid
Review of hot spots of last week (11.2-11.8)
利用 Python 一键下载网易云音乐 10W+ 乐库
【golang】GC详解
Explain Python input() function: get user input string
Glsb involves load balancing algorithm
配置交换机Trunk接口流量本地优先转发(集群/堆叠)
Reading design patterns adapter patterns
Git delete IML file
Pay attention to the request forwarding problem of. Net core
Android Studio Avd「真·小白食用方法」
Implement crud operation
Android 集成支付的四部曲
Oh, my God! Printing log only knows log4j?
SQL Chapter 2 Chapter 3
Looking for better dynamic getter and setter solutions
Well, these four ways to query the maximum value of sliding window are good
SEO见风使舵,是对还是错?
EFF 认为 RIAA 正在“滥用 DMCA”来关闭 YouTube-DL