当前位置:网站首页>彩虹排序 | 荷兰旗问题
彩虹排序 | 荷兰旗问题
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
边栏推荐
- Visual Studio (MAC) installation process notes
- Chrome browser engine blink & V8
- 微信圈子
- Visit Jingdong | members of Youth Innovation Alliance of China Academy of space technology visit Jingdong headquarters
- Aren't you curious about how the CPU performs tasks?
- Complete set of linked list operations of data structure and algorithm series (3) (go)
- nodejs学习笔记(慕课网nodejs从零开发web Server博客项目)
- Android Studio Avd「真·小白食用方法」
- “开源软件供应链点亮计划 - 暑期 2020”公布结果 基于 ChubaoFS 开发的项目获得最佳质量奖
- Biden wins the US election! Python developers in Silicon Valley make fun of Ku Wang in this way
猜你喜欢

Navigation component of Android architecture (2)

Visit Jingdong | members of Youth Innovation Alliance of China Academy of space technology visit Jingdong headquarters

Complete set of linked list operations of data structure and algorithm series (3) (go)

使用TreeView树型菜单栏(递归调用数据库自动创建菜单)

Setting up a proxy for the WGet command

Flink 系例 之 Reduce

android studio AIDL的使用

开源ERP招聘了

Nine kinds of distributed primary key ID generation schemes of sub database and sub table are quite comprehensive

分库分表的 9种分布式主键ID 生成方案,挺全乎的
随机推荐
Android check box and echo
How to use function framework to develop large web application
For and for... In, for each and map and for of
Where should wild card SSL certificate register and apply
走进京东 | 中国空间技术研究院青年创新联盟成员莅临参观京东总部
Introduction to zero based im development (4): what is message timing consistency in IM systems?
The history of C1 research in Shenzhen
Learning notes of nodejs
The choice of domain name of foreign trade self built website
From coding, network transmission, architecture design, Tencent cloud high quality, high availability real-time audio and video technology practice
Implement crud operation
Handwriting Koa.js Source code
Well, these four ways to query the maximum value of sliding window are good
注意.NET Core进行请求转发问题
Interface tests how to pass files in post requests
微信视频号播主排行榜2020年10月
[design pattern] Chapter 4: Builder mode is not so difficult
解决IDEA快捷键 Alt+Insert 失效的问题
Android Studio Avd「真·小白食用方法」
医疗项目管理的三种实用技巧