In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about the time complexity and space complexity of algorithms in C language. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. Preface 1.1 what is a data structure?
Data structure (Data Structure) is a way for computers to store and organize data, which refers to a collection of data elements that have one or more specific relationships with each other.
1.2 what is an algorithm?
Algorithm (Algorithm): a well-defined calculation process that takes one or a group of values as input and produces one or a set of values as output. To put it simply, an algorithm is a series of computational steps used to convert input data into output results.
two。 Algorithm efficiency 2.1 how to measure the quality of an algorithm
How to measure the quality of an algorithm? For example, for the following Fibonacci series:
Long long Fib (int N) {if (N)
< 3) return 1; return Fib(N-1) + Fib(N-2);} 斐波那契数列的递归实现方式非常简洁,但简洁一定好吗?那该如何衡量其好与坏呢? 2.2 算法的复杂度 算法在编写成可执行程序后,运行时需要耗费时间资源和空间(内存)资源 。因此衡量一个算法的好坏,一般是从时间和空间两个维度来衡量的,即时间复杂度和空间复杂度。 时间复杂度主要衡量一个算法的运行快慢,而空间复杂度主要衡量一个算法运行所需要的额外空间。在计算机发展的早期,计算机的存储容量很小。所以对空间复杂度很是在乎。但是经过计算机行业的迅速发展,计 算机的存储容量已经达到了很高的程度。所以我们如今已经不需要再特别关注一个算法的空间复杂度 2.3 复杂度在校招中的考察 3.时间复杂度3.1 时间复杂度的概念 时间复杂度的定义:在计算机科学中,算法的时间复杂度是一个函数,它定量描述了该算法的运行时间。一个算法执行所耗费的时间,从理论上说,是不能算出来的,只有你把你的程序放在机器上跑起来,才能知道。但是我们需要每个算法都上机测试吗?是可以都上机测试,但是这很麻烦,所以才有了时间复杂度这个分析方式。 一个算法所花费的时间与其中语句的执行次数成正比例,算法中的基本操作的执行次数,为算法的时间复杂度即:找到某条基本语句与问题规模N之间的数学表达式,就是算出了该算法的时间复杂度 // 请计算一下Func1中++count语句总共执行了多少次?void Func1(int N){ int count = 0; for (int i = 0; i < N ; ++ i) { for (int j = 0; j < N ; ++ j) { ++count; } } for (int k = 0; k < 2 * N ; ++ k) { ++count; } int M = 10; while (M--) { ++count; } printf("%d\n", count);} Func1 执行的基本操作次数 :F(N) = N^2+2*N+10 (‘^’在此章节表示为数乘) N = 10 F(N) = 130 N = 100 F(N) = 10210 N = 1000 F(N) = 1002010 实际中我们计算时间复杂度时,我们其实并不一定要计算精确的执行次数,而只需要大概执行次数,那么这里我们使用大O的渐进表示法。 3.2 大O的渐进表示法 大O符号(Big O notation):是用于描述函数渐进行为的数学符号。 推导大O阶方法: 1、用常数1取代运行时间中的所有加法常数。 2、在修改后的运行次数函数中,只保留最高阶项。 3、如果最高阶项存在且不是1,则去除与这个项目相乘的常数。得到的结果就是大O阶。使用大O的渐进表示法以后,Func1的时间复杂度为:O(N^2) N = 10 F(N) = 100 N = 100 F(N) = 10000 N = 1000 F(N) = 1000000 通过上面我们会发现大O的渐进表示法去掉了那些对结果影响不大的项,简洁明了的表示出了执行次数。 另外有些算法的时间复杂度存在最好、平均和最坏情况: 最坏情况:任意输入规模的最大运行次数(上界) 平均情况:任意输入规模的期望运行次数 最好情况:任意输入规模的最小运行次数(下界) 例如:在一个长度为N数组中搜索一个数据x 最好情况:1次找到 最坏情况:N次找到 平均情况:N/2次找到 在实际中一般情况关注的是算法的最坏运行情况,所以数组中搜索数据时间复杂度为O(N) 3.3 常见时间复杂度计算举例 实例1 // 计算Func2的时间复杂度?void Func2(int N){ int count = 0; for (int k = 0; k < 2 * N ; ++ k) { ++count; } int M = 10; while (M--) { ++count; } printf("%d\n", count);} 实例 2 // 计算Func3的时间复杂度?void Func3(int N, int M){ int count = 0; for (int k = 0; k < M; ++ k) { ++count; } for (int k = 0; k < N ; ++ k) { ++count; } printf("%d\n", count);} 实例 3 // 计算Func4的时间复杂度?void Func4(int N){ int count = 0; for (int k = 0; k < 100; ++ k) { ++count; } printf("%d\n", count);} 实例 4 // 计算strchr的时间复杂度?const char * strchr ( const char * str, int character ); 实例 5 // 计算BubbleSort的时间复杂度?void BubbleSort(int* a, int n){ assert(a); for (size_t end = n; end >0;-- end) {int exchange = 0; for (size_t I = 1; I
< end; ++i) { if (a[i-1] >A [I]) {Swap (& a [I-1], & a [I]); exchange = 1;}} if (exchange = = 0) break;}}
Example 6
/ / calculate the time complexity of BinarySearch? Int BinarySearch (int* a, int n, int x) {assert (a); int begin = 0; int end = n mid 1; while (begin > 1); / / Anti-overflow if (a [mid]
< x) begin = mid+1; else if (a[mid] >X) end = mid-1; else return mid;} return-1;}
Example 7
/ / calculate the time complexity of factorial recursive Fac? Long long Fac (size_t N) {if (0 = = N) return 1; return Fac (Nmur1) * N;}
Example 8
/ / calculate the time complexity of Fibonacci recursive Fib? Long long Fib (size_t N) {if (N)
< 3) return 1; return Fib(N-1) + Fib(N-2);} 实例答案及分析: 实例1基本操作执行了2N+10次,通过推导大O阶方法知道,时间复杂度为 O(N) 实例2基本操作执行了M+N次,有两个未知数M和N,时间复杂度为 O(N+M) 实例3基本操作执行了10次,通过推导大O阶方法,时间复杂度为 O(1) 实例4基本操作执行最好1次,最坏N次,时间复杂度一般看最坏,时间复杂度为 O(N) 实例5基本操作执行最好N次,最坏执行了(N*(N+1)/2)次(N-1 + N-2 + N-3…+2+1),通过推导大O阶方法+时间复杂度一般看最坏,时间复杂度为 O(N^2)。 实例6基本操作执行最好1次(第一次就找到了),最坏O(logN)次(全找了一遍但没找到的情况),时间复杂度为 O(logN) ps:logN在算法分析中表示是底数为2,对数为N。有些地方会写成lgN。(建议通过折纸查找的方式讲解logN是怎么计算出来的)(假设找了x次才找完,则共有2^x个数据。反过来讲就是N个数据最多要找logN(底数为2)次) 实例7通过计算分析发现基本操作递归了N次,时间复杂度为O(N)。 实例8通过计算分析发现基本操作递归了2^N 次,时间复杂度为O(2N)。(1+2+4+8……+2(n-1) 再减一些次数(忽略不计))(建议画图递归栈帧的二叉树) 总结:我们想要分析算法的时间复杂度,一定要去看思想,,不能只去看程序是几层循环。 递归算法时间复杂度的计算: 1.每次函数调用是O(1),那么就看递归次数 2.每次函数调用不是O(1),就把每次的调用次数相加。 4.空间复杂度 空间复杂度也是一个数学表达式,是对一个算法在运行过程中临时占用存储空间大小的量度 。 空间复杂度不是程序占用了多少bytes的空间,因为这个也没太大意义,所以空间复杂度算的是变量的个数。空间复杂度计算规则基本跟实践复杂度类似,也使用大O渐进表示法。 注意:函数运行时所需要的栈空间(存储参数、局部变量、一些寄存器信息等)在编译期间已经确定好了,因此空间复杂度主要通过函数在运行时候显式申请的额外空间来确定。 实例 1 // 计算BubbleSort的空间复杂度?void BubbleSort(int* a, int n){ assert(a); for (size_t end = n; end >0;-- end) {int exchange = 0; for (size_t I = 1; I
< end; ++i) { if (a[i-1] >A [I]) {Swap (& a [I-1], & a [I]); exchange = 1;}} if (exchange = = 0) break;}}
Example 2
/ / calculate the space complexity of Fibonacci? / / returns the first n terms of the Fibonacci series long long* Fibonacci (size_t n) {if (naphth0) return NULL; long long* fibArray = (long long*) malloc ((nx1) * sizeof (long long)); fibArray [0] = 0; fibArray [1] = 1; for (int I = 2; I)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 269
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.