Chapter 1 Programming:A General Overview
1.1 What is This Book About?
Problem.
K-Selection Problem. Input a array and choose the k-th largest.
User Bubble-sort
#include <iostream>
#include <stdlib.h>
void swap(int array[], int i, int j) {
if(i == j)
return;
int temp = array[j];
array[j] = array[i];
array[i] = temp;
}
void bubblesort(int array[], int nsize) {
for (int i = 0; i < nsize; i++) {
for(int j = 0; j < nsize - i - 1; j++) {
if(array[j] > array[j + 1])
swap(array, j, j + 1);
}
}
}
// Select the k-th largest number from a array.
int kthselection(int array[], int nsize, int k) {
if(k > nsize)
return -1;
bubblesort(array, nsize);
return array[nsize - k + 1];
}
int main(int argc, char ** argv) {
int a[] = {5, 3, 2, 1, 9, 10, 7 ,6};
int k = 0;
std::cout << "Please the k-th largest from the array:\n";
std::cin >> k;
printf("The %d-th largest of the array is %d\n", k,
kthselection(a, _countof(a), k));
return 0;
}
Optimized use BubbleSort for the first k-th
The algorithm related in the textbook is very complicated.(I think that we must use the link-list data structure)
A word puzzle find algorithm.
Find the word from horizontal, vertical, or diagonal directions.
The aim of this books :
To estimate the running time of a program for large inputs.
How to compare the running times of two programs without actually coding them.
To improve the speed of a program for a determing program bottlenecks.
1.2 Mathematics Review
1.2.1 Exponents
1.2.2 Logarithms
1.2.3 Series
A research about the second formula
http://blog.youkuaiyun.com/acdreamers/article/details/38929067
The second formula means f(N) relative to const N。 just summation N times of the function f(N) thus the result is N * f(N)
1.2.4 Modular Arthmetic
the theorem1)could be proved by the basic arthematic theorem(universal factorization theorem the proof step refer the Notebook)
the theorem2)could be proved by the quotient-remainder theorem and the result of theorem1) and the property that if a|b then a<=b. the step refer the Notebook.
the theorem3 ) ?
1.2.5 The P Word (proof method)
1)Mathematica Induction
2)Proof by counterexample
3)Contradiction
(There is an issue in the proof above Because we still can‘t decude that N is a prime. The actual approach to refer the notebook for[ Discrete Mathematics with applicaton] chapter 3. Simple approach: To prove that N is divisible by some prime number called P ,but since p1~pk is all of the prime number ,them p must in p1~pk, but all all prime numbers from p1~pk can't disivile N. so we deducde that N is divisible by p and N is not divisible by p, which is a contradiction.)
1.3 A Brief Introduction to Recursion
A function that is defined in terms of itself is called recursive. (Using recursion for numerical calculations is usually a bad idea)
A C++ implementation for recursion function f(x) = 2(fx - 1) + x^2 (f(0) = 0)
int f(int x) {
if (x == 0)
return 0;
else
return 2 * f(x - 1) + x * x;
}
Is that the recursion just circular logic?
f(5) by computing f(5)would be circular
f(5)by computing f(4) is not circular
Recursive calls will keep on being made until a base case is reached
f(-1)will cause a infinite loop and an undefined behavior(implementation depended)
Exp. A nonterminating recursive function.
int bad(int x) {
if (x == 0)
return 0;
else
return bad(x / 3 + 1) + x - 1;
}
In this case, bad(1) will failed because it will call bad(1) in else-routine of the if statement. and it will cause a infinite loop and run out of space(stack overflow)
It is the same as bad(2) because it will call bad(1)
the same for bad(3), bad(4), bad(5)...
It is only successful when bad(0) is called.
The two fundamental rules of recursion:
1) Base case. You must always have some base cases, which can be solved without recursion. (the return point for the function)
2) Making progress. For the cases that are to be solved recursively, the recursive ca