• 09-排序2 Insert or Merge(浙大数据结构)


    09-排序2 Insert or Merge

    分数 25

    作者 陈越

    单位 浙江大学

    According to Wikipedia:

    Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

    Merge sort works as follows: Divide the unsorted list into N sublists, each containing 1 element (a list of 1 element is considered sorted). Then repeatedly merge two adjacent sublists to produce new sorted sublists until there is only 1 sublist remaining.

    Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?

    Input Specification:

    Each input file contains one test case. For each case, the first line gives a positive integer N (≤100). Then in the next line, N integers are given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, print in the first line either "Insertion Sort" or "Merge Sort" to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resuling sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

    Sample Input 1:

    1. 10
    2. 3 1 2 8 7 5 9 4 6 0
    3. 1 2 3 7 8 5 9 4 6 0

    Sample Output 1:

    1. Insertion Sort
    2. 1 2 3 5 7 8 9 4 6 0

    Sample Input 2:

    1. 10
    2. 3 1 2 8 7 5 9 4 0 6
    3. 1 3 2 8 5 7 4 9 0 6

    Sample Output 2:

    1. Merge Sort
    2. 1 2 3 8 4 5 7 9 0 6

     solve one:

    1. #include <iostream>
    2. using namespace std;
    3. const int maxn = 110;
    4. int a[maxn],res[maxn];
    5. int b[maxn];
    6. int n;
    7. int ans=0;
    8. bool InsertSort(int *A,int num)
    9. {
    10. for(int i=1;i<num;++i)
    11. {
    12. int temp=A[i],j;
    13. for(j=i;j>0 && A[j-1]>temp;--j)
    14. {
    15. A[j]=A[j-1];
    16. }
    17. A[j]=temp;
    18. int flag=0;
    19. for(int i=0;i<num;++i)
    20. if(res[i]==A[i])
    21. ++flag;
    22. if(flag==num)
    23. {
    24. if(i!=num-1)
    25. {
    26. ++i;
    27. int tem=A[i],j;
    28. for(j=i;j>0 && A[j-1]>tem;--j)
    29. {
    30. A[j]=A[j-1];
    31. }
    32. A[j]=tem;
    33. }
    34. return true;
    35. }
    36. }
    37. return false;
    38. }
    39. void Merge(int *A,int l1,int r1,int l2,int r2)
    40. {
    41. int tem[r2-l1+1];
    42. int index=0,l=l1;;
    43. while(l1<=r1 && l2<=r2)
    44. {
    45. if(A[l1]<=A[l2])
    46. tem[index++]=A[l1++];
    47. else
    48. tem[index++]=A[l2++];
    49. }
    50. while(l1<=r1)
    51. tem[index++]=A[l1++];
    52. while(l2<=r2)
    53. tem[index++]=A[l2++];
    54. for(int i=0;i<index;++i)
    55. A[i+l]=tem[i];
    56. }
    57. void MSort(int *A,int l,int r) //MSort必须用迭代写法,用递归写法不能得到正确的序列
    58. {
    59. int flag=0;
    60. for(int step=1;step<n;step+=step)
    61. {
    62. for(int i=l;(i+step)<=r;i+=step*2)
    63. {
    64. Merge(A,i,i+step-1,i+step,min(n-1,i+step*2-1));
    65. }
    66. if(flag==1)
    67. return;
    68. for(int i=0;i<n;++i)
    69. {
    70. if(A[i]==res[i])
    71. flag=1;
    72. else
    73. {
    74. flag=0;
    75. break;
    76. }
    77. }
    78. }
    79. }
    80. void MergeSort(int *A,int num)
    81. {
    82. MSort(A,0,num-1);
    83. }
    84. int main()
    85. {
    86. cin >> n;
    87. for(int i=0;i<n;++i)
    88. {
    89. cin >> a[i];
    90. b[i]=a[i];
    91. }
    92. for(int i=0;i<n;++i)
    93. cin >> res[i];
    94. if(InsertSort(a,n)==true)
    95. {
    96. puts("Insertion Sort");
    97. for(int i=0;i<n;++i)
    98. {
    99. if(i==0)
    100. cout << a[i];
    101. else
    102. cout << ' ' << a[i];
    103. }
    104. cout << endl;
    105. }
    106. else
    107. {
    108. puts("Merge Sort");
    109. MergeSort(b,n);
    110. for(int i=0;i<n;++i)
    111. {
    112. if(i==0)
    113. cout << b[i];
    114. else
    115. cout << ' ' << b[i];
    116. }
    117. cout << endl;
    118. }
    119. return 0;
    120. }

    solve two:

    1. /**
    2. //sort 替代Merge;
    3. #include <iostream>
    4. #include <algorithm>
    5. using namespace std;
    6. const int maxn = 110;
    7. int a[maxn],res[maxn];
    8. int b[maxn];
    9. int n;
    10. int ans=0;
    11. bool InsertSort(int *A,int num)
    12. {
    13. int flag=0;
    14. for(int i=1;i<num;++i)
    15. {
    16. int temp=A[i],j;
    17. for(j=i;j>0 && A[j-1]>temp;--j)
    18. {
    19. A[j]=A[j-1];
    20. }
    21. A[j]=temp;
    22. if(flag==1)
    23. return true;
    24. for(int i=0;i<num;++i)
    25. {
    26. if(res[i]==A[i])
    27. flag=1;
    28. else
    29. {
    30. flag=0;
    31. break;
    32. }
    33. }
    34. }
    35. return false;
    36. }
    37. void MSort(int *A,int l,int r) //MSort必须用迭代写法,用递归写法不能得到正确的序列
    38. {
    39. int flag=0;
    40. for(int step=1;step<=n;step+=step)
    41. {
    42. for(int i=l;(i+step)<=r;i+=step*2)
    43. {
    44. sort(A+i,A+min(i+step*2,n));
    45. }
    46. if(flag==1)
    47. return;
    48. for(int i=0;i<n;++i)
    49. {
    50. if(A[i]==res[i])
    51. flag=1;
    52. else
    53. {
    54. flag=0;
    55. break;
    56. }
    57. }
    58. }
    59. }
    60. void MergeSort(int *A,int num)
    61. {
    62. MSort(A,0,num-1);
    63. }
    64. int main()
    65. {
    66. cin >> n;
    67. for(int i=0;i<n;++i)
    68. {
    69. cin >> a[i];
    70. b[i]=a[i];
    71. }
    72. for(int i=0;i<n;++i)
    73. cin >> res[i];
    74. if(InsertSort(a,n)==true)
    75. {
    76. puts("Insertion Sort");
    77. for(int i=0;i<n;++i)
    78. {
    79. if(i==0)
    80. cout << a[i];
    81. else
    82. cout << ' ' << a[i];
    83. }
    84. cout << endl;
    85. }
    86. else
    87. {
    88. puts("Merge Sort");
    89. MergeSort(b,n);
    90. for(int i=0;i<n;++i)
    91. {
    92. if(i==0)
    93. cout << b[i];
    94. else
    95. cout << ' ' << b[i];
    96. }
    97. cout << endl;
    98. }
    99. return 0;
    100. }
    101. */

  • 相关阅读:
    Configuration涉及的Full&Lite模式
    【leetcode】链表的中间节点
    【华为机试真题 JAVA】删除字符串中字符最少字符-100
    Spring Security 和 Shiro 该如何选择?
    大数据之DStream 转换 完整使用 (第十四章)
    Pytest系列-测试用例前后置固件setup和teardown的介绍和使用(2)
    jquery datatable固定列
    C++中不能重载为友元函数的四个运算符
    Redis之持久化RDB&AOF
    如何在小程序中设置导航栏文字颜色和背景颜色
  • 原文地址:https://blog.csdn.net/qq_51825761/article/details/126676155