当前位置:网站首页>Merge sort (recursive and iterative Implementation)
Merge sort (recursive and iterative Implementation)
2022-06-22 20:02:00 【Just one word】
Recursive implementation :B Station turtle video
https://www.bilibili.com/video/BV1jW411K7yg?p=95
#include <stdio.h>
#define MAXSIZE 10
// Merge , And store the final results in list1 in
void merging(int *list1, int list1_size, int *list2, int list2_size)
{
int i, j, k, m;
int temp[MAXSIZE];
i = j = k = 0;
while( i < list1_size && j < list2_size )
{
if( list1[i] < list2[j] )
{
temp[k++] = list1[i++];
}
else
{
temp[k++] = list2[j++];
}
}
while( i < list1_size )
{
temp[k++] = list1[i++];
}
while( j < list2_size )
{
temp[k++] = list2[j++];
}
for( m=0; m < (list1_size + list2_size); m++ )
{
list1[m] = temp[m];
}
}
void MergeSort(int k[], int n)
{
if( n > 1)
{
int *list1 = k;
int list1_size = n/2;
int *list2 = k + n/2;
int list2_size = n - list1_size;
MergeSort(list1, list1_size);
MergeSort(list2, list2_size);
merging(list1, list1_size, list2, list2_size);
}
}
int main()
{
int i, a[10] = {
5, 2, 6, 0, 3, 9, 1, 7, 4, 8};
MergeSort(a, 10);
printf(" The result of sorting is :");
for( i=0; i < 10; i++ )
{
printf("%d", a[i]);
}
printf("\n\n");
return 0;
}
Iterative implementation :
https://www.bilibili.com/video/BV1jW411K7yg?p=96
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10
void MergeSort(int k[], int n)
{
int i, next, left_min, left_max, right_min, right_max;
int *temp = (int *)malloc(n * sizeof(int));
for( i=1; i < n; i*=2 )
{
for( left_min=0; left_min < n-i; left_min = right_max )
{
right_min = left_max = left_min + i;
right_max = left_max + i;
if( right_max > n )
{
right_max = n;
}
next = 0;
while( left_min < left_max && right_min < right_max )
{
if( k[left_min] < k[right_min] )
{
temp[next++] = k[left_min++];
}
else
{
temp[next++] = k[right_min++];
}
}
while( left_min < left_max )
{
k[--right_min] = k[--left_max];
}
while( next > 0 )
{
k[--right_min] = temp[--next];
}
}
}
}
int main()
{
int i, a[10] = {
5, 2, 6, 0, 3, 9, 1, 7, 4, 8};
MergeSort(a, 10);
printf(" The result of sorting is :");
for( i=0; i < 10; i++ )
{
printf("%d", a[i]);
}
printf("\n\n");
return 0;
}
边栏推荐
- Storage structure of graph (adjacency matrix)
- [in depth understanding of tcapulusdb technology] tcapulusdb model
- delegate
- Upgrade VS2008 crystal report to the corresponding version of vs2013
- 51万奖池邀你参战!第二届阿里云ECS CloudBuild开发者大赛来袭
- Openpnp debugging ------ 0816 Feida Tui 0402 taping
- 再谈SQL profile : 到底能不能固定执行计划?
- 2. what is mechanical design?
- Huffman tree (C language)
- Search, insert and delete of binary sort tree
猜你喜欢
随机推荐
【深入理解TcaplusDB技术】集群管理操作
MySQL的函数
使用 Order by 与 rownum SQL 优化案例一则
Nrf51822 peripheral learning
【深入理解TcaplusDB技术】运维平台中实现TcaplusDB事务管理
Download files through Base64 (convert Base64 to BLOB)
【深入理解TcaplusDB技术】入门Tcaplus-JDBC开发
MySQL数据库DML操作练习
Peking University - robust task representation for off-line meta reinforcement learning through contrastive learning
[in depth understanding of tcapulusdb technology] tcapulusdb operation and maintenance doc
socket的connect函数用法
matplotlib设置坐标轴刻度间隔
Fibonacci search (golden section)
libcef最新下载地址-在VS2015下编译为MD-动态链接
从11小时到25秒--还有优化空间吗?
C WinForm embedded flash
Human pose estimation
Velocity 语法
Yarn notes
【深入理解TcaplusDB技术】TcaplusDB运维——日常巡检









