当前位置:网站首页>Merge sort and non comparison sort
Merge sort and non comparison sort
2022-07-07 08:27:00 【Old fish 37】
Merging and sorting is still difficult to understand , We should master the recursive and non recursive methods of merge sorting .
Now let's first understand what merge sorting is :
Four words ---- Divide and rule
First merge from the whole small , Then to the overall consolidation

Complete code :
void _MerGetSort(int* arr, int begin, int end, int* tmp)
{
if (begin >= end)
{
return;
}// Divide the area
int mid = (begin + end) / 2;
// recursive
_MerGetSort(arr, begin, mid, tmp);
_MerGetSort(arr, mid + 1, end, tmp);
// Merge and compare
int begin1 = begin;
int end1 = mid;
int begin2 = mid + 1;
int end2 = end;
int i = begin1;
while (begin1 <= end1 && begin2 <= end2)
{
if (arr[begin1] < arr[begin2])
{
tmp[i++] = arr[begin1++];
}
else
{
tmp[i++] = arr[begin2++];
}
}
// One of them must have finished Merge the rest into tmp in
while (begin1 <= end1)
{
tmp[i++] = arr[begin1++];
}
while (begin2 <= end2)
{
tmp[i++] = arr[begin2++];
}// It's over , take tmp Copy back arr Array
memcpy(arr + begin, tmp + begin, sizeof(int) * (end - begin + 1));
}void MerGetSort(int* arr, int len)
{
// Open up an array
int* tmp = (int *)malloc(sizeof(int) * len);
_MerGetSort(arr, 0, len - 1, tmp);
free(tmp);
}
void PrintSort(int* arr, int len)
{
for (int i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[] = { 3,1,2,4,9,5,6};
int len = sizeof(arr) / sizeof(arr[0]);
MerGetSort(arr, len);
PrintSort(arr, len);
}
The following describes non recursive writing :
void MerGetSort(int* arr, int len)
{
// Create a new array
int* tmp = (int*)malloc(sizeof(int) * len);
if (tmp == NULL)// Judge
{
printf("malloc fail\n");
exit(-1);
}
int gap = 1;// determine gap
while (gap < len)
{
printf("%d->", gap);
for (int i = 0; i < len; i += 2 * gap)
{
//[i, i + gap - 1] [gap + i, i + 2 * gap - 1] Divide the area
int begin1 = i, end1 = i + gap - 1;
int begin2 = gap + i, end2 = i + 2 * gap - 1;
printf("[%d][%d] [%d][%d]", begin1, end1, begin2, end2);
// Judge the cross-border area , And then modify
// because gap How much is the begin1 Will be the last , So from end1 Start to modify
if (end1 >= len)
{
end1 = len - 1;
begin2 = len; // here [begin2,end2]----[len,len-1] unreasonable , They don't execute
end2 = len - 1;
}
else if (begin2 >= len)// Invalid area modify
{
begin2 = len;
end2 = len - 1;
}
else if (end2 >= len)
{
end2 = len - 1;
}
int i = begin1;
// Then merge and compare
while (begin1 <= end1 && begin2 <= end2)
{
if (arr[begin1] < arr[begin2])
{
tmp[i++] = arr[begin1++];
}
else
{
tmp[i++] = arr[begin2++];
}
}
// After a walk , Put the remaining data into tmp
while (begin1 <= end1)
{
tmp[i++] = arr[begin1++];
}
while (begin2 <= end2)
{
tmp[i++] = arr[begin2++];
}
}
printf("\n");
memcpy(arr, tmp, sizeof(int) * len);// Copy
gap = gap * 2;
}
}
void PrintSort(int* arr, int len)
{
for (int i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[] = { 3,1,2,4,9,5,6};
int len = sizeof(arr) / sizeof(arr[0]);
MerGetSort(arr, len);
PrintSort(arr, len);
}
As for the invalid area, it refers to :
If you don't want to change the boundary , It can also be skipped directly , Revised as follows :
Method 2 :
such memcpy It's copied piece by piece , The first way is to copy all together , There are two ways to look at personal preferences .
Next, we will introduce non comparison sorting

What we will learn next is counting and sorting
Learn about counting sorting

This sort of counting has limitations :
1、 If it's a floating point number , String cannot be played
2、 If the data range is large , The space complexity will be very high , You can't play
3、 Generally, the difference between these data is relatively small
When you meet 100、1000 These data , If it's not much different , Using relative mapping is very practical .
// Count sorting
void CountSort(int* arr, int len)
{
int min = arr[0], max = arr[0];// The smallest and largest are the first Go to the back
for (int i = 1; i < len; i++)
{
if (arr[i] < min)
{
min = arr[i];
}
if (arr[i] > max)
{
max = arr[i];
}
}
// The largest and smallest have been found Open up space
int range = max - min + 1;
int* count = (int *)malloc(sizeof(int) * range);
if (count == NULL)
{
printf("malloc fail\n");
exit(-1);
}
memset(count, 0, sizeof(int) * range);// All initialized to 0// Count the number of times
for (int i = 0; i < len; i++)
{
count[arr[i] - min]++;
}// Write back sort
int j = 0;
for (int i = 0; i < range; i++)
{
while (count[i]--)
{
arr[j++] = i + min;
}
}
}
void PrintSort(int* arr, int len)
{
for (int i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[] = { 1003,1001,1002,1004,1009,1005,1006};
int len = sizeof(arr) / sizeof(arr[0]);
CountSort(arr, len);
PrintSort(arr, len);
}

Here we classify the previous sorts , And flawed assessment


I hope all my friends can learn these sorting , Your support is my driving force !
If there is a mistake , Your smile !
边栏推荐
- Transformation function map and flatmap in kotlin
- The field value in Splunk subquery fuzzy matching CSV is*
- Rainbond 5.6 版本发布,增加多种安装方式,优化拓扑图操作体验
- IP-guard助力能源企业完善终端防泄密措施,保护机密资料安全
- 如何理解分布式架构和微服务架构呢
- Splunk中single value视图使用将数值替换为文字
- One click deployment of highly available emqx clusters in rainbow
- Detailed explanation of apply, also, let, run functions and principle analysis of internal source code in kotlin
- Basic use of CTF web shrink template injection nmap
- 漏洞複現-Fastjson 反序列化
猜你喜欢

Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
![[quick start of Digital IC Verification] 14. Basic syntax of SystemVerilog learning 1 (array, queue, structure, enumeration, string... Including practical exercises)](/img/60/011b3ccdffa978d691436449a99e10.png)
[quick start of Digital IC Verification] 14. Basic syntax of SystemVerilog learning 1 (array, queue, structure, enumeration, string... Including practical exercises)

opencv学习笔记一——读取图像的几种方法

Iptables' state module (FTP service exercise)

柯基数据通过Rainbond完成云原生改造,实现离线持续交付客户

使用BiSeNet实现自己的数据集

opencv学习笔记五——梯度计算/边缘检测

漏洞複現-Fastjson 反序列化

Xcit learning notes

归并排序和非比较排序
随机推荐
接口作为参数(接口回调)
Infix keyword infix expression and the use of generic extension function in kotlin
【无标题】
iptables 之 state模块(ftp服务练习)
Rainbond 5.7.1 支持对接多家公有云和集群异常报警
The largest 3 same digits in the string of leetcode simple question
MES系統,是企業生產的必要選擇
拓维信息使用 Rainbond 的云原生落地实践
PLSQL的安装和配置
Xcit learning notes
Opencv learning note 3 - image smoothing / denoising
How to understand distributed architecture and micro service architecture
单场带货涨粉10万,农村主播竟将男装卖爆单?
The simple problem of leetcode is to judge whether the number count of a number is equal to the value of the number
Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience
国标GB28181协议视频平台EasyGBS新增拉流超时配置
Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications
eBPF Cilium实战(1) - 基于团队的网络隔离
Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu
[untitled]