当前位置:网站首页>归并排序
归并排序
2022-08-09 09:26:00 【心中要有一片海】
归并排序 2013年10月31日 22:49:05
#include <stdio.h>
#include <stdlib.h>
void merge(int a[],int left,int middle, int right)
{
int i = left;
int j = middle + 1;
int k = 0;
int *b = (int*)malloc(sizeof(int)*(right - left + 1));
while(i <= middle && j <= right)
{
if(a[i] > a[j])
{
b[k++] = a[j++];
}else{
b[k++] = a[i++];
}
}
while (i <= middle)
{
b[k++] = a[i++];
}
while (j <= right)
{
b[k++] = a[j++];
}
for(i= left,k = 0; i <= right;i++,k++)
{
a[i] = b[k];
}
free(b);
}
void mergeSort(int a[],int left,int right)
{
if(left==right)
{
return;
}
int middle = (left + right)/2;
mergeSort(a,left,middle);
mergeSort(a,middle+1,right);
merge(a,left,middle,right);
}
int main()
{
int x[]={4,-5,0,3,-1,12,9,-7,8,-4,11};
mergeSort(x,0,10);
for (int i = 0; i < sizeof(x)/sizeof(int); i++)
{
printf("%d ",x[i]);
}
return 0;
}
边栏推荐
猜你喜欢
Understanding of PID control motor output as motor PWM duty cycle input
一个项目的整体测试流程有哪几个阶段?测试方法有哪些?
单元测试是什么?怎么写?主要测试什么?
迭代
seata处理分布式事务
Anti App so层对抗分析
Teach you how to get a 0.1-meter high-precision satellite map for free
软件测试面试常见问题及答案(发散思维、接口、性能、概念、)
The div simulates the textarea text box, the height of the input text is adaptive, and the word count and limit are implemented
"The camera can't be used" + win8.1 + DELL + external camera + USB drive-free solution
随机推荐
GBase数据库中,源为 oracle 报出“ORA-01000:超出打开游标最大数”
使用Protege4和CO-ODE工具构建OWL本体的实用指南-1.3版本(4.Building An OWL Ontology)
自动化测试框架有哪几种?搭建的思路是什么?一篇文章让你彻底了解自动化
makefile学习-解决目标文件输出路径问题
JS报错-Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on...
约瑟夫问题的学习心得
What are the basic concepts of performance testing?What knowledge do you need to master to perform performance testing?
学习栈的心得和总结(数组实现)
学习双向链表的心得与总结
1.流的概念
8.递归遍历和删除案例
接口测试的基础流程和用例设计方法你知道吗?
WAVE SUMMIT 2022深度学习开发者峰会
自动化测试简历编写应该注意哪方面?有哪些技巧?
Go-控制语句那些事
Ontology Development Diary 05-Strive to Understand SWRL (Part 2)
on duplicate key update
选择黑盒测试用例设计方法的综合策略方案总结
Environment build onnxruntime 】
软件测试面试题目:请你列举几个物品的测试方法怎么说?