Data structure (continually updated)

algorithm

  • Calculation = information processing
    by means of a tool, in accordance with certain rules, carried out in the form of clear and machinery
  • The so-called algorithms, that is, under the specific computer model, a sequence of instructions designed to solve specific problems
    • Pending input information (question)
    • Information processed output (answer)
    • Correctness can indeed solve specific problems
    • Any of a deterministic algorithm can be described as a sequence of basic operations consisting of
    • Feasibility of each basic operation can be realized, in constant time to complete the cut
    • Have poor resistance to any input, and poor times through basic operations can be output
序列Hailstone(n)   

Good algorithm

  • correct
  • robust
  • Readable
  • Efficiency (as fast as possible; little storage space as possible)
To measure is to know.
If you can not measure it,
you can not improve it.
- Lord Kelvin

Analysis of Algorithms

The two main aspects:

  1. Correctness;
  2. Cost: Running Time + Space Required

RAM:Random Access Machine

  • Register sequence number, the total number is not restricted (R [0], R [1] ...)
  • Each basic operation only time constants
  • As with the TM model look, RAM is generally computer model simplification and abstraction, so that we can be independent of any specific platform, to make credible comparison and evaluation on the efficiency of the algorithm
  • Running time of the algorithm in these models + infinity algorithms need yo ah basic number of operations performed by T (n) = algorithm to solve the scale of the problem of n, the number of operations required to perform the basic

Big O notation

Mathematics is more in need
of good notations than 
of new theorems.
- Alan Turing
好读书不求甚解
每有会意,便欣然忘食
   -- 陶渊明

Constant (constant function)
the most efficient algorithm of this type of
algorithm analysis

He calculated just as men breathe
as eagles sustain themselves in the air.
- Francois Arago

The two main tasks

  1. Correctness
  2. the complexity

The main methods of complexity analysis

  1. Iteration: Summation
  2. Recursion: recursively tracking + push release
  3. Guess + authentication

series

  • Arithmetic progression: the square of the same order with the last item
  • Power Sum Series: higher than the power of the first order
  • Geometric progression: the last item of the same order
  • Convergent series: constant
  • Harmonic series: 1 + 1/2 + ... + 1 / n = O (log n)
  • Logarithmic progression: log1 + log2 + ... + logn = O (log n)
相关书籍:Concrete Mathematics

Bubble sort

//n 为在某个位置之前排序(不包含),相当于arr数组的长度
public void vuvvlesort(int[] arr,int n){
  for(boolean sorted = false; sorted = !sorted;  n--){
    for(int i = 1; i < n; i++){
      if(arr[i-1]>arr[i]){
        swap(arr,arr[i-1],arr[i]);//交换两个元素位置函数,需要自己实现
      }
    }
  }
}

Back cover estimate (Back-Of-The-Envelope Calculation)

某位物理学家当时在测量原子弹爆炸威力时,在安全范围内将一张纸撕成很多个碎片,在爆炸时将手里的纸扔下,通过计算纸张落下的位置来 计算原子弹爆炸的威力,与当时计算机计算出来的数值仅仅差距为2倍,换算成O的算法,同阶!!! 

Thinking small: If you measure the circumference of the earth, how it?

Iteration is the artificial, recursive party supernatural powers (To iterate is human, to recurse , divine.)
Where a rule the public such as the rule widowed, the score is also (The control of a large force is the same principle as the control of a few men: it is merely a question of dividing up their numbers )

Jane and conquer

Divide and rule

  • Half recursive
    array summation: half recursive
sum(int[] arr,int lo,int hi){
  if(lo == hi)  return arr[lo];
  int mi = (lo + hi) >> 1;
  return sum(arr, lo, mi) + sum(arr, mi + 1, hi);
}
    public int[] max2(int[] arr, int lo, int hi){
        int[] result=new int[2];
        if (lo + 2 == hi) {
             return arr[lo] >= arr[lo+1] ? new int[]{ arr[lo], arr[lo + 1] } : new int[]{arr[lo + 1], arr[lo]};
        }
        if (lo + 3 == hi) {
            if (arr[lo] >= arr[lo + 1]) {
                return arr[lo + 1] > arr[lo + 2] ? new int[]{arr[lo], arr[lo + 1]} 
                                            : arr[lo] >= arr[lo + 2]  ? new int[]{arr[lo], arr[lo + 2]} : new int[]{arr[lo + 2], arr[lo]};
            } else {
                return arr[lo] > arr[lo + 2] ? new int[]{arr[lo+1], arr[lo]}
                                            : arr[lo + 1] >= arr[lo + 2] ? new int[]{arr[lo + 1], arr[lo + 2]}: new int[]{arr[lo + 2], arr[lo + 1]};
            }
        }
        int mi = (lo + hi) >> 1;
        int[] los = max2(arr, lo, mi);
        int[] his = max2(arr, mi, hi);
        if (los[0] >= his[0]) {
            return los[1] >= his[0] ? new int[]{los[0], los[1]} : new int[]{los[0], his[0]};
        } else {
            return his[1] >= los[0] ? new int[]{his[0], his[1]} : new int[]{his[0], los[0]};
        }
    }
    private void swap(int[] arr, int a, int b) {
        arr[a] = arr[a] ^ arr[b];
        arr[b] = arr[a] ^ arr[b];
        arr[a] = arr[a] ^ arr[b];
    }

Dynamic Programming

Make it work,
make it right,
make it fast.
- Kent Beck

Thinking calculation: Fibonacci number

public int  f(int n){
  if(n == 0){
  	return 0;
  }else if(n == 1){
  	return 1;
  }
  return f(n - 1) + f(n - 2);
}

The time complexity of algorithm is particularly high, probably about 50 required number will see a noticeable delay, and after each will show a plurality grow exponentially, the time complexity of the algorithm is O (n ^ 2)
Improvement:

public int f(int n){
  int f=0;
  int g=0;
  while(0 < n--){
    g += f;
    f = g - f;
  }
  return g;
}

Sequence (Subsequence):
to do the most common subsequence of two strings

等待更新...
递归版本(O(2^n)
动态规划版本(O(m*n)

vector

& Abstract data type data structure

  • Abstract data types: data model + model defined on the set of operations
  • Data Structure: based on a specific language, to achieve a set of algorithms ADT

linear array

  • Static space management -> dynamic space management
    cicada's philosophy:
    the body after every period of growth, as well as housing can not accommodate
    that is shedding the original shell, instead ...
  • Double the capacity strategy
    Increment

Analytical method:

  • Average analysis
  • Share analysis

Sensitive input (input-sensitive): best and worst complexity disparities
54

Released seven original articles · won praise 0 · Views 105

Guess you like

Origin blog.csdn.net/weixin_44188300/article/details/103744150