Divison and Recursion-MergeSort

#include<iostream>
using namespace std;

void DoMerge(int array[], int buff[], int begin, int middle, int end){
 int leftHalfBegin = begin;
    int leftHalfEnd   = middle;

int rightHalfBegin = middle+1;
    int rightHalfEnd   = end;
    int index = begin;

while (leftHalfBegin <= middle && rightHalfBegin <= end){
  if (array[leftHalfBegin] < array[rightHalfBegin]){
   buff[index] = array[leftHalfBegin];
   index++;
   leftHalfBegin++;
  }else{
   buff[index] = array[rightHalfBegin];
   index++;
   rightHalfBegin++;
  }
 }

while (leftHalfBegin <= middle){
  buff[index++] = array[leftHalfBegin++];
 }
 while(rightHalfBegin <= end)
 {
  buff[index++] = array[rightHalfBegin++];
 }

for(int i = begin; i<= end;i++){
  array[i] = buff[i];
 }

}
void MergeSort(int array[], int buff[], int begin, int end)
{
 int middle = (begin+end)/2;
 if (begin < end){
  MergeSort(array, buff, begin, middle);
  MergeSort(array, buff, middle + 1, end);
  DoMerge(array, buff, begin, middle, end);
 }
}

int main(){
 int array[6]={3,5,9,8,7,5};
 int buff[6];
    MergeSort(array, buff, 0, 5);
    for(int i =0;i<6;i++){cout<<array[i]<<endl;}

}

时间: 2024-10-09 17:14:24

Divison and Recursion-MergeSort的相关文章

6-15 Iterative Mergesort (25分)

How would you implement mergesort without using recursion? The idea of iterative mergesort is to start from N sorted sublists of length 1, and each time to merge a pair of adjacent sublists until one sorted list is obtained. You are supposed to imple

Python的最大递归深度错误 “maximum recursion depth exceeded while calling a Python object”

今天在写爬虫的时候,发现了一个诡异的事情,使用str方法强制转换一个BeautifulSoup对象成字符串的时候报错了,提示是"maximum recursion depth exceeded while calling a Python object",意思大致是"当调用该对象超过最大递归深度" 报错如下:   Traceback (most recent call last):   File "<stdin>", line 1, 

Mergesort

Mergesort is one of the best-known examples of the unility of the divide-and-conquer paradigm for efficent algorithm design. It is as good as Quicksort. #include<cstdio> #include<iostream> using namespace std; void Merge( int a[], int first, i

Scala Learning(3): Tail Recursion定义

关于尾递归 ,使用Scala的两个例子展示尾递归的定义和简单实现. 例子比较 求最大公约数的函数 def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b) 计算的展开是尾递归的, gcd(14, 21) -> if (21 == 0) 14 else gcd(21, 14 % 21) -> if (false) 14 else gcd(21, 14 % 21) -> gcd(21, 14 % 21) -> gcd

常见排序算法(一) MergeSort

算法思想灰常重要,常见的用到分治思想的算法包括快速排序,归并,二分搜搜,大整数乘法等(参考 http://blog.csdn.net/com_stu_zhang/article/details/7233761,归纳很到位) 简单用归并对一个数组排序 思路: 简单来说对一个数组,只要他的左右两部分都是有序的,那么简单合并就ok了,那么左右两部分可以进一步划分各自的左右两部分----明显就是要递归了 算法:归并排序 1. 将数组一分为二,subArray1 和subArray2 2. 归并排序sub

递归和迭代(Recursion and Iteration)

递归 特点:简而言之,递归就是应用程序调用自身.所以,存在预期收敛,才能使用递归(因为不能无限期递归调用下去). 优点:程序看着比较简单,比较容易实现. 缺点:递归要占用额外的栈空间,如果递归的深度比较大,那么占用的栈比较多,而且调用函数的时间也比较多,时空性都不好. 所以选择递归要考虑好处和缺点之间的权衡. 迭代 特点:通过步号寻找需要的信息,经典的例子比如C++中的for循环语句(迭代遍历程序). 优点:开销只因循环的增加而相应增加,没有额外的空间开销和时间开销. 缺点:编写复杂问题时可能程

Recursion

Recursion It is legal for one function to call another; it is also legal for a function to call itself. It may not be obvious why what is a good thing, but it turns out to be one of the most magical things a program can do. For example: If a recursio

排序--MergeSort 归并排序?

MergeSort 's implementation MergetSort 的中心思想就是分治思想,通过解决每一个小问题来解决大问题 假设你有2个已经排好序的数组 数组[ 4 ][ 8 ] 和 数组[ 5 ][ 7 ] 进行排序 4 是第一个数组最小的值  和 第二个数组最小的值5 进行比较 4 < 5 所以最小的元素是4 排完第一个元素后是[ 4 ][ null ][ null ][ null ] 接着排第二个元素, 8 和 5 比较.5 比较小 所以是 [ 4 ][ 5 ][ null ]

Collections.sort()中的mergeSort归并排序

@SuppressWarnings({"unchecked", "rawtypes"}) private static void mergeSort(Object[] src, Object[] dest, int low, int high, int off) { int length = high - low; // Insertion sort on smallest arrays if (length < INSERTIONSORT_THRESHOLD

用Scala编写MergeSort

object MergeSort { def msort[T](less: (T, T) => Boolean )(xs: List[T]): List[T] = { def merge(xs: List[T], ys: List[T]): List[T] = { (xs, ys) match { case (Nil, _) => ys case (_, Nil) => xs case (x :: xs1, y :: ys1) => if (less(x, y)) x :: mer