//合并有序数组

//合并例子

void MemeryArray(int a[], int n, int b[],int m, int c[])
{
int i,j,k; i=j=k=0; while(i<n && j<m)
{
if(a[i]<b[j])
{
c[k++] = a[i++];
}
else
{
c[k++]=b[j++];
}
} while(i<n)
{
c[k++]=a[i++];
}
while(j<n)
{
c[k++] = b[j++];
} }

//排序需要调用的如下

//排序调用

bool MergeSort(int a[], int n)
{
int *p = new int[n]; if(p==NULL)
{
return false;
} mergesort(a,0,n-1,p); //用n-1是因为 合并的时候用的是 "<=" 而不是 "<" delete[] p; return true;
}

//数组按照中值,左右执行递归

void mergesort(int a[],int first,int last,int temp[])
{
if(first<last)
{
int mid = (first+last)/2;
mergesort(a,first,mid,temp);
mergesort(a,mid+1,last,temp);
mergearray(a,first,mid,last,temp); }
}

//最后对数组进行合并

void mergearray(int a[], int first, int mid, int last, int temp[])
{
int i=first, j = mid+1;
int m = mid, n =last;
int k=0; while(i<=m&&j<=n)
{
if(a[i]<=a[j])
{
temp[k++]=a[i++];
}
else
{
temp[k++] = a[j++];
}
}
while(i<=m)
{
temp[k++] = a[i++];
}
while(j<=n)
{
temp[k++]=a[j++];
} for(i=0;i<k;i++)
{
a[first+i] = temp[i];
}
}

C++_归并排序的更多相关文章

  1. C++_归并排序(纯C版)

    #include <iostream> #include <stdlib.h> using namespace std; int compared(const void *ke ...

  2. [计蒜客T2238]礼物_线段树_归并排序_概率期望

    礼物 题目大意: 数据范围: 题解: 这题有意思啊($md$卡常 直接做怎么做? 随便上个什么东西,维护一下矩阵乘和插入,比如说常数还算小的$KD-Tree$(反正我是没见人过过 我们漏掉了一个条件, ...

  3. 稳定排序nlogn之归并排序_一维,二维

    稳定排序nlogn之归并排序_一维,二维 稳定排序:排序时间稳定的排序 稳定排序包括:归并排序(nlogn),基数排序[设待排序列为n个记录,d个关键码,关键码的取值范围为radix,则进行链式基数排 ...

  4. 归并排序_分治算法 (白书P226)

    #include<iostream> #include<cstdio> #include<algorithm> using namespace std; int a ...

  5. 算法 排序NB二人组 堆排序 归并排序

    参考博客:基于python的七种经典排序算法     常用排序算法总结(一) 序前传 - 树与二叉树 树是一种很常见的非线性的数据结构,称为树形结构,简称树.所谓数据结构就是一组数据的集合连同它们的储 ...

  6. [Swift]八大排序算法(七):归并排序

    排序分为内部排序和外部排序. 内部排序:是指待排序列完全存放在内存中所进行的排序过程,适合不太大的元素序列. 外部排序:指的是大文件的排序,即待排序的记录存储在外存储器上,待排序的文件无法一次装入内存 ...

  7. Scala 实现快速排序和归并排序

    def quickSort1(array: Array[Int]): Array[Int] = { def swap(x: Int, y: Int): Unit = { val tmp = array ...

  8. Scala实现冒泡排序、归并排序和快速排序

    1.冒泡排序 def sort(list: List[Int]): List[Int] = list match { case List() => List() case head :: tai ...

  9. Go基于协程的归并排序简单实现

    归并排序这个可能很多人都不知道,今天用Go语言简单的实现下,其他语言可能要基于线程来实现. //产生一个源 func ArraySource(a ...int) chan int{ out :=mak ...

随机推荐

  1. ASM基本操作

    1. 添加一个磁盘组 SQL> create diskgroup recover external redundancy disk 'ORCL:kel3'; Diskgroup created. ...

  2. 树形DP+树状数组 HDU 5877 Weak Pair

    //树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...

  3. 多级联动导航栏(top导航)

    http://www.dynamicdrive.com/style/csslibrary/item/jquery_multi_level_css_menu_2/ This is a multi-lev ...

  4. mapreduce的调度算法和job调优

    调度算法: mapreduce当有很多的作业在执行的时候,是按照什么顺序去执行的? 调度算法顺序需要关注: 1.提高作业的吞吐量. 2.要考虑优先级. 三种调度器:如果作业跑不完,并且机器资源利用率比 ...

  5. Junit。。。

    keep the bar green to keep the code clean.

  6. CentOS上firefox安装flash

    CentOS下firefox安装flash说明 CentOS下自带了firefox,但没有flash插件的,按它自己的提示安装不成功,需要手动安装,如下: 1.打开flash官网,http://lab ...

  7. 【转】Maven实战(三)---插件动态打包

    原博文出于:http://blog.csdn.net/liutengteng130/article/details/41622013    感谢! maven把项目的构建划分为不同的生命周期(life ...

  8. [iOS 多线程 & 网络 - 2.0] - 发送接收 服务器信息

    A.搭建java服务器 使用eclipse.tomcat和struts2框架搭建一个简单的服务器 1.准备好合适版本的JDK.eclipse EE.tomcat.struts2 框架包 2.配置JDK ...

  9. [iOS 多线程 & 网络 - 1.0] - 多线程概述

    A.进程 什么是进程进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开QQ.Xcode,系统就会分别启动2个进程 通过"活 ...

  10. Scala List的排序函数sortWith

    //原始方法: //val list=List("abc","bcd","cde") scala> list.sortWith( (s ...