M erge sort is based on the divide-and-conquer paradigm. Its worst-case running time has a lower order of growth
than insertion sort. Since we are dealing with subproblems, we state each subproblem as sorting a subarray A[p .. r].
Initially, p = 1 and r = n, but these values change as we recurse through subproblems.

To sort A[p .. r]:
1. Divide Step
If a given array A has zero or one element, simply return; it is already sorted. Otherwise, split A[p .. r]
into two subarrays A[p .. q] and A[q + 1 .. r], each containing about half of the elements of A[p .. r].
That is, q is the halfway point of A[p .. r].
2. Conquer Step
Conquer by recursively sorting the two subarrays A[p .. q] and A[q + 1 .. r].
3. Combine Step
Combine the elements back in A[p .. r] by merging the two sorted subarrays A[p .. q] and A[q + 1 .. r] into
a sorted sequence. To accomplish this step, we will define a procedure MERGE (A, p, q, r).
Note that the recursion bottoms out when the subarray has just one element, so that it is trivially sorted.

归并操作

归并操作(merge),也叫归并算法,指的是将两个顺序序列合并成一个顺序序列的方法。
如 设有数列{6,202,100,301,38,8,1}
初始状态:6,202,100,301,38,8,1
第一次归并后:{6,202},{100,301},{8,38},{1},比较次数:3;
第二次归并后:{6,100,202,301},{1,8,38},比较次数:4;
第三次归并后:{1,6,8,38,100,202,301},比较次数:4;
总的比较次数为:3+4+4=11,;
逆序数为14;
 
 /* C program for Merge Sort */
 #include<stdlib.h>
 #include<stdio.h>

 // Merges two subarrays of arr[].
 // First subarray is arr[l..m]
 // Second subarray is arr[m+1..r]
 void merge(int arr[], int l, int m, int r)
 {
     int i, j, k;
     ;
     int n2 = r - m;

     /* create temp arrays */
     int L[n1], R[n2];

     /* Copy data to temp arrays L[] and R[] */
     ; i < n1; i++)
         L[i] = arr[l + i];
     ; j < n2; j++)
         R[j] = arr[m + + j];

     /* Merge the temp arrays back into arr[l..r]*/
     i = ; // Initial index of first subarray
     j = ; // Initial index of second subarray
     k = l; // Initial index of merged subarray
     while (i < n1 && j < n2)
     {
         if (L[i] <= R[j])
         {
             arr[k] = L[i];
             i++;
         }
         else
         {
             arr[k] = R[j];
             j++;
         }
         k++;
     }

     /* Copy the remaining elements of L[], if there
     are any */
     while (i < n1)
     {
         arr[k] = L[i];
         i++;
         k++;
     }

     /* Copy the remaining elements of R[], if there
     are any */
     while (j < n2)
     {
         arr[k] = R[j];
         j++;
         k++;
     }
 }

 /* l is for left index and r is right index of the
 sub-array of arr to be sorted */
 void mergeSort(int arr[], int l, int r)
 {
     if (l < r)
     {
         // Same as (l+r)/2, but avoids overflow for
         // large l and h
         ;

         // Sort first and second halves
         mergeSort(arr, l, m);
         mergeSort(arr, m+, r);

         merge(arr, l, m, r);
     }
 }

 /* UTILITY FUNCTIONS */
 /* Function to print an array */
 void printArray(int A[], int size)
 {
     int i;
     ; i < size; i++)
         printf("%d ", A[i]);
     printf("\n");
 }

 /* Driver program to test above functions */
 int main()
 {
     , , , , , };
     ]);

     printf("Given array is \n");
     printArray(arr, arr_size);

     mergeSort(arr, , arr_size - );

     printf("\nSorted array is \n");
     printArray(arr, arr_size);
     ;
 }

