2.1 insertion sort 《算法导论》答案

答案索引帖

2.1-1

Using Figure 2.2 as a model, illustrate the operation of INSERTION-SORT on the array A ={31; 41; 59; 26; 41; 58}

这一题很简单,自己想一想过程。(figure 2.2是升序)

2.1-2

Rewrite the INSERTION-SORT procedure to sort into non-increasing instead of non-decreasing order.

这一题用c语言代码来写:

  • 降序
#include <stdio.h>
int main()
{
    int i,j,right_hand;
    int card[10]={1,2,3,4,5,6,7,8,9,10};
    for(i=1;i<=9;i++)
    {
        right_hand=card[i];
        j=i-1;
        while(j>=0&&card[j]<right_hand)
        {
            card[j+1]=card[j];
            j--;
        }
        card[j+1]=right_hand;
    }
    for(i=0;i<10;i++)
        printf("%d ",card[i]);
    return 0;
}
  • 升序
#include <stdio.h>
int main()
{
    int i,j,right_hand;
    int card[10]={10,9,8,7,6,5,4,3,2,1};
    for(i=1;i<=9;i++)
    {
        right_hand=card[i];
        j=i-1;
        while(j>=0&&card[j]>right_hand)
        {
            card[j+1]=card[j];
            j--;
        }
        card[j+1]=right_hand;
    }
    for(i=0;i<10;i++)
        printf("%d ",card[i]);
    return 0;
}

2.1-3

Consider the searching problem:

Input: A sequence of n numbers A = {a1; a2; : : : ;} and a value.

Output: An index i such that v = A[i] or the special value NIL if does not appear in A.

Write pseudocode for linear search, which scans through the sequence, looking for v. Using a loop invariant, prove that your algorithm is correct. Make sure that your loop invariant fulfills the three necessary properties.

这一题并不简单:所谓的linear search在这里可以理解为简单的一个一个找。写出代码为:

#include <stdio.h>
#define NIL -1
int linear_search(int v,int *a)
{
    int i;
    for(i=0;i<=9;i++)
        if(a[i]==v)
            return i;
    return NIL;
}
int main()
{
    int p[10]={1,2,3,4,5,6,7,8,9,10};
    int result;
    int v;
    scanf("%d",&v);
    result=linear_search(v, p);
    printf("%d",result);
    return 0;
}

证明:

我在Google找了不少答案,但都不是令人那么满意。最终在Stack Overflow上找到了一个不错的答案。这里我都给大家放上来,以供大家参考,有可能需要科学上网,也需要懂一点点英文。

现在我自己来写一下:

  • loop invariant:v不等于a[j](\(0\le j\le i-1\)),但v有可能等于a[j](\(i\le j\le n\))。这个\(0\le j\le i-1\)非常关键,如果我们写成了\(0\le j\le i\)这一题有可能就证不出来了。
  • initialization:当i=0时,我们会发现我们需要证明的v不会出现在a[0]~a[-1]之中,但有可能出现在a[0]~a[n]之中。a[0]~a[-1]明显是个空集,v怎么可能会出现在空集之中,所以这里初始化正确。
  • maintenance:我们进行一次迭代。我们来分类讨论:1.如果v=a[i],函数就会被立刻返回i。不过这个仍然满足循环不变式。2.如果v!=a[i],会进行i会自加一次,仍然符合循环不变式。
  • termination:当我们进行最后一次迭代时,i=n+1时,v仍不在a[0]~a[n]之中,所以仍符合循环不变式。但此时会返回NIL。

2.1-4

Consider the problem of adding two n-bit binary integers, stored in two n-element arrays A and B. The sum of the two integers should be stored in binary form in an (n+1)-element array C. State the problem formally and write pseudocode for adding the two integers.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n,i,temp,temp1=0;
    printf("please input how much bit is number\n");
    scanf("%d",&n);
    int *a,*b,*c;
    a=malloc(n*sizeof(int));
    b=malloc(n*sizeof(int));
    c=malloc((n+1)*sizeof(int));
    printf("please input the first binary integer\n");//输入时,数字之间要加上空格。
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    printf("please input the second binary integer\n");
    for(i=0;i<n;i++)
        scanf("%d",&b[i]);
    for(i=n;i>=1;i--)
    {
        temp=a[i-1]+b[i-1]+temp1;
        if(temp>=2)
        {
            c[i]=a[i-1]+b[i-1]-2+temp1;
            temp1=1;
        }
        else
        {
            c[i]=a[i-1]+b[i-1]+temp1;
            temp1=0;
        }
    }
    if(temp1==1)
        c[0]=1;
    else
        c[0]=0;
    for(i=0;i<=n;i++)
        printf("%d",c[i]);
    printf("\n");
    return 0;
}

算法思想就是普通的二进制加法,逢二进一。

