当前位置: 首页 > news >正文

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:

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

Sample Output 1:

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

Sample Input 2:

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

Sample Output 2:

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

 solve one:

#include <iostream>

using namespace std;

const int maxn = 110;
int a[maxn],res[maxn];
int b[maxn];

int n;
int ans=0;

bool InsertSort(int *A,int num)
{
    for(int i=1;i<num;++i)
    {
        int temp=A[i],j;
        for(j=i;j>0 && A[j-1]>temp;--j)
        {
            A[j]=A[j-1];
        }
        A[j]=temp;

        int flag=0;
        for(int i=0;i<num;++i)
            if(res[i]==A[i])
                ++flag;
        if(flag==num)
        {
            if(i!=num-1)
            {
                ++i;
                int tem=A[i],j;
                for(j=i;j>0 && A[j-1]>tem;--j)
                {
                    A[j]=A[j-1];
                }
                A[j]=tem;
            }
            return true;
        }
    }

    return false;
}

void Merge(int *A,int l1,int r1,int l2,int r2)
{
    int tem[r2-l1+1];
    int index=0,l=l1;;
    while(l1<=r1 && l2<=r2)
    {
        if(A[l1]<=A[l2])
            tem[index++]=A[l1++];
        else
            tem[index++]=A[l2++];
    }

    while(l1<=r1)
        tem[index++]=A[l1++];
    while(l2<=r2)
        tem[index++]=A[l2++];

    for(int i=0;i<index;++i)
        A[i+l]=tem[i];
}

void MSort(int *A,int l,int r)  //MSort必须用迭代写法,用递归写法不能得到正确的序列
{
    int flag=0;
    for(int step=1;step<n;step+=step)
    {
        for(int i=l;(i+step)<=r;i+=step*2)
        {
            Merge(A,i,i+step-1,i+step,min(n-1,i+step*2-1));
        }

        if(flag==1)
            return;

        for(int i=0;i<n;++i)
        {
            if(A[i]==res[i])
                flag=1;
            else
            {
                flag=0;
                break;
            }
        }
    }
}

void MergeSort(int *A,int num)
{
    MSort(A,0,num-1);
}

int main()
{
    cin >> n;
    for(int i=0;i<n;++i)
    {
        cin >> a[i];
        b[i]=a[i];
    }
    for(int i=0;i<n;++i)
        cin >> res[i];

    if(InsertSort(a,n)==true)
    {
        puts("Insertion Sort");
        for(int i=0;i<n;++i)
        {
            if(i==0)
                cout << a[i];
            else
                cout << ' ' << a[i];
        }
        cout << endl;
    }
    else
    {
        puts("Merge Sort");

        MergeSort(b,n);
        for(int i=0;i<n;++i)
        {
            if(i==0)
                cout << b[i];
            else
                cout << ' ' << b[i];
        }
        cout << endl;

    }

    return 0;
}

solve two:


/**
// 用sort 替代Merge;
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 110;
int a[maxn],res[maxn];
int b[maxn];

int n;
int ans=0;

bool InsertSort(int *A,int num)
{
    int flag=0;
    for(int i=1;i<num;++i)
    {
        int temp=A[i],j;
        for(j=i;j>0 && A[j-1]>temp;--j)
        {
            A[j]=A[j-1];
        }
        A[j]=temp;

        if(flag==1)
            return true;

        for(int i=0;i<num;++i)
        {
            if(res[i]==A[i])
                flag=1;
            else
            {
                flag=0;
                break;
            }
        }
    }

    return false;
}


void MSort(int *A,int l,int r)  //MSort必须用迭代写法,用递归写法不能得到正确的序列
{
    int flag=0;
    for(int step=1;step<=n;step+=step)
    {
        for(int i=l;(i+step)<=r;i+=step*2)
        {
            sort(A+i,A+min(i+step*2,n));
        }

        if(flag==1)
            return;

        for(int i=0;i<n;++i)
        {
            if(A[i]==res[i])
                flag=1;
            else
            {
                flag=0;
                break;
            }
        }
    }
}

void MergeSort(int *A,int num)
{
    MSort(A,0,num-1);
}

int main()
{
    cin >> n;
    for(int i=0;i<n;++i)
    {
        cin >> a[i];
        b[i]=a[i];
    }
    for(int i=0;i<n;++i)
        cin >> res[i];

    if(InsertSort(a,n)==true)
    {
        puts("Insertion Sort");
        for(int i=0;i<n;++i)
        {
            if(i==0)
                cout << a[i];
            else
                cout << ' ' << a[i];
        }
        cout << endl;
    }
    else
    {
        puts("Merge Sort");

        MergeSort(b,n);
        for(int i=0;i<n;++i)
        {
            if(i==0)
                cout << b[i];
            else
                cout << ' ' << b[i];
        }
        cout << endl;

    }

    return 0;
}
*/

