惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
InfoQ
S
SegmentFault 最新的问题
N
Netflix TechBlog - Medium
B
Blog
Jina AI
Jina AI
人人都是产品经理
人人都是产品经理
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 聂微东
Last Week in AI
Last Week in AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
大猫的无限游戏
大猫的无限游戏
U
Unit 42
J
Java Code Geeks
IT之家
IT之家
aimingoo的专栏
aimingoo的专栏
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
When Does the Stability of Sorting Algorithms Matter?
2017-09-30 · via jdhao's digital space

In this post, I will write about the stability of sorting algorithms and why it is important.

When we read articles about sorting algorithms, a term that often appears is the “stability” of an algorithm. The definition of stability is pretty simple: Given an unsorted list and a sorting algorithm. If two equal elements in the original list remain their relative order in the sorted list, then the sorting algorithm is stable, otherwise it is an unstable algorithm.

Now that we know the definition and there tons of article telling you which sorting algorithm is stable and which is not, we may wonder what is the point of identifying if an algorithm is stable or not. In fact, if you only have a list of single items to sort, the stability of the sorting algorithm is not important. However, if you have a list where each element is some kind of record which has several keys, you way want to sort the record based on several keys. That is where stability comes into play.

For example, we have a list of some students who belong to different labs,1

Dave  A
Alice B
Ken   A
Eric  B
Carol A

We want to sort the records first using the student names as the key, the result is:

Alice B
Carol A
Dave A
Eric B
Ken A

Then we want to sort the above result further based on their labs, we may get the result of

Dave A
Ken A
Carol A
Alice B
Eric B

or

Carol A
Dave A
Ken A
Alice B
Eric B

If the use a stable algorithm, it will ensure that the order of previous sorting will be preserved if two keys are the same. So the names corresponding to the same labs are in sorted order. The result will be

Carol A
Dave A
Ken A
Alice B
Eric B

In summary, the stability of sorting algorithms is important only when you want to chain multiple sort according to different keys in the record. If your record only has one key, the stability issue is irrelevant.

References#


  1. The example is from this page↩︎