procedure max (a[1..n]: integers)
max := a[1]
for i := 2 to n
if max < a[i] then max := a[i]
Run Code Online (Sandbox Code Playgroud)
是复杂性O(1)还是O(n)最佳情况?序列包含n元素.它是伪代码.
$haystack = array('T', 'h', 'i', 's', 'i', 's', 's', 'r', 'i', 'k', 'a', 'n', 't', 'h');
$needle = array('s', 'r', 'i', 'k', 'a', 'n', 't', 'h');
$array = array();
$k = -1;
$m = count($needle);
$n = count($haystack);
//****************1st type********************
for ($i = 0; $i < $m; $i++) {
for ($j = 0; $j < $n; $j++) {
if ($needle[$i] == $haystack[$j]) {
$array[++$k] = $needle[$i];
//echo $needle[$i]."<br/>";
break;
}
}
}
//********************2nd type**************************
$found_array = array();
$j = 0; …Run Code Online (Sandbox Code Playgroud) 我正在修改考试,我在互联网上发现了这个问题,并想知道我将如何解决它.
(使用基数2日志)
证明log(2 n)是O(log n)的成员.
我已经试了一下,但我不确定我是否正确,因为没有提供答案.能否请你帮忙?
这是我的尝试:
日志2 Ñ - ç日志Ñ ≤0
日志2 +登录Ñ - Ç登录Ñ ≤0
1 +(1- C ^)日志ñ ≤0
(然后我通过对数除以Ñ.)
示例:n = 8且c = 10的计算结果小于零.因此确实如此.
我的问题是:
我这样做了吗?
我的答案可以进一步简化吗?
请有人能告诉我如何2n = O(3n)计算?
以下是其他一些例子:
2^4 = O(1)
10n = O(n)
n log2(n) = O(n log n)
# given an array, it solves sum of elements in it,
# args: array, initial and final indices
# Returns: sum of elements
def ArrayTot (arr, low, high):
return sum( arr[low : high+1] )
# Is this linear time?
# args: array, initial and final indices
# returns: Max possible value in sub-array.
def MaxSubArray (arr, low, high):
# max value of sub-array
TotalArray = 0
# starts iterating arr from left - right i.e., arr[low, j]
for j in …Run Code Online (Sandbox Code Playgroud) 假设我有一个if子句
if (!f(x))
{
g(x);
}
Run Code Online (Sandbox Code Playgroud)
f(x)= O(x ^ 3)的复杂度和g(x)= O(x ^ 2)的复杂度.在这种情况下,整体复杂性是多少?O(x ^ 5)?还是O(x ^ 3)?
我想增加问题的大小.
while(z(x))
{
for(p(x))
{
if (!f(x))
{
g(x);
}
}
}
Run Code Online (Sandbox Code Playgroud)
其中,z(x)= O(x ^ 5),p(x)= O(x),f(x)= O(x ^ 3),g(x)= O(x ^ 2)
algorithm complexity-theory time-complexity asymptotic-complexity
关键是二进制计数器在开头有一些内容.它是否仍然按时间复杂度摊销?怎么证明呢?
假设我们有11010二进制计数器,我们将它递增,以便它现在11011等等.
单增量运营的摊余成本是多少?
我需要计算这段代码的复杂性.我知道它是O(n),但我需要公式中的证据.例如,循环具有复杂性1 + 3*n + n*f(body).
代码1:
int i = 0;
int t = 0;
int j = n/2;
while (i<n) {
while (t<n/2) {
t++;
i++;
}
while (j<n) {
j++;
i++;
}
Run Code Online (Sandbox Code Playgroud)
}
UPD:我也需要计算这段代码的复杂性.它与Code 1不同吗?
代码2:
int k = 0;
int i = 0;
int t = 0;
int j = n/2;
while (i<n) {
k=0;
while ((t<n/2) && (k<=10)) {
t++;
i++;
k++;
}
k=0;
while ((j<n) && (k<=10)) {
j++;
i++;
k++;
} …Run Code Online (Sandbox Code Playgroud) algorithm complexity-theory time-complexity asymptotic-complexity
问题:哪个复杂性有我的功能?以及如何找到算法的时间复杂度?
该函数检查给定的int数组是否已排序.
我的代码:
public static boolean isSorted(double d[]){
boolean sortedAscending = true;
boolean sortedDescending = true;
boolean bool = false;
for (int i = 0; i < d.length-1; i++) {
if(d[i] > d[i+1] && sortedAscending){
sortedAscending = false;
if(bool){
break;
}
bool = true;
}
else if(d[i] < d[i+1]&& sortedDescending){
sortedDescending = false;
if(bool){
break;
}
bool = true;
}
}
return sortedAscending || sortedDescending;
}
Run Code Online (Sandbox Code Playgroud) 我已经知道这个问题的答案是,O(N^2)但是我不知道如何。我知道for循环的运行N时间,但是如何运行N^2呢?
public static String rev(String s) {
String r = "";
int N = s.length();
for (int i = 0; i < N; i++) {
r = s.charAt(i) + r;
}
return r;
}
Run Code Online (Sandbox Code Playgroud)