0% found this document useful (0 votes)
43 views

Session 7 (Algorithms)

The document discusses algorithms and their analysis. It defines algorithms, time complexity, space complexity, and asymptotic analysis. It then discusses different types of algorithms like searching (linear search, binary search) and sorting (selection sort, bubble sort, insertion sort). Linear search is described as comparing an element to search for to each element in the array one by one. Binary search and other algorithm topics are also covered but in less detail.

Uploaded by

Luka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Session 7 (Algorithms)

The document discusses algorithms and their analysis. It defines algorithms, time complexity, space complexity, and asymptotic analysis. It then discusses different types of algorithms like searching (linear search, binary search) and sorting (selection sort, bubble sort, insertion sort). Linear search is described as comparing an element to search for to each element in the array one by one. Binary search and other algorithm topics are also covered but in less detail.

Uploaded by

Luka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

AMIT LEARNING

SESSION 7 ALGORITHMS
CONTENTS

1 W H AT I S Algorithm 1

1.1 Algorithm analysis 1

1.2 Types of Algorithm 1

1.3 What is Searching Algorithm 1

1.4 linear search 1

1.5 Binary search 1

1.6 what is sorting Algorithm

1.7 Selection Sort 1

1.8 Bubble sort


WHAT IS 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.

➢ Descibe time complexity and space complexity by Asymptotic analysis


➢ Asymptotic analysis : are the mathematical notations that calculate, how the time (or space) taken by an algorithm increases
with the input size. evaluate the performance of an algorithm in terms of input size (we don’t measure the actual running
time).
WHAT IS ALGORITHM 1

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:

1. Best case: This is the lower bound on running time of an algorithm.

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

▪ So, time complexity is constant: O(1) i.e. every time constant


amount of time require to execute code, no matter which
operating system or which machine configurations you are using.
WHAT IS ALGORITHM 1

1 W H A T I S Algorithm
1.1 Algorithm analysis

CODE OUTPUT

DESCRIPTION

▪ “Hello World!!!” will print N times. So, time complexity of above


code is O(N).
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
WHAT IS ALGORITHM 1

1 W H A T I S Algorithm
1.1 Algorithm analysis

CODE DESCRIPTION

▪ If n is true So time complexity will be o(n)2


▪ Here we're nesting two loops. If our array has n items, our outer
loop runs n times and our inner loop runs n times for each iteration
of the outer loop, giving us n2 total prints,
▪ Thus this function runs in O(n2) time (or "quadratic time"). If the
array has 10 items, we have to print 100 times. If it has 1000 items,
we have to print 1000000 times.
WHAT IS ALGORITHM 1

1 W H A T I S Algorithm
1.1 Algorithm analysis

CODE DESCRIPTION

▪ Polynomial time, Triple nested loop ,


▪ This algorithm has a cubic running time: O(n^3).
WHAT IS ALGORITHM 1

1 W H A T I S Algorithm
1.1 Algorithm analysis

DESCRIPTION
CODE

▪ Logarithmic time complexities usually apply to algorithms


that divide problems in half every time.
▪ the algorithm B split the problem in half on each iteration O(log n).
▪ O(log n) - Logarithmic time
▪ The runtime of the work done outside the recursion (line
3-4): O(1)
▪ How many recursive calls the problem is divided (line 11
or 14): 1 recursive call. Notice only one or the other will
happen, never both.
▪ How much n is reduced on each recursive call (line 10 or
13): 1/2. Every recursive call cuts n in half.
WHAT IS ALGORITHM 1

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[].

• A simple approach is to do a linear search, i.e


▪ Start from the leftmost element of arr[] and one by one compare x with each element of arr[]
▪ If x matches with an element, return the index.
▪ If x doesn’t match with any of elements, return -1.
WHAT IS ALGORITHM 1

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

▪ The time complexity of the above algorithm is


O(n).

▪ Linear search is rarely used practically because


other search algorithms such as the binary
search algorithm more faster to found key and
reduce the time
WHAT IS ALGORITHM 1

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

▪ The time complexity is O(log n) - Logarithmic


time
WHAT IS ALGORITHM 1

1 W H A T I S Algorithm
1.5 what is sorting Algorithm

➢ what is sorting Algorithm:


▪ Selection Sort
▪ Bubble Sort
▪ Insertion Sort
▪ Merge Sort
▪ Heap Sort
▪ QuickSort
▪ Radix Sort
▪ Counting Sort
▪ Bucket Sort
▪ ShellSort
▪ Comb Sort
▪ Pigeonhole Sort
▪ Cycle Sort
WHAT IS ALGORITHM 1

1 W H A T I S Algorithm
1.6 Selection Sort

➢ what is Selection Sort Algorithm:

▪ 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.

1) The subarray which is already sorted.

2) Remaining subarray which is unsorted.

▪ 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

➢ what is Selection Sort Algorithm:

▪ Following example explains the above steps


WHAT IS ALGORITHM 1

1 W H A T I S Algorithm
1.6 Selection Sort

➢ what is Selection Sort Algorithm:


WHAT IS ALGORITHM 1

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

▪ Time Complexity: O(n2) as there are two nested


loops.
▪ Auxiliary Space: O(1).
WHAT IS ALGORITHM 1

1 W H A T I S Algorithm
1.7 Bubble sort

➢ what is Bubble sort algorithms

▪ 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

➢ Graphical Illustration of Bubble Sort:


WHAT IS ALGORITHM 1

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

▪ Worst and Average Case Time Complexity: O(n*n).


Worst case occurs when array is reverse sorted.

▪ Best Case Time Complexity: O(n). Best case occurs


when array is already sorted.

▪ Auxiliary Space: O(1)

You might also like