👀个人简介:一个正在努力奋斗逆天改命的二本觉悟生
前言:在前面的学习中,我们实现了直接插入排序,希尔排序,直接选择排序,堆排序,冒泡排序,快速排序。那么今天我们就接着学习归并排序
目录
一.递归实现归并排序
归并排序算法思想:
归并排序(MERGE-SORT)是建立在归并操作上的⼀种有效的排序算法,该算法是采用分治法(Divide and Conquer)的⼀个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成⼀个有序表,称为二路归并。
分治的思想:将待排序的数组不断分割成更小的子数组,直到每个子数组只包含一个元素。
具体过程:
- 找到数组的中间位置,一般是通过 mid = left + (right - left) / 2 来计算(为了防止越界),left 和 right 分别表示当前数组范围的起始和结束索引。
然后递归地对数组的左半部分(索引范围从 left 到 mid)和右半部分(索引范围从 mid + 1 到 right)进行同样的分割操作,直到子数组的长度为 1。
代码实现:
void _MergeSort(int* arr, int left, int right,int*tmp)
{
//分解
if (left >= right)
{
return;
}
//根据mid将[left,right]划分为左右两个序列[left,mid] [mid+1,right]
int mid = left + (right - left) / 2;
_MergeSort(arr, left, mid,tmp);
_MergeSort(arr, mid+1, right,tmp);
//合并[left,mid] [mid+1,right]
int begin1 = left, end1 = mid;
int begin2 = mid+1, end2 = right;
int index = left;
while (begin1 <= end1 && begin2 <= end2)
{
if (arr[begin1] < arr[begin2])
{
tmp[index++] = arr[begin1++];
}
else
{
tmp[index++] = arr[begin2++];
}
}
//要么begin1越界
//要么begin2越界
while (begin1 <= end1)
{
tmp[index++] = arr[begin1++];
}
while (begin2 <= end2)
{
tmp[index++] = arr[begin2++];
}
for (int i = left; i <= right; i++)
{
arr[i] = tmp[i];
}
}
//归并排序
void MergeSort(int* arr, int n)
{
int* tmp = (int*)malloc(sizeof(int)*n);
//[0,n-1]
_MergeSort(arr, 0, n - 1,tmp);
free(tmp);
tmp = NULL;
}
提醒:
test.c:
#include"Sort.h"
void printArr(int* arr, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
void test01()
{
int a[] = { 5,3,9,6,2,4,7,1,8 };
//int a[] = { 6,1,2,7,9,3 };
int n = sizeof(a) / sizeof(a[0]);
printf("排序之前:");
printArr(a, n);
//InsertSort(a, n);
//ShellSort(a, n);
//SelectSort(a, n);
//HeapSort(a, n);
//BubbleSort(a, n);
//QuickSort(a, 0, n - 1);
//QuickSortNorR(a, 0, n - 1);
MergeSort(a, n);
//CountSort(a, n);
//MergeSortNonR(a, n);
printf("排序之后:");
printArr(a, n);
}
void TestOP()
{
srand(time(0));
const int N = 100000;
int* a1 = (int*)malloc(sizeof(int) * N);
int* a2 = (int*)malloc(sizeof(int) * N);
int* a3 = (int*)malloc(sizeof(int) * N);
int* a4 = (int*)malloc(sizeof(int) * N);
int* a5 = (int*)malloc(sizeof(int) * N);
int* a6 = (int*)malloc(sizeof(int) * N);
int* a7 = (int*)malloc(sizeof(int) * N);
for (int i = 0; i < N; ++i)
{
a1[i] = rand();
a2[i] = a1[i];
a3[i] = a1[i];
a4[i] = a1[i];
a5[i] = a1[i];
a6[i] = a1[i];
a7[i] = a1[i];
}
int begin1 = clock();
InsertSort(a1, N);
int end1 = clock();
int begin2 = clock();
ShellSort(a2, N);
int end2 = clock();
int begin3 = clock();
SelectSort(a3, N);
int end3 = clock();
int begin4 = clock();
HeapSort(a4, N);
int end4 = clock();
int begin5 = clock();
QuickSort(a5, 0, N - 1);
int end5 = clock();
//int begin6 = clock();
//MergeSort(a6, N);
//int end6 = clock();
int begin7 = clock();
BubbleSort(a7, N);
int end7 = clock();
printf("InsertSort:%d\n", end1 - begin1);
printf("ShellSort:%d\n", end2 - begin2);
printf("SelectSort:%d\n", end3 - begin3);
printf("HeapSort:%d\n", end4 - begin4);
printf("QuickSort:%d\n", end5 - begin5);
//printf("MergeSort:%d\n", end6 - begin6);
printf("BubbleSort:%d\n", end7 - begin7);
free(a1);
free(a2);
free(a3);
free(a4);
free(a5);
free(a6);
free(a7);
}
int main()
{
test01();
//TestOP();
return 0;
}
测试结果:
测试完成,升序排序没有问题,退出码为0
时间复杂度:O(n*logn)
递归时间复杂度计算:
- 时间复杂度 = 单次递归时间复杂度 * 递归次数
二.非递归实现归并排序
非递归版本的归并排序(迭代实现),核心思想是 “自底向上” 合并子数组,无需递归,直接通过循环控制分组大小(gap),逐步完成排序。
具体过程:
1. 分组合并:
- gap 从 1 开始,每次翻倍(gap *= 2),表示当前合并的子数组长度。
- gap=1 时,合并相邻的 1 个元素 组成的子数组(实际是单个元素,视为有序);gap=2 时,合并相邻的 2 个元素 组成的有序子数组,以此类推。
2. 边界修正
- 计算 end1,end2 时,需判断是否超出数组范围(n-1 是最后一个元素的索引)。
- 若 begin2 >= n,说明第二个子数组不存在,直接跳过合并。
3. 合并操作
- 用双指针法合并两个有序子数组到 tmp,再通过 memcpy 写回原数组。
- 保证合并后子数组有序,逐层向上归并。
代码实现:
//归并排序--非递归版本
void MergeSortNonR(int* a, int n)
{
int* tmp = (int*)malloc(sizeof(int) * n);
if (tmp == NULL)
{
perror("malloc fail!\n");
exit(1);
}
int gap = 1;
while (gap < n)
{
//根据gap划分组,两两合并
for (int i = 0; i < n; i += 2 * gap)
{
int begin1 = i, end1 = i + gap - 1;
int begin2 = i + gap, end2 = i + 2 * gap - 1;
int index = begin1;
if (begin2 >= n)
{
break;
}
if (end2 >= n)
{
end2 = n - 1;
}
//合并
while (begin1 <= end1 && begin2 <= end2)
{
if (a[begin1] < a[begin2])
{
tmp[index++] = a[begin1++];
}
else
{
tmp[index++] = a[begin2++];
}
}
while (begin1 <= end1)
{
tmp[index++] = a[begin1++];
}
while (begin2 <= end2)
{
tmp[index++] = a[begin2++];
}
//tmp导入到a数组中
memcpy(a + i, tmp+i, sizeof(int)*(end2 - i + 1));
}
gap *= 2;
}
free(tmp);
}
test.c:
#include"Sort.h"
void printArr(int* arr, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
void test01()
{
int a[] = { 5,3,9,6,2,4,7,1,8 };
//int a[] = { 6,1,2,7,9,3 };
int n = sizeof(a) / sizeof(a[0]);
printf("排序之前:");
printArr(a, n);
//InsertSort(a, n);
//ShellSort(a, n);
//SelectSort(a, n);
//HeapSort(a, n);
//BubbleSort(a, n);
//QuickSort(a, 0, n - 1);
//QuickSortNorR(a, 0, n - 1);
//MergeSort(a, n);
//CountSort(a, n);
MergeSortNonR(a, n);
printf("排序之后:");
printArr(a, n);
}
void TestOP()
{
srand(time(0));
const int N = 100000;
int* a1 = (int*)malloc(sizeof(int) * N);
int* a2 = (int*)malloc(sizeof(int) * N);
int* a3 = (int*)malloc(sizeof(int) * N);
int* a4 = (int*)malloc(sizeof(int) * N);
int* a5 = (int*)malloc(sizeof(int) * N);
int* a6 = (int*)malloc(sizeof(int) * N);
int* a7 = (int*)malloc(sizeof(int) * N);
for (int i = 0; i < N; ++i)
{
a1[i] = rand();
a2[i] = a1[i];
a3[i] = a1[i];
a4[i] = a1[i];
a5[i] = a1[i];
a6[i] = a1[i];
a7[i] = a1[i];
}
int begin1 = clock();
InsertSort(a1, N);
int end1 = clock();
int begin2 = clock();
ShellSort(a2, N);
int end2 = clock();
int begin3 = clock();
SelectSort(a3, N);
int end3 = clock();
int begin4 = clock();
HeapSort(a4, N);
int end4 = clock();
int begin5 = clock();
QuickSort(a5, 0, N - 1);
int end5 = clock();
//int begin6 = clock();
//MergeSort(a6, N);
//int end6 = clock();
int begin7 = clock();
BubbleSort(a7, N);
int end7 = clock();
printf("InsertSort:%d\n", end1 - begin1);
printf("ShellSort:%d\n", end2 - begin2);
printf("SelectSort:%d\n", end3 - begin3);
printf("HeapSort:%d\n", end4 - begin4);
printf("QuickSort:%d\n", end5 - begin5);
//printf("MergeSort:%d\n", end6 - begin6);
printf("BubbleSort:%d\n", end7 - begin7);
free(a1);
free(a2);
free(a3);
free(a4);
free(a5);
free(a6);
free(a7);
}
int main()
{
test01();
//TestOP();
return 0;
}
测试结果:
测试完成,升序排序没有问题,退出码为0
三.归并排序的性能测试
通过测试函数看看它们的性能对比吧
代码演示:
void TestOP()
{
srand(time(0));
const int N = 100000;
int* a1 = (int*)malloc(sizeof(int) * N);
int* a2 = (int*)malloc(sizeof(int) * N);
int* a3 = (int*)malloc(sizeof(int) * N);
int* a4 = (int*)malloc(sizeof(int) * N);
int* a5 = (int*)malloc(sizeof(int) * N);
int* a6 = (int*)malloc(sizeof(int) * N);
int* a7 = (int*)malloc(sizeof(int) * N);
for (int i = 0; i < N; ++i)
{
a1[i] = rand();
a2[i] = a1[i];
a3[i] = a1[i];
a4[i] = a1[i];
a5[i] = a1[i];
a6[i] = a1[i];
a7[i] = a1[i];
}
int begin1 = clock();
InsertSort(a1, N);
int end1 = clock();
int begin2 = clock();
ShellSort(a2, N);
int end2 = clock();
int begin3 = clock();
SelectSort(a3, N);
int end3 = clock();
int begin4 = clock();
HeapSort(a4, N);
int end4 = clock();
int begin5 = clock();
QuickSort(a5, 0, N - 1);
int end5 = clock();
int begin6 = clock();
MergeSort(a6, N);
int end6 = clock();
int begin7 = clock();
BubbleSort(a7, N);
int end7 = clock();
printf("InsertSort:%d\n", end1 - begin1);
printf("ShellSort:%d\n", end2 - begin2);
printf("SelectSort:%d\n", end3 - begin3);
printf("HeapSort:%d\n", end4 - begin4);
printf("QuickSort:%d\n", end5 - begin5);
printf("MergeSort:%d\n", end6 - begin6);
printf("BubbleSort:%d\n", end7 - begin7);
free(a1);
free(a2);
free(a3);
free(a4);
free(a5);
free(a6);
free(a7);
}
int main()
{
//test01();
TestOP();
return 0;
}
测试结果:
观察发现:希尔排序、快速排序、堆排序、归并排序的优秀
往期回顾:
总结:到目前为止,我们已经讲将常见的比较类的各种排序都实现完了,在下篇博客中我会为大家介绍一下非比较排序中的计数排序以及总结各种排序的空间复杂度,时间复杂度以及稳定性,如果文章对你有帮助的话,欢迎评论,点赞,收藏加关注,感谢大家的支持。