Vous êtes sur la page 1sur 23

Mazhar Ul Islam

Analysis of Algorithm
BSCS 5th Software B Evening

Federal Urdu University


OF
Arts Science & Technology
Analysis of Algorithms:
The subject in which we study how much time & size of algorithm will be required during
execution
Analysis in case of tow terms:
o Size
o Time
For size, we don’t concern with it because today the size is not a big issue
The main focus is to overcome the time
Time and size vary environment to environment

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

The minimum time of an algorithm


Unit time for best case is O(1)
For example:
o If we want to search an element and that is element is found in first comparison
o If we want to sort a data and that data is already sorted

Worst Time

The maximum time of an algorithm


For example:
o If we want to search any element and that is element is found in last comparison
o Or that element does not exist in the list of data

Average Time

Sum of best time & worst time divided by two


Best Time + Worst Time
2
 Note:
We cannot tell exact time for any algorithm, we tell generally in terms of N
Minimum time of any algorithm is O(1)
O(N):
o It means the ration of increase of elements and ration of increase time is same
O(logN):
o Very slow growth in time
O(NlogN):
o Because of multiplication growth is little bit greater then O(LogN)
O (N2), O (N3), O (2n), O (N!):
o Very much growth in 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

Algorithm for Linear Search:


Binary Search:
It is applicable when data is in particular order like ascending or descending
Data list is divided into two half lists in each step
Advantage:
o It is suitable for large amount of data
Disadvantage:
o We have to arrange data in some specific order
Example 1:
Example 2:
Algorithm for Binary Search:
Tree:
The data structure in which elements are arranged in parent/child or hierarchical form
All elements of tree are dependent to each other

Binary Tree:

The tree in which each parent node has 2 or less than 2 child nodes

Binary Search Tree:


It has three steps
o Make first element as root node
o All the elements which are less than root node are placed on left side
o All the elements which are greater than or equal to root node are placed on right side
Example:


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

Over Flow Area:

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

Time complexity for this technique


o Best case for bubble sort is O (N2)
o Worst case for bubble sort is O (N2)
o Average case for bubble sort is O (N2)
Example:

One element takes N-1 comparison to be placed in its proper position


Algorithm for Bubble Sort:
 Divide & Conquer Approach:
Used to minimize the time complexity
Steps are followed as:
o Divide the problems into sub-problems or modules
o Solve each module independently
o Finally combine all the solution of sub-problems to get solution of whole problem
Modeling is on basis of logic
More modules may also problematic for us because if we divide into many modules then
it will mix up and we feel very difficulty
Selection Sort:
Best technique as compared to Bubble sort because in bubble in every iteration of inner
loop there is swapping but in selection the swapping will in outer loop
Outer loop starts from first to N-1
Inner loop starts from second to N
Time complexity is:
o For best case --- O (N2)
o For worst case --- O (N2)
o For average case --- O (N2)
Time will always less as compare to bubble sort although has same O (N2)
Example:
Algorithm for Selection Sort:
Insertion Sort:
Best technique as compared to bubble sort as well as selection sort
It starts comparison from 2nd element and compare it with previous element from it
If less then replace in case of descending order
Time complexity for it:
o Best case --- O (N)
o Worst case --- O (N2)
o Average case --- O (N2/2)
Although time complexity is same as bubble and selection but it will has always very less
comparison as compared to bubble and selection techniques
Example:
Algorithm for Insertion Sort:

Vous aimerez peut-être aussi