Session 7 (Algorithms)
Session 7 (Algorithms)
SESSION 7 ALGORITHMS
CONTENTS
1 W H AT I S Algorithm 1
1 W H A T I S Algorithm
1.1 Algorithm analysis
WHAT IS ALGORITHM 1
1 W H A T I S Algorithm
1.1 Algorithm analysis
➢ Time Complexity: is a concept in computer science that deals with the quantification of the amount of time taken by a set of
code or algorithm to process or run as a function of the amount of input. In other words, the time complexity is how long a
program takes to process a given input. The efficiency of an algorithm depends on two parameters:
▪ Time Complexity.
▪ Space Complexity.
➢ Time Complexity: It is defined as the number of times a particular instruction set is executed rather than the total time is
taken. It is because the total time took also depends on some external factors like the compiler used, processor’s speed, etc.
➢ Space Complexity: It is the total memory space required by the program for its execution.
1 W H A T I S Algorithm
1.1 Algorithm analysis
➢ There are three asymptotic notations that are used to represent the time complexity of an algorithm. They are:
1. Θ Notation (theta).
2. Big O Notation.
3. Ω Notation.
➢ An algorithm can have different time for different inputs. It may take 1 second for some input and 10 seconds for some
other input. So, this can be divided into three cases:
2. Average case: We calculate the running time for all possible inputs, sum all the calculated values and divide the sum
by the total number of inputs.
3. Worst case: This is the upper bound on running time of an algorithm. We must know the case that causes the
maximum number of operations to be executed.
WHAT IS ALGORITHM 1
1 W H A T I S Algorithm
1.1 Algorithm analysis
➢ Θ Notation (theta):
▪ The Θ Notation is used to find the average bound of an algorithm i.e. it defines an upper
bound and a lower bound, and your algorithm will lie in between these levels.
➢ Big O Notation:
▪ The Big O notation defines the upper bound of any algorithm i.e. you algorithm can't take more time than this
time. In other words, we can say that the big O notation denotes the maximum time taken by an algorithm or the
worst-case time complexity of an algorithm. So, big O notation is the most used notation for the time complexity
of an algorithm.
➢ Ω Notation:
▪ The Ω notation denotes the lower bound of an algorithm i.e. the time taken by the algorithm can't be lower than
this. In other words, this is the fastest time in which the algorithm will return a result
▪ We can express algorithmic complexity using the big-O notation. For a problem of input size N:
✓ A constant-time function/method is “order 1” : O(1)
✓ A linear-time function/method is “order N” : O(N)
✓ A quadratic-time function/method is “order N squared” : O(N 2 )
WHAT IS ALGORITHM 1
1 W H A T I S Algorithm
1.1 Algorithm analysis
▪ We can express algorithmic complexity using the big-O notation. For a problem of input size N:
WHAT IS ALGORITHM 1
1 W H A T I S Algorithm
1.1 Algorithm analysis
CODE OUTPUT
DESCRIPTION
1 W H A T I S Algorithm
1.1 Algorithm analysis
CODE OUTPUT
DESCRIPTION
1 W H A T I S Algorithm
1.1 Algorithm analysis
CODE
DESCRIPTION
WHAT IS ALGORITHM 1
1 W H A T I S Algorithm
1.1 Algorithm analysis
CODE DESCRIPTION
WHAT IS ALGORITHM 1
1 W H A T I S Algorithm
1.1 Algorithm analysis
CODE DESCRIPTION
1 W H A T I S Algorithm
1.1 Algorithm analysis
CODE DESCRIPTION
1 W H A T I S Algorithm
1.1 Algorithm analysis
DESCRIPTION
CODE
1 W H A T I S Algorithm
1.2 Types of Algorithm
➢ Types of Algorithm
▪ Searching .
▪ Sorting.
➢ Searching:
❑ Linear Search
❑ Binary Search
➢ Sorting:
❑ Selection Sort
❑ Bubble Sort
❑ Insertion Sort
WHAT IS ALGORITHM 1
1 W H A T I S Algorithm
1.3 What is Searching Algorithm
➢ Searching:
❑ Linear Search:
• Problem: Given an array arr[] of n elements, write a function to search a given element x in arr[].
1 W H A T I S Algorithm
1.4 Linear Search
➢ Searching:
❑ Linear Search:
WHAT IS ALGORITHM 1
1 W H A T I S Algorithm
1.4 Linear Search
CODE OUTPUT
DESCRIPTION
1 W H A T I S Algorithm
1.5 Binary search
➢ Searching:
❑ Binary Search:
▪ Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering
the whole array. If the value of the search key is less than the item in the middle of the interval, narrow
the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is
found or the interval is empty
WHAT IS ALGORITHM 1
1 W H A T I S Algorithm
1.5 Binary search
➢ Binary Searching:
▪ We basically ignore half of the elements just after one comparison.
▪ Compare x with the middle element.
▪ If x matches with middle element, we return the mid index.
▪ Else If x is greater than the mid element, then x can only lie in right half subarray after the mid element. So we recur for
right half.
▪ Else (x is smaller) recur for the left half.
WHAT IS ALGORITHM 1
1 W H A T I S Algorithm
1.5 Binary search
OUTPUT
DESCRIPTION
1 W H A T I S Algorithm
1.5 what is sorting Algorithm
1 W H A T I S Algorithm
1.6 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.
▪ In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted
subarray is picked and moved to the sorted subarray.
WHAT IS ALGORITHM 1
1 W H A T I S Algorithm
1.6 Selection Sort
1 W H A T I S Algorithm
1.6 Selection Sort
1 W H A T I S Algorithm
1.6 Selection Sort
CODE
WHAT IS ALGORITHM 1
1 W H A T I S Algorithm
1.6 Selection Sort
CODE OUTPUT
DESCRIPTION
1 W H A T I S Algorithm
1.7 Bubble sort
▪ Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are
in wrong order.
WHAT IS ALGORITHM 1
1 W H A T I S Algorithm
1.7 Bubble sort
WHAT IS ALGORITHM 1
1 W H A T I S Algorithm
1.7 Bubble sort
1 W H A T I S Algorithm
1.7 Bubble Sort
CODE
WHAT IS ALGORITHM 1
1 W H A T I S Algorithm
1.7 Bubble Sort
CODE OUTPUT
DESCRIPTION