• Tidak ada hasil yang ditemukan

Complexity Analysis

N/A
N/A
Protected

Academic year: 2023

Membagikan "Complexity Analysis"

Copied!
13
0
0

Teks penuh

(1)

Complexity Analysis

(2)

int main() {

int a, b, i,sum=0;

a=10; 1 b=20; 1

sum=a+b; 1 O(1) printf("%d",sum);

}

(3)

• int main() {

int n, b, i,sum=0;

scanf("%d",&n);

sum=n*(n+1)/2 0(1) printf("%d",sum);

}

(4)

int main() {

int n, b, i,sum=0;

scanf("%d",&n);

for(i=0;i<n;i++){ 1,2,3,……n O(n) sum=sum+i;

}

printf("%d",sum);

}

(5)

int main() {

int n, b, i,count=0;

scanf("%d",&n);

for(i=0; i<n; i++) 1+2+3….n= n(n+1)/2=n^2+n/2 =O(n^2) {

for(j=0; j<n; j++) {

count=count+1;

} }

printf("%d",count);

}

(6)

int main() {

for(i=0; i<n; i++) {

for(j=0; j<n; j++) {

for(k=0;k<n;k++) {

count=count+1;

} } }

(7)

int main() {

for(i=0; i<n; i++) {

for(j=0; j<n; j++) n^2 n^2+n=O(n^2) {

count=count+1;

} }

for(i=0; i<n; i++) {

count=count+1; n

}

(8)

a() {

s=1;i=1;

While(s<=i) s=1 3 i=1 2 {

i++;

s=s+i;

Printf(“%d”,s);

} }

(9)

int main()

{

int n, b, i,count=0;

scanf("%d",&n);

for(i=0; i^2<n; i++) n!

{

printf("something") ;

} }

(10)

Big-O notation represents the upper bound of the running time of an algorithm. Thus, it gives the worst case complexity of an algorithm.

(11)

Say

f(n) is your algorithm runtime,

g(n) is an arbitrary time complexity f(n) = C(g(n)),

For some real constants c (c > 0) and n0 >1, f(n) <= c g(n) for every input size n (n > n0).

F(n)=O(g(n))

(12)

• Example

F(n)=3n+2 g(n)=n F(n)<=Cg(n)

3n+2<=Cn C=4 3n+2<=4n

n>=2n

F(n)=O(g(n))

(13)

Big O notation Growth

Referensi

Dokumen terkait

Worst-case Average-case/expected Algorithm running time running time Insertion sort ‚.n2/ ‚.n2/ Merge sort ‚.nlgn/ ‚.nlgn/ Heapsort O.nlgn/ — Quicksort ‚.n2/ ‚.nlgn/ expected