读书笔记:算法导论第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:
3.Exercise2.1-2:
Question:Rewrite the INSERTION -SORT procedure to sort into nonincreasing instead of decreasing order.
Source code:
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:
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; }
相关文章推荐
- Red-Black Tree 的Java实现
- 逆序对
- 编程资源共享导航(更新)
- Asymptotic Notation and Recurrences
- 读书笔记:算法导论第1章
- Chapter1,Problems 1-1
- 目录说明
- Chapter 2, Exercise 2.3-7
- Chapter 2, Problem 2-4
- Chapter 3, Exercise 3.2.3
- Chapter 3, Exercise 3.2.4
- 对归并排序的优化和思考
- 不同情景下的快速排序的优化
- Young tableaus 的一种实现方法
- 单源最短路径 : Bellman-Ford 算法
- 最小生成树 : Kruskal 算法
- 最小生成树 : Prim 算法
- 每对顶点的最短路径 : 基本算法
- 每对顶点的最短路径 : Johnson 算法
- 最大流 : FordFulkerson 算法