归并排序(merge sort)的更多相关文章

  1. 经典排序算法 - 归并排序Merge sort

    经典排序算法 - 归并排序Merge sort 原理,把原始数组分成若干子数组,对每个子数组进行排序, 继续把子数组与子数组合并,合并后仍然有序,直到所有合并完,形成有序的数组 举例 无序数组[6 2 ...

  2. 排序算法二:归并排序(Merge sort)

    归并排序(Merge sort)用到了分治思想,即分-治-合三步,算法平均时间复杂度是O(nlgn). (一)算法实现 private void merge_sort(int[] array, int ...

  3. 连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort)

    连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort) 1,起泡排序(bubble sort),大致有三种算法 基本版,全扫描. 提前终止版,如果发现前区里没有发生交换 ...

  4. STL 源代码剖析 算法 stl_algo.h -- merge sort

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie merge sort ----------------------------------- ...

  5. 归并排序——Merge Sort

    基本思想:参考 归并排序是建立在归并操作上的一种有效的排序算法.该算法是采用分治法的一个非常典型的应用.首先考虑下如何将2个有序数列合并.这个非常简单,只要从比较2个数列的第一个数,谁小就先取谁,取了 ...

  6. 归并排序Merge Sort

    //C语言实现 void mergeSort(int array[],int first, int last) { if (first < last)//拆分数列中元素只剩下两个的时候,不再拆分 ...

  7. 归并排序Merge sort(转)

    原理,把原始数组分成若干子数组,对每一个子数组进行排序, 继续把子数组与子数组合并,合并后仍然有序,直到全部合并完,形成有序的数组 举例 无序数组[6 2 4 1 5 9] 先看一下每个步骤下的状态, ...

  8. 数据结构 - 归并排序(merging sort)

    归并排序(merging sort): 包含2-路归并排序, 把数组拆分成两段, 使用递归, 将两个有序表合成一个新的有序表. 归并排序(merge sort)的时间复杂度是O(nlogn), 实际效 ...

  9. 数据结构 - 归并排序(merging sort) 具体解释 及 代码

    归并排序(merging sort) 具体解释 及 代码 本文地址: http://blog.csdn.net/caroline_wendy 归并排序(merging sort): 包括2-路归并排序 ...

随机推荐

  1. 使用jQuery解析JSON数据(由ajax发送请求到php文件处理数据返回json数据,然后解析json写入html中呈现)

    在上一篇的Struts2之ajax初析中,我们得到了comments对象的JSON数据,在本篇中,我们将使用jQuery进行数据解析. 我们先以解析上例中的comments对象的JSON数据为例,然后 ...

  2. --hdu 1800 Flying to the Mars(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Ac code: #include<stdio.h> #include<std ...

  3. linux 定时 svn 代码更新,配置文件不修改

    普通参数: 普通参数为正常的传参数:  例子:  f1("111") 指定参数: 指定参数为指定哪个参数给函数方法里面某个形式参数专用,优点:不受传参数的位置约束.   例子:  ...

  4. HTTP协议详解(一直在用可是这篇太好了转一下)

    引言 HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1. ...

  5. 运用加密技术保护Java源代码/定制ClassLoader

    为什么要加密? 对于传统的C或C++之类的语言来说,要在Web上保护源代码是很容易的,只要不发布它就可以.遗憾的是,Java程序的源代码很容易被别人偷看.只要有一个反编译器,任何人都可以分析别人的代码 ...

  6. 【iOS】app的生命周期

    对于iOS应用程序,关键的是要知道你的应用程序是否正在前台或后台运行.由于系统资源在iOS设备上较为有限,一个应用程序必须在后台与前台有不同的行为.操作系统也会限制你的应用程序在后台的运行,以提高电池 ...

  7. C++中析构函数的作用,

    如果构造函数打开了一个文件,最后不需要使用时文件就要被关闭.析构函数允许类自动完成类似清理工作,不必调用其他成员函数.析构函数也是特殊的类成员函数.简单来说,析构函数与构造函数的作用正好相反,它用来完 ...

  8. javascript仿天猫加入购物车动画效果

    javascript仿天猫加入购物车动画效果   注意:首先需要声明的是:代码原思路不是我写的,是在网上找的这种效果,自己使用代码封装了下而已:代码中都有注释,我们最主要的是理解抛物线的思路及在工作中 ...

  9. JQ系列:css操作

    JQ中的 .css()有三种使用方法: $('#p').css('width'); 取得ID为p的样式属性的width值;等同 return width $('#p').css('width','10 ...

  10. 获取window窗口大小

    窗口大小 跨浏览器确定一个窗口的大小不是一件简单的事.IE9+.Firefox.Safari.Opera和Chrome均为此提供了4个属性:innerWidth.innerHeight.outerWi ...