Professional Documents
Culture Documents
Lect 4 - 2024
Lect 4 - 2024
Foundations of
Data Structures & Algorithms
Lect 4
Sameera Muhamed Salam
Dept of CSIS, BITS Pilani, Hyderabad Campus
BITS F232 Sameera M S 2
Introduction
Algorithm
A step by step procedure for solvig a problem in a finite amount of time
set of instructions
to
INPUT convert input OUTPUT
to
expected output
I Calculated in terms of
I Time Complexity
I Time needed to execute a program that implements the algorithm
I Space Complexity
I Computer memory needed to execute a program that implements the algorithm
Experimental method:
I Write a program that implements the
algorithm
I Run the program with data sets of
varying size and composition.
I Use a system call to get an accurate
measure of the actual running time.
Disadvantages:
Experimental method:
I Write a program that implements the
algorithm
I Run the program with data sets of
varying size and composition.
I Use a system call to get an accurate
measure of the actual running time.
Disadvantages:
I It is necessary to implement and test
the algorithm in order to determine
Experimental method: its running time.
I Write a program that implements the I Experiments
algorithm I is done only on a limited set of
I Run the program with data sets of inputs,
varying size and composition. I May not be indicative of the
running time on other inputs not
I Use a system call to get an accurate
included in the experiment.
measure of the actual running time.
I In order to compare two algorithms,
the same hardware and software
environments are needed
Theoretical method:
I To find Time Complexity
I Need to identify key operations performed in the algorithm.
I Examples
I algorithm to find average of 2 numbers
I algorithm to find roots of a quadratic equation
I algorithm to print values present in all nodes of a graph
I algorithm for inorder traversal of a tree
I algorithm to search a number in an array
I algorithm to sort n elements in an array in descending order
I algorithm to insert an element in agiven position of an array
I Count the number of times key operations are performed.
Algorithm LinearSearch(a,n,x):
INPUT: Array a storing n ≥ 1
integers, Example:
x is the number to be
searched in the array a
OUTPUT: Yes if x is present in array a
No otherwise.
I for(i=0 to n)
I if(a[i]==x)
Print Yes;
Break;
I Print No
Algorithm LinearSearch(a,n,x):
INPUT: Array a storing n ≥ 1
integers, Example:
x is the number to be
searched in the array a
OUTPUT: Yes if x is present in array a
No otherwise.
I for(i=0 to n)
I if(a[i]==x)
Print Yes;
Break;
I Print No
Algorithm LinearSearch(a,n,x):
INPUT: Array a storing n ≥ 1
integers, Example:
x is the number to be
searched in the array a
OUTPUT: Yes if x is present in array a
No otherwise.
I for(i=0 to n)
I if(a[i]==x)
Print Yes;
Break;
I Print No
Algorithm LinearSearch(a,n,x):
INPUT: Array a storing n ≥ 1
integers, Example:
x is the number to be
searched in the array a
OUTPUT: Yes if x is present in array a
No otherwise.
I for(i=0 to n)
I if(a[i]==x)
Print Yes;
Break;
I Print No
10N 2
100N
10 N
100N
10 N
100N
10 N
10N 2
100N
10 N
log2 n
16 n
log2 n
16 n
I n0 = 16
BITS F232 Sameera M S 46
Properties of Asymptotic Notations