PTA 09-排序2 Insert or Merge(25 分)

09-排序2 Insert or Merge(25 分)

题目地址: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?

  • 输入格式:
    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.

  • 输出格式:
    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.


解题方法:
一种求归并排序错误解法,至于错误的原因还没想明白。。:

while (Flag2)
{    /* 循环排序Arr 直到Arr与MedArr相同,再进行一次排序 退出循环 */
 Flag2 = 0;
 for (i = 0; i < N; i++)
 {
     if (Arr[i] != MedArr[i])
     {
         Flag2 = 1;
         break;
     }
 }
 length *= 2;
 for (i = 0; i < N-length; i += length)
     sort(Arr+i, Arr+length+i);
 sort(Arr+i, Arr+N);
}

程序:

#include 
#include 
#include 
using namespace std;

int main(int argc, char const *argv[])
{
    int N, i, flag = 0, j, k;
    cin >> N;
    int Arr[N], MedArr[N];
    for (i = 0; i < N; i++)
        cin >> Arr[i];  // 输入起始序列
    for (i = 0; i < N; i++)
        cin >> MedArr[i];  // 输入中间结果序列
    for (i = 1; i < N && MedArr[i-1] <= MedArr[i]; i++);    
    for (j = i; j < N; j++)
        if (Arr[j] != MedArr[j])
            flag = 1;   // 如果后面序列不一致说明肯定不是插入排序
    if (!flag)
    {   /* 如果是插入排序 */
        printf("Insertion Sort\n");
        sort(MedArr, MedArr+i+1);   // 在进行一次插入排序(这里用sort模拟插入排序)
        for (k = 0; k < N; k++)
        {
            if (k != 0)     /* 格式化输出 */
                printf(" ");
            printf("%d", MedArr[k]);
        }
    }
    else
    {   /* 如果是归并排序 */
        printf("Merge Sort\n");
        int Flag2 = 1, length;
        for (length = 2; Flag2; length *= 2)
        {   /* 下面循环i += 2 必须这么写!*/
            /* 比如序列入下:
             24      59      14      67
            4  2    9  5    1  4    7  6
            如果不是i += 2 就会出现问题 */        
            for (i = 1; i < N/length; i += 1)  
                if (MedArr[i*length-1] > MedArr[i*length])
                {
                    Flag2 = 0;
                    break;
                }
        }
        cout << length << endl;
        for (i = 0; i < N-length; i += length)
            sort(MedArr+i, MedArr+length+i);
        sort(MedArr+i, MedArr+N);

        for (j = 0; j < N; j++)
        {
            if (j != 0)
                printf(" ");
            printf("%d", MedArr[j]);
        }
    }
    return 0;
}

你可能感兴趣的:(PTA)