2.1 insertion sort 《算法导论》答案的更多相关文章

  1. 跳跃空间(链表)排序 选择排序(selection sort),插入排序(insertion sort)

    跳跃空间(链表)排序 选择排序(selection sort),插入排序(insertion sort) 选择排序(selection sort) 算法原理:有一筐苹果,先挑出最大的一个放在最后,然后 ...

  2. MIT算法导论——第五讲.Linear Time Sort

    本栏目(Algorithms)下MIT算法导论专题是个人对网易公开课MIT算法导论的学习心得与笔记.所有内容均来自MIT公开课Introduction to Algorithms中Charles E. ...

  3. 排序算法 - 插入排序(Insertion sort)

    插入排序对于少量元素的排序是很高效的,而且这个排序的手法在每个人生活中也是有的哦. 你可能没有意识到,当你打牌的时候,就是用的插入排序. 概念 从桌上的牌堆摸牌,牌堆内是杂乱无序的,但是我们摸上牌的时 ...

  4. 【算法】插入排序(Insertion Sort)

    (PS:内容参考MIT算法导论) 插入排序(Insertion Sort): 适用于数目较少的元素排序 伪代码(Pseudocode): 例子(Example): 符号(notation): 时间复杂 ...

  5. 经典排序算法 – 插入排序Insertion sort

    经典排序算法 – 插入排序Insertion sort  插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕. 插入排序方法分直接插入排序和折半插入排序两种, ...

  6. [算法] 插入排序 Insertion Sort

    插入排序(Insertion Sort)是一种简单直观的排序算法.它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入.插入排序在实现上,通常采用in-pla ...

  7. 《算法4》2.1 - 插入排序算法(Insertion Sort), Python实现

    排序算法列表电梯: 选择排序算法:详见 Selection Sort 插入排序算法(Insertion Sort):非常适用于小数组和部分排序好的数组,是应用比较多的算法.详见本文 插入排序算法的语言 ...

  8. 排序算法--插入排序(Insertion Sort)_C#程序实现

    排序算法--插入排序(Insertion Sort)_C#程序实现 排序(Sort)是计算机程序设计中的一种重要操作,也是日常生活中经常遇到的问题.例如,字典中的单词是以字母的顺序排列,否则,使用起来 ...

  9. 算法导论(CLRS)答案

    算法导论(CLRS)答案 Chapter Section I 1 2 p II 1 2 3 p III 1 2 p IV 1 2 3 4 p V 1 2 3 4 p VI 1 2 3 4 5 p VI ...

随机推荐

  1. ThinkPHP5.0更改框架的验证方法对象->validate(true)->save();

    我们更希望看到: // 新增对象至数据表 $result = $Teacher->validate(true)->save(); 而不是: // 新增对象至数据表 $result = $T ...

  2. 【初码干货】记一次分布式B站爬虫任务系统的完整设计和实施

    [初码文章推荐] 程序员的自我修养 Azure系列文章 阿里云系列文章 爬虫系列文章 [初码产品推荐] AlphaMS开发模式 闪送达城市中央厨房 今天带来一个有意思的东西-分布式B站爬虫任务系统 这 ...

  3. unity3D:游戏分解之角色移动和相机跟随

          游戏中,我们经常会有这样的操作,点击场景中某个位置,角色自动移动到那个位置,同时角色一直是朝向那个位置移动的,而且相机也会一直跟着角色移动.有些游戏,鼠标滑动屏幕,相机就会围绕角色旋转. ...

  4. WINDOWS程序设计对话框加载显示bmp图像及刷新

    参考文章:http://blog.csdn.net/wangjian8006/article/details/7464431 图片的加载与显示也是属于窗口绘制这一部分的.所以其代码要写在消息函数的WM ...

  5. C++构造函数(二)

    本篇是介绍C++的构造函数的第二篇(共二篇),属于读书笔记,对C++进行一个系统的复习. 复制构造函数 复制构造函数是构造函数的一种,也被称为拷贝构造函数,他只有一个参数,参数类型是本类的引用.默认构 ...

  6. JDBC 数据库连接操作——实习第三天

    今天开始了比较重量级的学习了,之前都是对于Java基础的学习和回顾.继续上篇的话题,<谁动了我的奶酪>,奉献一句我觉得比较有哲理的话:"学会自嘲了,而当人们学会自嘲,能够嘲笑自己 ...

  7. 将angular-ui-bootstrap的弹出框定义成一个服务的实践

    定义一个弹出框的服务:alert_boxdefiine(["app"],function(mainapp){ mainapp.controller('ModalInstanceCt ...

  8. 【JavaScript学习】-事件响应,让网页交互

    什么是事件: JavaScript 创建动态页面.事件是可以被 JavaScript 侦测到的行为. 网页中的每个元素都可以产生某些可以触发 JavaScript 函数或程序的事件. 比如说,当用户单 ...

  9. 【Android Developers Training】 86. 基于连接类型修改您的下载模式

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  10. Socket实现-Socket I/O

    Socket层的核心是两个函数:sosend()和soreceive().这两个函数负责处理所有Socket层和协议层之间的I/O操作. select()系统调用的作用是监控文件描述符的状态.一般用于 ...