读书笔记:算法导论第2章 第1节 Insertion sort
来源:互联网 发布:福士苍汰 知乎 编辑:程序博客网 时间:2024/06/05 11:11
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;}
- 读书笔记:算法导论第2章 第1节 Insertion sort
- 读书笔记:算法导论第1章
- 读书笔记:算法导论第2章 第2节分析算法
- 【算法导论】2-1 插入排序insertion-sort.cpp
- 读书笔记:《算法导论》,第2章:算法基础
- 算法导论-第2章
- 算法导论-第1章
- 算法导论第1章
- 《算法导论》读书笔记之第12章 二叉查找树
- 《算法导论》读书笔记之第6章 优先级队列
- 《算法导论》读书笔记之第12章 二叉查找树
- 《算法导论》读书笔记之第11章 散列表
- 算法导论读书笔记 第4章 分治策略
- 《算法导论》读书笔记之第13章 红黑树
- 《算法导论》读书笔记之第12章 二叉查找树
- [算法导论学习心得]关于Insertion Sort
- 算法导论-插入排序 insertion sort
- 读书笔记:《算法导论》,第1章:算法在计算中的作用
- Win7和ubuntu双系统ubuntu上不去网问题解决
- JSONKit用法
- jqGrid单元格编辑配置,事件及方法
- SFMT
- 随身笔记
- 读书笔记:算法导论第2章 第1节 Insertion sort
- Unity中脚本的生命周期
- [Linux]Fedora 20 虚拟机VirtualBox安装笔记
- 成本
- unity3D参数赋值
- Android中程序与Service交互的方式——交互方式
- 如何让你的Sublime和Codeblocks支持C++11 (转载)
- 研华/Broadwin的WebAccess RPC漏洞
- bonjour 概述