Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Analysis of Algorithm

Rating
-
Sold
-
Pages
6
Uploaded on
25-10-2022
Written in
2022/2023

Excellent Explanation about algorithm in computer science How to Analyze Algorithm Algorithm to find mean of all elements of array Algorithm to sum up two matrices

Institution
Course

Content preview

How to Analyze an Algorithm

Algorithm to swap the contents of 2 variables

Algorithm Exchange(a, b)
Begin
Tmp  a ; --------------------------- 1 (unit amount of time)
a  b; ------------------------------ 1
b  a; ------------------------------ 1
End

Time function (or time complexity of algorithm): F(n)=3 , is constant value
F(n)= Θ(1)
Note: (Θ(1) is used to represent any constant/fixed value like 100, 200)
We are assuming that every single statement is taking unit amount of time even if it is complex
statement. We will not consider clock time.
X=4 *a + 9*b
Since we do our analysis at shallow(basic) level, therefore, we assume that the above statement
is taking unit amount or constant amount of time; although if we carefully look at this statement,
we will see that there are four operations in total. However, we ignore the detail analysis. One
can do detailed analysis up to the level of machine code.
Algorithm to sum up all the elements of Array
Algorithm sum(A, n)
Begin
s=0; --------------------------- 1
n times n+1 n times
For ( i = 0; i< n; i++) ; --------------------------- n+1 times(bother about loop condition)
Begin
S= s + A[i]; --------------------------- n times
end
Return s; --------------------------- 1 time
End

, Time function (or time complexity of algorithm): F(n)= 2n+3
F(n)= Θ(n)
Algorithm to find mean of all the elements of the Array
Algorithm sum(A, n)
Begin
s=0; --------------------------- 1
n times n+1 n times
For ( i = 0; i< n; i++) ; --------------------------- n+1 times(bother about loop condition)
Begin
S= s + A[i]; --------------------------- n times
End
mean= s/n --------------------------- 1 time
Return s; --------------------------- 1 time
End

Time function (or time complexity of algorithm): F(n)= 2n+4
F(n)= Θ(n) (Consider high order
terms only and skip the rest of the constants and variables)


Algorithm to sum up the two matrices.
Algorithm sum(A, B, n) // A and B are square matrices of dimension nxn
Begin
For ( I= 0; I< n; I++) ; --------------------------- n+1
{
For ( J = 0; J< n; J++) ; -------------------------n x (n+1) =n2 +n
{
C[i, J] = A [I, J] + B[I, J]; -------------------------n x n
}
}


Time function (or time complexity of algorithm): F(n)= 2n2 + 2n +1
F(n)= Θ(n2)

Written for

Institution
Course

Document information

Uploaded on
October 25, 2022
Number of pages
6
Written in
2022/2023
Type
Class notes
Professor(s)
Atif
Contains
All classes

Subjects

$11.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
03059387882

Get to know the seller

Seller avatar
03059387882 Exam Questions
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
3 year
Number of followers
0
Documents
5
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions