Chinaunix首页 | 论坛 | 博客
  • 博客访问: 293342
  • 博文数量: 82
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 874
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-21 09:58
个人简介

traveling in cumputer science!!

文章分类

全部博文(82)

文章存档

2016年(13)

2015年(69)

我的朋友

分类: C/C++

2016-02-21 21:16:59

1.归并排序理论
基本思想是将两个已排序的表归并为一个有序表。归并排序也是分治策略的思想体现。
(1)按递归的方法,从上向下的角度来思考,把待排序表一分为二,然后将分割后的两个表再次平分,直到分割后只剩下一个元素为止,然后将分割的结果进行两两归并,直到递归程序结束,排序完成
数据举例:
int arrIN[] = {98, 17, 38, 19, 22, 69, 45, 12, 65, 359, 984, 65, 32, 36};
二次划分
98, 17, 38, 19, 22, 69, 45¥¥12, 65, 359, 984, 65, 32, 36
二次划分
98, 17, 38, 19¥¥ 22, 69, 45¥¥12, 65, 359, 984¥¥65, 32, 36
二次划分
98, 17,¥¥38, 19¥¥ 22, 69¥¥ 45¥¥12, 65,¥¥359, 984¥¥65, 32,¥¥36
二次划分
98,¥¥17,¥¥38,¥¥19¥¥ 22, ¥¥69¥¥ 45¥¥12, ¥¥65,¥¥359, ¥¥984¥¥65, ¥¥32,¥¥36
递归到底
回溯进行归并
17,98,¥¥19,38, ¥¥ 22, 69¥¥ 45¥¥12, 65,¥¥359, 984¥¥ 32,65,¥¥36
回溯进行归并
17, 19, 38, 98¥¥  22, 45, 69¥¥12, 65, 359, 984¥¥32, 36,65
回溯进行归并
17, 19, 22, 38, 45, 69, 98¥¥12, 32, 36, 65, 65, 359, 984
回溯进行归并
12 ,17 ,19 ,22 ,32 ,36 ,38 ,45 ,65 ,65 ,69 ,98 ,359 ,984
回溯到最外层,递归结束,排序结束!!
(2)按迭代的方法,从下向上的角度来思考,首先把待排序表中每个元素都看作一个有序表,然后将元素两两归并,然后把操作后的表中元素每对儿看作是一个有序表,然后再以对儿为基础两两归并,同理直到将所有元素归并为一个有序表,排序结束
数据举例:
int arrIN[] = {98, 17, 38, 19, 22, 69, 45, 12, 65, 359, 984, 65, 32, 36};
第一次迭代:每2个归并为一组
count 1 : ,17 ,98 ¥¥19 ,38 ¥¥22 ,69 ¥¥12 ,45 ¥¥65 ,359 ¥¥65 ,984 ¥¥32 ,36
第二次迭代:每4个归并为一组
count 2 : ,17 ,19 ,38 ,98 ¥¥12 ,22 ,45 ,69¥¥65 ,65 ,359 ,984 ¥¥32 ,36
第三次迭代:每8个归并为一组
count 3 : ,12 ,17 ,19 ,22 ,38 ,45 ,69 ,98¥¥32 ,36 ,65 ,65 ,359 ,984
第四次迭代:每16个归并为一组
count 4 : ,12 ,17 ,19 ,22 ,32 ,36 ,38 ,45 ,65 ,65 ,69 ,98 ,359 ,984
迭代结束,排序结束!!
2.C++代码实现:
递归代码:

点击(此处)折叠或打开

  1. /*****************************
  2. filename: recurMerge.cpp
  3. description: realize merge sort in recursion mode
  4. author: warrior mail:15933533880@163.com
  5. date:2016-2-21
  6. log:
  7. *****************************/

  8. /****** include *******/
  9. #include "recurMerge.h"
  10. using namespace std;
  11. /****** define *********/
  12. /****** variable ********/
  13. /****** function *******/
  14. /****************
  15. description: realize merge sort in recursion mode
  16. input:
  17.     int * arrIN
  18.     const int left
  19.     const int right
  20. output:
  21.     none
  22. ****************/
  23. void recurMergeSort(int* arrIN, const int leftSide, const int rightSide)
  24. {
  25.     int mid;
  26.     if(leftSide<rightSide)
  27.     {
  28.         mid = (leftSide+rightSide)/2;
  29.         recurMergeSort(arrIN, leftSide, mid);
  30.         recurMergeSort(arrIN, mid+1, rightSide);
  31.         mergeTwo2One(arrIN, leftSide,
  32.                     arrIN, leftSide, mid,
  33.                     arrIN, mid+1, rightSide);
  34.     }
  35. }
  36. /****************
  37. description: merge two sorted list to one sorted list
  38. input:
  39.     int * arrOUT, int arrOUT_start
  40.     int * arrIN1, int arrIN1_start, int arrIN1_end
  41.     int * arrIN2, int arrIN2_start, int arrIN2_end
  42. output:
  43.     none
  44. ****************/
  45. void mergeTwo2One(
  46.                 int * arrOUT, const int arrOUT_start,
  47.                 int * arrIN1, const int arrIN1_start, const int arrIN1_end,
  48.                 int * arrIN2, const int arrIN2_start, const int arrIN2_end)
  49. {
  50.     int i,j,k=0;
  51.     int tempARR[arrIN1_end-arrIN1_start + arrIN2_end-arrIN2_start +2];//= new int();
  52.     i = arrIN1_start;
  53.     j = arrIN2_start;
  54.     k = 0;
  55.     while((i<=arrIN1_end) && (j<=arrIN2_end))
  56.     {
  57.         if(arrIN1[i]<arrIN2[j])
  58.         {
  59.             tempARR[k] = arrIN1[i];
  60.             k++;
  61.             i++;
  62.         }
  63.         else
  64.         {
  65.             tempARR[k] = arrIN2[j];
  66.             k++;
  67.             j++;
  68.         }
  69.     }
  70.     while(i<=arrIN1_end)
  71.     {
  72.         tempARR[k] = arrIN1[i];
  73.         k++;
  74.         i++;
  75.     }
  76.     while(j<=arrIN2_end)
  77.     {
  78.         tempARR[k] = arrIN2[j];
  79.         k++;
  80.         j++;
  81.     }
  82.     for(int h=0; h<k; h++)
  83.     {
  84.         arrOUT[arrOUT_start+h] = tempARR[h];
  85.         cout<<" ,"<<tempARR[h];
  86.     }
  87.     cout<<endl;
  88.     //delete tempARR;
  89. }


点击(此处)折叠或打开

  1. #ifndef _RECURMERGE_H_
  2. #define _RECURMERGE_H_
  3. /****** include *******/
  4. #include <iostream>
  5. #include <stdio.h>
  6. #include<fstream>
  7. /****** define *********/
  8. /****** variable ********/
  9. /****** function *******/
  10. void recurMergeSort(int* arrIN, const int leftSide, const int rightSide);
  11. void mergeTwo2One(
  12.                 int * arrOUT, const int arrOUT_start,
  13.                 int * arrIN1, const int arrIN1_start, const int arrIN1_end,
  14.                 int * arrIN2, const int arrIN2_start, const int arrIN2_end);
  15. #endif // _RECURMERGE_H_

迭代代码:

