当前位置:网站首页>归并排序
归并排序
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;
}
边栏推荐
猜你喜欢
随机推荐
第四讲 SVN
白盒测试的概念、目的是什么?及主要方法有哪些?
Onnx - environment build 】 【 tensorrt
有返回值的函数
JS报错-Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on...
测试用例的原则、缺陷报告怎么写你都知道吗?
How much do you know about the mobile APP testing process specifications and methods?
Go-控制语句那些事
如何用数组实现环形队列
Lecture 4 SVN
第三方免费开放API 获取用户IP 并查询其地理位置
【分布式事务】
接口开发规范及测试工具的使用
Do you know the basic process and use case design method of interface testing?
栈的实现之用链表实现
8.Properties属性集合
自动化测试框架有哪几种?搭建的思路是什么?一篇文章让你彻底了解自动化
字典
What are the basic concepts of performance testing?What knowledge do you need to master to perform performance testing?
一个项目的整体测试流程有哪几个阶段?测试方法有哪些?






