读书笔记:算法导论第2章 第1节 Insertion sort

1. We use loop invariants to help us understand why an algorithm is correct. We must show three things about a loop invariant:

  • Initialization: It is true prior to the first iteration of the loop.
  • Maintenance: If it is true before an iteration of the loop, it remains true before the next iteration.
  • Termination: When the loop terminates, the invariant gives us a useful property that helps show that the algorithm is correct.

2. Exercise2.1-1:

Question: Insertion Sort  a[] = {31, 41, 59, 26, 41, 58} by increasing order.

Source code:

#include <stdio.h>

void insertionSort(int *a, int len)
{
    int i, j, key;
    for(i = 1; i < len; i++){
        key = a[i];
        j = i - 1;
        while(j >= 0 && a[j] > key){
            a[j + 1] = a[j];
            j--;
        }
        a[j + 1] = key;
    }
}
int main(int argc, char *argv[])
{
    int a[] = {31, 41, 59, 26, 41, 58};
    int len = sizeof(a)/ sizeof(a[0]);
    int i;

    printf("Before insertion sort:\n");
    printf("a[] = {");
    for(i = 0; i < len - 1; i++)
        printf("%d, ", a[i]);
    printf("%d}\n", a[len - 1]);

    insertionSort(a, len);

    printf("After insertion sort:\n");
    printf("a[] = {");
    for(i = 0; i < len - 1; i++)
        printf("%d, ", a[i]);
    printf("%d}\n", a[len - 1]);

    return 0;
}


3.Exercise2.1-2:

Question:Rewrite the INSERTION -SORT procedure to sort into nonincreasing instead of decreasing order.

Source code:

void insertionSort(int *a, int len)
{
    int i, j, key;
    for(i = 1; i < len; i++){
        key = a[i];
        j = i - 1;
        while(j >= 0 && a[j] < key){
            a[j + 1] = a[j];
            j--;
        }
        a[j + 1] = key;
    }
}

4.Exercise 2.1-3

Question:

Consider the searching problem:
Input: A sequence of n numbers A={a1,a2, ...,an} and a value v.
Output: An index i such that v = a[i] or the special value NIL if v 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.

pseudo code:

LINEAR-SEARCH(A):
1   for i = 0 to A.length - 1
2          if v == A[i]
3                print  i
4   if i==A.length
5          v == NIL


5. Exercise 2.1-4
Question: 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 .


Source code:

#include <stdio.h>

void binary_array_addition(int *a, int *b, int *c, int len)
{
    int temp, carry, i;
    carry = 0;
    for(i = len - 1; i >= 0; i--){
        temp = a[i] + b[i] + carry;
        if(temp == 3){
            carry = 1;
            c[i + 1] = 1;
        }
        else if(temp == 2)
            carry = 1;
        else if(temp == 1){
            carry = 0;
            c[i + 1] = 1;
        }
    }
    if(carry == 1)
        c[i + 1] = 1;
}

int main(int argc, char *argv[])
{
    int i;
    int a[] = {1, 0, 1, 1, 0};
    int b[] = {1, 1, 0, 1, 1};
    int len = sizeof a / sizeof a[0];
    int c[sizeof a / sizeof a[0] + 1] = {0};

    binary_array_addition(a, b, c, len);

    printf("a[] + b[] = ");
    for(i = 0; i < len; i++)
        printf("%d", a[i]);
    printf(" + ");
    for(i = 0; i < len; i++)
        printf("%d", b[i]);
    printf(" = ");
    for(i = 0; i < len + 1; i++)
        printf("%d", c[i]);
    putchar('\n');

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值