Westward data structure algorithms 02-

1. prologue

       Describe a specific algorithm to solve the problem solution step, expressed as a finite sequence of instructions in a computer, and each represents one or more instruction operations. That is, the process data structure and algorithm analysis.

F7CB5EC5-C260-4d9a-87D2-6D8CB29171B2

     Now I ask you to write a 1 + 2 + 3 + ...... + 100 program evaluates the results, most people write:

05408FFE-0491-4d62-8238-B03F8E85B599

       This corresponds to find another arithmetic sequence algorithm arithmetic sequence from the second means, the difference between each one of its front equals the same constant one of several columns, commonly used A, P represents. This constant is called the arithmetic sequence of tolerances, common letter d represents; for example: 1,3,5,7,9 ...... 2n-1. Term Formula as: an = a1 + (n- 1) * d. First item a1 = 1, tolerance of d = 2.


2. The algorithm defines

       What is the algorithm it? Algorithm method is described to solve the problem. Algorithm (Algo pay m) the word first appeared in the Andean sub-mathematician Al-dense thorns in AD 825 years (the equivalent of our Chinese Tang Dynasty) written by "Indian digital arithmetic" in. Now generally accepted definition of clean is counted:

        Describe a solution-specific algorithm Q Og solution step, expressed as a finite sequence of instructions in a computer, and each represents one or more instruction operations .

2.1 algorithm properties

Algorithm has five basic features: input, output, finite, certainty and feasibility.

  • Input and output characteristics easier to understand, the algorithm has zero or more inputs, at least one or more of the algorithm output , the output algorithms are necessarily
  • Finite resistance : means algorithm after the step of performing a limited, but ended automatically without an infinite loop , and each step is completed within an acceptable time, can not be infinite loop
  • Uncertainty : each step of the algorithm have the meanings defined, will not be ambiguous .
  • 可行性:算法的每一步都必须是可行的, 也就是说,每一步都能够通过执行有限次数完成

 

2.2.算法设计的要求

     算法不是唯一的。也就是说,同一个问题,可以有多种解决问 题的算法,但相对好的算法还是存在的,所以有几大要求要满足 正确性,可读性,健壮性和时间效率高、存储量低。

  • 正确性 算法的正确性是指算法至少应该具有输入、输出和加工处理无歧义性、 能正确反映问题的需求、能够得到问题的正确答案。 但是算法"正确"通常在用法上有很大的差别,大体分为以下四个层次。
    1)算法程序没有语法错误。
    2)算法程序对于合法的输入数据能够产生满足要求的输出结果。
    3)算法程序对于非法的输入数据能够得出满足规格说明的结果。
    4)算法程序对于精心选择的,甚至刁难的测试数据都有满足要求的输出结果。

  • 可读性  算法设计的另一目的是为了便于阅读、 理解和交流。 可读性高有助于人们理解算法,晦涩难愤的算法往往隐含错误,不易被发现, 并 且难于调试和修改。我们写代码的目的,一方面是为了让计算机执行,但还有一个重要的目的是为了 便于他人阅读, 让人理解和交流
  • 健壮性 当输入数据不合法时,算法也能做出相关处理, 不产生异常或莫名其妙的结果。
  • 时间效率高和存储量低:时间效率指的是算法的执行时间 , 对于同一个问题,如果有多个算法能够解决, 执行时间短的算法效率高,执行时间长的效率低。 存储量需求指的是算法在执行过程 中需要的最大存储空间, 主要指算法程序运行时所占用的内存或外部硬盘存储空间。


2.3.算法效率的度量方法

  • 事后统计方法:这种方法主要是通过设计好的测试程序和数据,利用计算机计时 器对不同算法编制的程序的运行时间进行比较,从而确定算法效率的高低。
  • 事前分析估算方法:在计算机程序编制前,依据统计方法对算法进行估算。

       1)算法采用的策略、 方法。
        2)编译产生的代码质量。
        3)问题的输入规模。
        4)机器执行指令的速度。

       我们在分析一个算法的运行时间时,重要的是把基本操作的敬量与输入规模关联起来, f!P基本操作的数量必须表示成输入规模的函数:是 f (n) =n的次方(执行次数)