相关文章:

  • DPDK18.08上对VIRTIO IN ORDER 功能所做的优化
  • java毕业设计软件S2SH人力资源管理系统|人事薪资招聘oa人力请假考勤工资[包运行成功]
  • 关于C/C++中const关键字作用的一些想法
  • STC15单片机-通过PWM调整灯亮度
  • 9.3DDD之集成事件
  • 抖音获取商品原数据API接口展示
  • C++ 删除链表的倒数第N个结点
  • Pinia的使用
  • java — 认识String类的常用方法(上)
  • 高速专线不打烊 DPDK Hotplug助你实现设备动态管理
  • 【数据结构 二叉树 递归与非递归遍历】
  • 微信公众号消息推送教程
  • MiddleWare ❀ Zookeeper基础概述
  • 多任务学习算法在推荐系统中的应用
  • webpack5入门教程
  • JS中 map, filter, some, every, forEach, for in, for of 用法总结
  • [Vue CLI 3] 配置解析之 css.extract
  • Akka系列(七):Actor持久化之Akka persistence
  • Angular 4.x 动态创建组件
  • CSS魔法堂:Absolute Positioning就这个样
  • git 常用命令
  • Go 语言编译器的 //go: 详解
  • iOS高仿微信项目、阴影圆角渐变色效果、卡片动画、波浪动画、路由框架等源码...
  • React Transition Group -- Transition 组件
  • Redis 懒删除(lazy free)简史
  • SpringCloud(第 039 篇)链接Mysql数据库,通过JpaRepository编写数据库访问
  • Vue2.0 实现互斥
  • vue--为什么data属性必须是一个函数
  • win10下安装mysql5.7
  • 从零开始的webpack生活-0x009:FilesLoader装载文件
  • 多线程 start 和 run 方法到底有什么区别?
  • 分布式事物理论与实践
  • 分享一个自己写的基于canvas的原生js图片爆炸插件
  • 基于Volley网络库实现加载多种网络图片(包括GIF动态图片、圆形图片、普通图片)...
  • 记录一下第一次使用npm
  • 开源中国专访:Chameleon原理首发,其它跨多端统一框架都是假的?
  • 前端设计模式
  • 深入 Nginx 之配置篇
  • 微信公众号开发小记——5.python微信红包
  • k8s使用glusterfs实现动态持久化存储
  • 阿里云移动端播放器高级功能介绍
  • ​一些不规范的GTID使用场景
  • ​中南建设2022年半年报“韧”字当头,经营性现金流持续为正​
  • # 安徽锐锋科技IDMS系统简介
  • ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTr
  • #etcd#安装时出错
  • #HarmonyOS:软件安装window和mac预览Hello World
  • (1)安装hadoop之虚拟机准备(配置IP与主机名)
  • (2)nginx 安装、启停
  • (LeetCode 49)Anagrams
  • (八十八)VFL语言初步 - 实现布局
  • (二)linux使用docker容器运行mysql
  • (规划)24届春招和25届暑假实习路线准备规划
  • (数据结构)顺序表的定义
  • (一)appium-desktop定位元素原理