您的位置:首页 > 其它

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

2014-01-12 09:22 821 查看
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.

[align=left]Termination: When the loop terminates, the invariant gives us a useful property that helps show that the algorithm is correct.[/align]

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法导论