当前位置:网站首页>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 :
边栏推荐
- 收藏 | VLOOKUP函数的这些妙用你都知道吗?
- Understand shell script in one article
- SqlServer复习
- Prometeus 2.36.0 new features
- 笔记
- The Best of Many Worlds_ Dual Mirror Descent for Online Allocation Problems
- Leetcode detailed explanation of stack type
- [sword finger offer] 50 First character that appears only once
- Is it safe to open a stock account by mobile phone?
- Linux Installation mysql5.7 (centos7.6) tutorial
猜你喜欢

k线图基础知识图解——单根K线的含义

After crossing, she said that the multiverse really exists

Qtcreater5.15.0 source code compilation process record

How to solve the problem of desktop without sound

WEB API学习笔记1

Business atlas in super factory

This simple little function saves 213 hours for our production research team in half a year

Master the usage of const

微搭低代码中实现二维码生成

Leetcode detailed explanation of stack type
随机推荐
Oracle set password complexity and timeout exit function
一文读懂,WMS仓储管理系统与ERP有什么区别
Fanuc robot_ Introduction to Karel programming (2)_ Usage of general IO signal
Multiomics single cell data integration and regulatory reasoning based on graph linked embedding
A password error occurred when docker downloaded the MySQL image to create a database link
What is the difference between WMS warehouse management system and ERP
自媒体行业内卷严重:企业自媒体应该何去何从
Complex nested object pool (4) -- manage the object pool of multi class instances and multi-stage instances
Wave picking of WMS warehouse management system module
Tanghongbin, Yaya live CTO: to truly localize, the product should not have the attribute of "origin"
Linq连表查询
Lecun predicts AgI: big model and reinforcement learning are both ramps! My world model is the new way
小样本利器2.文本对抗+半监督 FGSM & VAT & FGM代码实现
[kotlin] beautiful pop-up box, custom pop-up box (dialog box), extension function, chrysanthemum waiting bar, message prompt box
Prometeus 2.36.0 new features
Keil project, RTT cannot print after too many programs are written
Is it safe to open a stock account online?
【Word 教程系列第 1 篇】如何去除 Word 表格中的箭头
【剑指Offer】50. 第一个只出现一次的字符
Mono 的执行流程