第一次归并:

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

23

8

19

33

15

6

27

↑             ↑

i     j

最开始i指向a[0],j指向a[1],比较a[0],a[1]大小,并进行swap

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

8

23

19

33

6

15

27

↑                 ↑

i      j

i指向j时候归并结束

第二次归并:

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

8

23

19

33

6

15

27

↑                                     ↑

i            j

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

8

23

19

33

6

15

27

↑            ↑

i            j

i向后移动,直到找到比j指向的数大的那个数,此时,i之前的数是两段数值中最小的数

j原来的位置标记为Index,j向后移动找到比此时i指向的数大的数.

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

8

33

6

15

27

↑                    ↑               ↑

i     index    j

则此时a[Index,j]之间的数小于a[i,index]之间的数,将此两段数值互换,得到

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

8

23

33

6

15

27

↑               ↑

i     j

互换以后需要调整i的位置,将i向后移动index-j个单位【i+=(j-index)】

找到第一个比j指向的数大的,即此时i指向j,第二次归并结束。

第三次归并:

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

8

19

23

33

6

15

27

↑                                                                               ↑

i                         j

重复上述步骤,在此归并中,第一次比较时i不动,j指向a[5]

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

8

19

23

33

6

15

27

↑                                                                               ↑                ↑

i                        index    j

交换a[index,j]和a[i,index]两段数据,i+=(j-index)

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

6

8

19

23

33

15

27

↑                                                                              ↑                                                          i                        j

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

6

8

19

23

33

15

27

↑                                                       ↑

i                                                          j

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

6

8

19

23

27

↑                                                         ↑             ↑

i                 index   j

swap(i,j,index)

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

6

8

19

23

33

27

↑                                                       ↑

i                  j

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

6

8

15

19

23

33

27

↑             ↑

i    j

此时j任然指向a[6],index也指向a[6],交换

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

6

8

15

19

23

27

33

↑              ↑

i     j

i之后第一个比j指向的数大的数为33,此时i=j,归并结束

代码还没写完。。。。。脑子笨,感觉不大对,再改看看吧。pow这个函数总觉得用的不大对,这两天看看别人写的代码再找找思路看。

void in_palce_merge_sort(double* a,int n){
int index;
int edge=;
int i=;//第一段有序数组入口
int j=;//第二段有序数组入口
int inpoint=;
while(pow(,edge)<n){
while(a[i]<a[j]&&i<j) i++;
index=j;
while(a[j]<a[i]&&j<inpoint+pow(,edge)) j++;
swap(a[i],a[j],a[index]);
i+=(j-index);
if(i==j&&i<n){
i++; //此时往后移一位即新的有序数组的入口
j=i+pow(,edge); //等下考虑下2倍
inpoint=j; }
else if(i==n){
edge++;
i=;
j=pow(,edge);
inpoint=j;
}
}
}

关于swap(),个人想到的是需要一段临时空间作为暂存空间进行交换,如果这样的话感觉就失去原地归并的意义了,其他搜索到有手摇算法,等仔细研究下再来修改。

【原创】Algorithms:原地归并排序的更多相关文章

  1. STL:原地归并排序模板(InplaceMergeSort)

    原理:就是在归并排序上改进,以时间复杂度换空间复杂度,利用元素反转完成排序 具体过程如下: 具体操作看代码吧,应该没什么难度,主要是reverse要反转三次 typedef int Position; ...

  2. 【Algorithms】归并排序(merge sort)

    几个比较常见的排序算法里头,这个我是比较生疏的一个,有一天突然被问了一个问题是,归并排序最大的特点是什么,我才想起这个算法来.下午又看不进书啦,就实现一下,记下来. 归并排序采取的是分治策略,就是先将 ...

  3. Coursera Algorithms week3 归并排序 练习测验: Shuffling a linked list

    题目原文: Shuffling a linked list. Given a singly-linked list containing n items, rearrange the items un ...

  4. Coursera Algorithms week3 归并排序 练习测验: Counting inversions

    题目原文: An inversion in an array a[] is a pair of entries a[i] and a[j] such that i<j but a[i]>a ...

  5. Coursera Algorithms week3 归并排序 练习测验: Merging with smaller auxiliary array

    题目原文: Suppose that the subarray a[0] to a[n-1] is sorted and the subarray a[n] to a[2*n-1] is sorted ...

  6. java归并排序,单线程vs多线程

    一.什么是归并排序 归并排序又称合并排序,它是成功应用分治技术的一个完美例子.对于一个需要排序的数组A[0..n-1],归并排序把它一分为二:A[0..n/2-1]和A[n/2..n-1],并对每个子 ...

  7. 普林斯顿大学算法课 Algorithm Part I Week 3 归并排序 Mergesort

    起源:冯·诺依曼最早在EDVAC上实现 基本思想: 将数组一分为(Divide array into two halves) 对每部分进行递归式地排序(Recursively sort each ha ...

  8. 总结: Sort 排序算法

    排序总结 面试经验 硅谷某前沿小Startup面试时,问到的一个题目就是写一个快速排序算法.进而面试官问到了各种算法的算法复杂度,进而又问了Merge Sort 与 QuickSort 的优劣. 对排 ...

  9. 033.[转] Java 工程师成神之路 | 2019正式版

    Java 工程师成神之路 | 2019正式版 原创: Hollis Hollis 2月18日 https://mp.weixin.qq.com/s/hlAn6NPR1w-MAwqghX1FPg htt ...

随机推荐

  1. 大嫂的HTML

      <html>   <head>   <style type="text/css">   *{ margin: 0; padding: 0; ...

  2. 上位机控制led

    使用库函数,调试的结果在标红程序上,int main(void){  u8 a;                  u8 t;        u8 len;                u16 ti ...

  3. access手工注入

    1.判断数据类型 and exists (select * from msysobjects) >0 and exists (select * from sysobjects) >0 一般 ...

  4. kali linux karmetasploit配置

    原理分析:http://www.freebuf.com/articles/77055.html 转官方说明:https://www.offensive-security.com/metasploit- ...

  5. (转)innerHTML、innerText和outerHTML、outerText的区别

    原文:http://walsh.iteye.com/blog/261966 innerHTML.innerText和outerHTML.outerText的区别          博客分类: CSS/ ...

  6. 解决:Android4.3锁屏界面Emergency calls only - China Unicom与EMERGENCY CALL语义重复

    从图片中我们可以看到,这里在语义上有一定的重复,当然这是谷歌的原始设计.这个问题在博客上进行共享从表面上来看着实没有什么太大的意义,不过由于Android4.3在锁屏功能上比起老版本做了很大的改动,而 ...

  7. 多数求和(java)

    实验题目:从命令行接受多个数字,求和之后输出结果. 设计思想:命令行输入的字符会赋值给args数组,所以在命令行输入数字后,直接取出args的数组长度,作为循环语句的终点判断,然后利用循环将字符型改为 ...

  8. Spring处理器

    Spring容器内部工作机制 Spring的AbstractApplicationContext是ApplicationContext抽象实现类,该抽象类的refresh()方法定义了Spring容器 ...

  9. Magento Soap Api接口出错无法使用

    在给客户测试Magento Soap接口的时候出现如下错误提示. This page contains the following errors:error on line 3 at column 6 ...

  10. magento memcache缓存配置

    在app/etc/local.xml <global>配置段中添加 cache段配置 <config> <global> <install> <d ...