Professional Documents
Culture Documents
SECTION : K18UW
REG NO : 11802507
TOPICS
Analysis of algorithm
RECURSION
Data structures
SEARCHING
SORTING
Data structures:
Algorithms:
Store and retrieve data in most efficient way.
Asymptotic Notations
Asymptotic notations are mathematical tools to represent the time complexity
of algorithms for asymptotic analysis.
There are mainly three asymptotic notations:
Theta notation
Omega notation
Big-O notation
Theta Notation (Θ-notation)
Theta notation encloses the function from above and below. Since it
represents the upper and the lower bound of the running time of an
algorithm, it is used for analyzing the average case complexity of an
algorithm.
Θ(g(n)) = { f(n): there exist positive constants c1, c2 and n0
such that 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0 }
Big-O Notation (O-notation)
Big-O notation represents the upper bound of the running time of an
algorithm. Thus, it gives the worst case complexity of an algorithm.
O(g(n)) = { f(n): there exist positive constants c and n0
such that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0 }
Omega Notation (Ω-notation)
Omega notation represents the lower bound of the running time of an
algorithm. Thus, it provides best case complexity of an algorithm.
Ω(g(n)) = { f(n): there exist positive constants c and n0
such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0 }
What is Recursion?
The process in which a function calls itself directly or indirectly is called
recursion and the corresponding function is called as recursive function. Using
recursive algorithm, certain problems can be solved quite easily.
Examples of such problems are Towers of Hanoi (TOH),
Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc.
i
nt fact(int n)
{
if (n < = 1) // base case
return 1;
else
return n*fact(n-1);
}
Data structures
• Data structures has two types:
Linear
Non Linear
linear data structures
•Data structure where data elements are arranged sequentially
or linearly where the elements are attached to its previous and
next adjacent in what is called a linear data structure.
• Array
• Linked list
•Stack
•Queue
Array: An Array is a collection of data items having similar data
types.
Linked list
A Linked list is a collection of nodes, where each node has a data
element and a reference to the next node in the sequence.
• Root − The node at the top of the tree is called root. There is only one root per tree
and one path from the root node to any node.
• Parent − Any node except the root node has one edge upward to a node called parent.
• Child − The node below a given node connected by its edge downward is called its
child node.
• Leaf − The node which does not have any child node is called the leaf node.
V = {a, b, c, d, e}
linear search
Linear search is a very simple search algorithm. In this type of search, a
sequential search is made over all items one by one. Every item is
checked and if a match is found then that particular item is returned,
otherwise the search continues till the end of the data collection.
Binary Search
Binary Search is a searching algorithm for searching an element in a
sorted list or array. Binary Search is efficient than Linear Search algorithm
and performs the search operation in logarithmic time complexity for
sorted arrays or lists.
compare key value to mid of the element
mid=(l+r)/2
low=mid+1
high=mid-1
SORTING
Sorting refers to arranging data in a particular format.
real-life scenarios
• Second Pass:
• ( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
• ( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
• ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
• ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
• Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without
any swap to know it is sorted.
• Third Pass:
• ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Insertion sort
Insertion sort is a simple sorting algorithm that works similar to
the way you sort playing cards in your hands. The array is
virtually split into a sorted and an unsorted part. Values from the
unsorted part are picked and placed at the correct position in the
sorted part.
Selection Sort
The selection sort algorithm sorts an array by repeatedly finding the
minimum element (considering ascending order) from unsorted part
and putting it at the beginning.
The algorithm maintains two subarrays in a given array.
THANK YOU