E2AB84A1-8E32-4d0a-BBB2-BD80160235BA

       

3.算法评估

3.1.时间复杂度计算 T ( n ) = O(f(n))

      在进行算法分析时,语句总的执行次撞 T ( n )是关于问题规模 n 的函数,进而分析 T ( n )随 n 的变化情况并确定T(n)的数量,因此算法的时间复杂度=算法的时间量度,记作: T ( n ) = O(f(n))。 它表示随问题规模 n 的增大,算法执行时间的增长率和 f(n)的增长率相同,都作算法的渐近时间复杂度,简称为时间复杂度。 其中 f ( n) 是问题规模 n 的某个函数。这样用大写 O()来体现算法时间复杂度的记法,我们称之为大 0 记法。 一般情况下,随着 n 的增大, T(n)增长最慢的算法为最优算法。大写 O()来体现算法时间复杂度的记法,我们称之为大 0 记法。 一般情况下,随着 n 的增大, T(n)增长最慢的算法为最优算法

        我们现在来判断一下,两个算法 A 和 B 哪个更好。 假设两个算法的输入规模都是 n,算法 A 要做 2n + 3 次操作,你可以理解为先有一个 n 次的循环,执行完成后,再有一于n 次循环,最后三次赋值或运算,共 2n + 3 次操作。 算法 B 要做 3n + 1 次 操作。 你觉得它们谁更快呢?

8154EC8B-0BAB-4e49-899D-8F471AD80C7D

函数的渐近增长:给定两个画鼓 f (川和 g( n ), 如果存在-个整鼓 N, 使得对于所有的 n > N, f ( n )总是比 g( n )大,那么, 我们说 f ( n ,) 的增长渐近快子 g ( n )。某个算法,随着 n 的增大,宫会越来越优于另一算法,或 者越来越羞于另一算法。

常见时间复杂度表(复杂度按升序排列)

0(1) < O(logn) < O(n) < O(nlogn) < 0(n2 ) < 0(n3 ) < 0(2") < O(n!) < O(n")

61CD9C01-5F50-4cb6-8E6E-62359B4290DD

3.2.算法空间复杂度 S(o)= O(f(o))

      算法的空间复杂度通过计算算法所需的存储空间实现,算法空间复杂度的计算公 式记作: S(o)= O(f(o)),其中, 0 为问题的规模, f(n)为语句关于 n 所占存储空间的函数。


3.3.最坏情况与平均情况

       最坏情况运行时间是一种保证,那就是运行时间将不会再坏了。 在应用中,这是一种最重要的需求, 通常,除非特别指定, 我们提到的运行时间都是最坏情况的运行时间。平均运行时闯是所有情况中最有意义的,因为它是期望的运行时间

       平均运行时间也就是从概率的角度看 , 这个数字在每一个位置的可能性是相同 的,所以平均的查找时间为 n/2 次后发现这个目标元素。




4.总结

     算法的定义:算法是解决特定问题求解步骤的描述,在计算机中为指令的有限序 列,并且每条指令表示一个或多个操作。

  • 算法的特性: 有穷性、确定性、可行性、输入、输出。
  • 算法的设计的要求: 正确性、可读性、健壮性、 高效率和低存储量需求。
  • 算法特性与算法设计容易混,需要对比记忆。
  • 算法的度量方法: 事后统计方法(不科学、不准确)、 事前分析估算方法。

     对算法的分析:

  • 一种方法是计算所有情况的平均值,这种时间复杂度的计算方法 称为平均时间复杂度。
  • 一种方法是计算最坏情况下的时间复杂度,这种方法称为最 坏时间复杂度。 一般在没有特殊说明的情况下,都是指最坏时间复杂度。

      Derivation large 0th order :
• replacing all the runtime constant adder by a constant 1.
• The number of runs a function in the revised, retaining only the highest order term.
• If the highest order term exists and is not 1, then remove to this item multiplied by a constant.
The result is the big 0-order. After the adoption of this step, we can get the number of runs expression algorithm will soon be time complexity Palace, that big 0th order. I also remind everyone, in fact, derive a large 0-order is easy, but how to get the number of runs is an expression of the need for mathematical skills.













Guess you like

Origin www.cnblogs.com/rango0550/p/12059504.html