点击(此处)折叠或打开

  1. /*****************************
  2. filename: recurMerge.cpp
  3. description: realize merge sort in iteration mode
  4. author: warrior mail:15933533880@163.com
  5. date:2016-2-21
  6. log:
  7. *****************************/

  8. /****** include *******/
  9. #include "iteraMerge.h"
  10. #include "recurMerge.h"
  11. using namespace std;
  12. /****** define *********/
  13. /****** variable ********/
  14. /****** function *******/
  15. /****************
  16. description: realize merge sort in iteration mode
  17. input:
  18.     int * arrIN
  19.     const int arrLength
  20. output:
  21.     none
  22. ****************/
  23. void iteraMergeSort(int* arrIN, const int arrLength)
  24. {
  25.     int stepLength = 1;
  26.     int currentIndex = 0;
  27.     int i=0;
  28.     //ofstream outputFile;
  29.     //outputFile.open("output.txt");
  30.     while(stepLength<arrLength)
  31.     {
  32.         stepLength *= 2;
  33.         mergeOnce(arrIN, arrLength, stepLength);
  34.         cout<<"count "<<int(log(stepLength)/log(2))<<" :";
  35.         //outputFile<<"count "<<int(log(stepLength)/log(2))<<" :";
  36.         for(i=0; i<arrLength; i++)
  37.         {
  38.             cout<<" ,"<<arrIN[i];
  39.             //outputFile<<" ,"<<arrIN[i];
  40.         }
  41.         //outputFile<<"\n";
  42.         cout<<endl;
  43.     }
  44.     //outputFile.close();
  45.     for(i=0; i<arrLength; i++)
  46.     {
  47.         cout<<" ,"<<arrIN[i];
  48.     }
  49.     cout<<endl;
  50. }

  51. /****************
  52. description: excute once merge sort
  53. input:
  54.     int * arrIN
  55.     const int arrLength
  56.     const int stepLongth
  57. output:
  58.     none
  59. ****************/
  60. void mergeOnce(int* arrIN, const int arrLength, const int stepLength)
  61. {
  62.     int iteratorNum = stepLength;
  63.     int i=0;
  64.     if(arrLength%iteratorNum == 0)
  65.     {
  66.         for(i=0; i<arrLength; i+=iteratorNum)
  67.         {
  68.             mergeTwo2One(
  69.                         arrIN, i,
  70.                         arrIN, i, i+iteratorNum/2-1,
  71.                         arrIN, i+iteratorNum/2, i+iteratorNum-1);
  72.         }
  73.     }
  74.     else
  75.     {
  76.         for(i=0; i<(arrLength-arrLength%iteratorNum); i+=iteratorNum)
  77.         {
  78.             mergeTwo2One(
  79.                         arrIN, i,
  80.                         arrIN, i, i+iteratorNum/2-1,
  81.                         arrIN, i+iteratorNum/2, i+iteratorNum-1);
  82.         }
  83.         if(arrLength%iteratorNum > iteratorNum/2)
  84.         {
  85.             mergeTwo2One(
  86.                         arrIN, i,
  87.                         arrIN, i, i+iteratorNum/2-1,
  88.                         arrIN, i+iteratorNum/2, arrLength-1);
  89.         }
  90.     }

  91. }


点击(此处)折叠或打开

  1. #ifndef _ITERAMERGE_H_
  2. #define _ITERAMERGE_H_
  3. /****** include *******/
  4. #include <iostream>
  5. #include <stdio.h>
  6. #include <math.h>
  7. #include<fstream>
  8. /****** define *********/
  9. /****** variable ********/
  10. /****** function *******/
  11. void iteraMergeSort(int* arrIN, const int arrLength);
  12. void mergeOnce(int* arrIN, const int arrLength, const int stepLength);
  13. #endif // _ITERAMERGE_H_

主函数:

点击(此处)折叠或打开

  1. #include "recurMerge.h"
  2. #include "iteraMerge.h"

  3. using namespace std;

  4. int main()
  5. {
  6.     int arrIN1[] = {98, 17, 38, 19, 22, 69, 45, 12, 65, 359, 984, 65, 32, 36};
  7.     int arrIN2[] = {98, 17, 38, 19, 22, 69, 45, 12, 65, 359, 984, 65, 32, 36};
  8.     int leftSide = 0;
  9.     int rightSide = sizeof(arrIN1)/sizeof(int) - 1;
  10.     cout << "arr lenght :" << sizeof(arrIN2)/sizeof(int)<<endl;
  11.     recurMergeSort(arrIN1, leftSide, rightSide);
  12.     iteraMergeSort(arrIN2, sizeof(arrIN2)/sizeof(int));

  13.     return 0;
  14. }

3.总结
(1)思考了递归与迭代的两种逆向思维
(2)理解了分治策略的实践
(3)程序中尝试了C++中I/O文件操作方法
     参考链接:http://blog.csdn.net/mak0000/article/details/3230199
阅读(2638) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~