当前位置:网站首页>Counting sorting and stability of sorting
Counting sorting and stability of sorting
2022-06-28 23:06:00 【Salted fish learning code】
What we said earlier is to sort by comparison , This time I'll talk about non comparative sorting . then , Summarize the sorting .
List of articles
1. Count sorting
1.1 The basic idea
thought : Counting sorting is also called pigeon nest principle , It is a variant application of hash direct addressing method . Operation steps :
1. Count the number of occurrences of the same element
2. According to the statistical results, the sequence is recycled into the original sequence
Let's take an example :
In this set of data , The largest number we can see is 10, So we define a subscript to 10 Array of , And assign all the contents in the array to 0.
then , We traverse the original array , How many times does a value appear , Its mapping position is ++ A few times .
This is what happens after mapping , Then we assign the original array from small to large .
This mapping is also called absolute mapping , But if the data is particularly large , Then absolute mapping will waste space . For example, the following set of data :
If absolute mapping is used for this set of data, the previous 1000 There will be a waste of space . here , We need to use relative mapping . How to use relative mapping ?
We need to find the smallest and largest data , Then subtracting is the size of the mapping array .
From the above set of data , The biggest thing is 5000, The smallest is 1000, Subtraction is 4000, So we need to define 4001 Space .
here ,0 It stands for 1000,4000 It stands for 5000. here , Mapping according to relative position .1500 Namely 500,3700 Namely 2700,1800 Namely 800,2100 Namely 1100.
1.2 Concrete realization
That's the basic idea , We need to implement it :
Here we need to focus on the two parts of counting and sorting , Implementation is not difficult .
here , We need to think about one more question : How about negative numbers ?
Absolute mapping , We don't have to think about , Surely not , How about relative mapping ?
ad locum , The biggest thing is 2100, The smallest is -5000, Then we need to define 2100-(-5000)=7100.
ad locum ,1000 It stands for 6000,-1500 It stands for 3500,-3700 It stands for 1300,1800 It stands for 6800.
When counting 6000,3500,1300,6800 Location ++, When sorting returns the original array , Add... To these positions -5000, Is the original data , So negative numbers are OK . So let's verify that :
Count sorting , We can see that it applies to the data in the scope set , It is also possible to have negative numbers in an array . But other types don't work , Like floating point 、 Strings and so on .
1.3 Complexity
First , There is nothing to say about spatial complexity , Is the extra space :O(range)
Time complexity , Let's talk about :
The first two cycles are both n, There is nothing to say , The main last cycle may be a problem :
Inside while loop , When all the numbers have been subtracted , It's in the original array n, Outside for The number of times the loop is executed is range Time . The two cycles have nothing to do with each other , therefore , The total number of times the two loops are executed is n+range, At large O The time complexity in the progressive representation of is O(n+range)
2. stability
2.1 What is stability
You may think that stability refers to the fluctuation of performance , But it's not like that .
stability : Assume in the sequence of records to be sorted , There are multiple records with the same keyword , If sorted , The relative order of these records remains the same , In the original sequence ,r[i]=r[j], And r[i] stay r[j] Before , In the sorted sequence ,r[i] Still in r[j] Before , We call this sort algorithm stable ; Otherwise it is called unstable .
for instance :
For example, in an array , There are two identical numbers . Blue 10 In the red 10 front .
After sorting out , Blue 10 Still in red 10 front , That means it is stable , Otherwise, it will be unstable .
2.2 Which sort is stable
Which sort is stable , Let's analyze :
Direct insert sort , According to our thoughts : We insert the small one forward , Large and equal immobility . therefore , We can think of it as stable .
Shell Sort , It's unstable , Because the same data may be divided into different gap Group .
For example, this set of data , Blue 3 Will be indexed to 2 The location of , Red 3 Will be indexed to 1 The location of . The relative positions of the two are different , It's unstable .
Selection sort , It's unstable . Let's look at this set of data :
We chose the smallest 1, And the leftmost 3 swapping .
You'll find two 3 The relative position has changed . So it's not stable .
Heap sort , It's unstable . We can see from the following data :
Two 8 The relative position of has changed .
Bubble sort , It's stable . Because bubbling is a comparison between two , The big ones move back , Small and equal do not move .
Quick sort , It's unstable . because , Every election key When it is uncertain , It may select the first number in the middle .
Merge sort , It's stable . Let's take an example :
From the example above , If we go blue first , It's stable .
Next, I will attach a sort complexity diagram :
边栏推荐
- 国盛证券开户是真的安全可靠吗
- The love digital smart 2022 summit opens, sharing data strategy and building data-driven organization methodology
- Realization of 2D code generation in micro build low code
- keil工程,程序写多后,RTT不能打印
- 在线文本过滤小于指定长度工具
- 自媒体行业内卷严重:企业自媒体应该何去何从
- 带链接跳转的微信红包封面制作教程和使用指南
- Undefined symbol main (referred from entry9a.o).
- frameworks/base/core/res/res/values/symbols.xml:3915: error: no definition for declared symbol解决办法
- 月薪6万,互联网“降本增效”后,这类人开始被疯抢
猜你喜欢

Online text filter less than specified length tool
![LeetCode 324 摆动排序 II[排序 双指针] HERODING的LeetCode之路](/img/41/b8ba8d771b7224eac1cc8c54fe9d29.png)
LeetCode 324 摆动排序 II[排序 双指针] HERODING的LeetCode之路

CS5463代码模块解析(包含下载链接)
利用Redis实现点赞功能的示例代码

Zadig + cave Iast: let safety dissolve in continuous delivery

Qtcreater5.15.0 source code compilation process record

Powerful open source API interface visual management platform Yapi

Production environment sonarqube installation

深入虚拟内存(Virtual Memory,VM)

Realization of 2D code generation in micro build low code
随机推荐
Google Earth Engine(GEE)——利用sentinel-2数据进行农作物提取分析
Linux Installation mysql5.7 (centos7.6) tutorial
Lecun predicts AgI: big model and reinforcement learning are both ramps! My world model is the new way
Flowable boundary timer
深入虚拟内存(Virtual Memory,VM)
Code example of hiredis
WMS仓库管理系统模块之波次拣货
Web API learning notes 1
Business atlas in super factory
The Best of Many Worlds_ Dual Mirror Descent for Online Allocation Problems
Interpretation of papers (DCN) towards k-means-friendly spaces: simultaneous deep learning and clustering
2022年PMP项目管理考试敏捷知识点(4)
Multiomics single cell data integration and regulatory reasoning based on graph linked embedding
Oracle deleting archived logs and adding scheduled tasks
Undefined symbol main (referred from entry9a.o).
In the era of industrial Internet, the Internet in the traditional sense will evolve into many new forms
【深度学习】(3) Transformer 中的 Encoder 机制,附Pytorch完整代码
Online linear programming: Dual convergence, new algorithms, and regret bounds
Powerful open source API interface visual management platform Yapi
长投学堂帮忙开证券账户是安全靠谱的吗?个人如何开