Analysis of Algorithm
Analysis of Algorithm
Analysis of Algorithm
BSCS 5th Software B Evening
Time Complexity:
How much time an algorithm takes
It is categorized in three terms
o Best time/case
o Average time/case
o Worst time/case
Best Time
Worst Time
Average Time
Non-Deterministic Problems:
Occur in O (N!) or O (2n)
For the solution of non deterministic problems we compare it with deterministic
problems and guess the solution
Searching:
To find out some element and its position
o Linear Search
o Binary Search
o Binary Search Tree
o Hashing
There are two types of searching
o Successful
o Unsuccessful
Successful means if searching element is found
Unsuccessful means if searching element is not found
Linear Search:
Also known as Sequential Search
The search in which we scan all elements starting from first to last
Advantage:
o Linear search is for simplicity
o We don’t have to arrange data in ascending or descending order for linear or
sequential search
Disadvantage:
o But for large amount of data this search is not so good because it scan all elements
Best time for linear search is
o O (1)
Worst time for linear search is
o O(N)
Average time for linear search is
o Best Time + Worst Time
2
= O (1) + O (N)
2
= (N+1)
2
= N
2
Binary Tree:
The tree in which each parent node has 2 or less than 2 child nodes
‘
This technique is used for sorting as well as searching
For searching we use three concepts
o Pre-Order
(Node, Left, Right)
o Post-Order
(Left, Right, Node)
o In-Order
(Left, Node, Right)
Advantage:
o No need to arrange the data in ascending or descending order
Disadvantage:
o We have to construct the tree
Root Node:
The node from where the tree structure’s starts
Hashing:
Technique used for searching as well as sorting data
Store elements through hash functions
Hash Function:
A formula through which data store as well as retrieve from memory
We have to apply some hash functions on data to store
There are two columns to store elements in memory using hash function
o First column to store actual element
o Second column to store the location of next element
For last element the second column have empty value
We apply hash function with our own choice
o Like take mod with 10 of every element that tell that element’s location
o 55 % 10 = 5 it means 5th location
o 67 % 10 = 0 it means 7th location
For example we have some elements as:
55 97 32 11 50 69 72 83 54
o Now we use hash function like take mod with 10 of every element and store in the
calculated location
Example:
How to search element:
Now in above example for example we want to search 78
o We use Hash function here
o Take mod with 10 – 78 % 10 = 8
o Now search at 8th location
Advantage:
o We came to know in first comparison that element exists or not
Best, worst and average case of hashing is O (1) if no collision
Hash Collision:
When two or more than two values or elements try to save at same location then hash
collision occurs
The area in which we place those elements whose position is already filled
This is temporary area
Hash Resolving:
Remove the collision mean placed back the element from over flow area to main memory
Place the first element at the first available location in main memory
More hash collision more searching time
Over flow area must be equal to N-1, here N is equal to number of elements to save in
memory
Example:
Note:
Maximum hash collision occurs when if we set no of digits is the location in memory like
in above example almost all have two no of digits
o So every digit try to save at 2nd location
More hash collisions more iterations
We should apply hash function according to the nature of data
Disadvantages:
o May be data is like that you experienced more collisions
o Never 100% optimization
o Always chances of more improvement
o Algorithm will be data dependent
Sorting:
Arrangement of data either in ascending or descending order
Sorting techniques are followings:
o Bubble sort
o Selection sort
o Insertion sort
o Quick sort
Bubble Sort:
Advantage:
o Very easiest approach
o Good for small data
Disadvantage:
o Worst with respect to time
o Worst for large data
o If data is already sorted then it will also take all comparison as the no of elements
exists