Professional Documents
Culture Documents
TECH V SEM
CSE
ACADEMIC YEAR: 2021-2022
quiz Assessment
Mid term examination criteria’s
• Course Handout
• Communicate through eMail
• Office hours
• To be communicated
• Grading policy
• Will be communicated as per university guidelines
• Textbook
•Introduction to Algorithms 3rd ,Cormen, Leiserson, Rivest
and Stein, The MIT Press,
•Fundamentals of Computer Algorithms, 2nd, Sartaj Sahni,
Ellis Horowitz, Sanguthevar Rajasekaran
• Others
•Introduction to Design & Analysis Computer Algorithm 3rd,
Sara Baase, Allen Van Gelder, Adison-Wesley, 2000.
•Algorithms, Richard Johnsonbaugh, Marcus Schaefer, Prentice
Hall, 2004.
•Introduction to The Design and Analysis of Algorithms 2 nd
Edition, Anany Levitin, Adison-Wesley, 2007.
8
3 Types of Decrease and Conquer
• Decrease by a constant (usually by 1):
– insertion sort
– graph traversal algorithms (DFS and BFS)
– topological sorting
• Decrease by a constant factor (usually by half)
– binary search
• Variable-size decrease
– Euclid’s algorithm
10
Decrease by a Constant
11
Decrease by a constant
12
Decrease by a Constant Factor
• The decrease-by-a-constant-factor technique
suggests reducing a problem’s instance by the
same constant factor on each iteration of the
algorithm.
13
Decrease by a Constant Factor
14
Decrease by a Constant Factor
• If the instance of size n is to compute an, the instance of half its size will
be to compute an/2
n/2, with the obvious relationship between the two: an =
(an/2
n/2)2.
15
Variable Size Decrease
• the variable-size-decrease variety of decrease-and-conquer, a size
reduction pattern varies from one iteration of an algorithm to another.
16
Insertion Sort
• Idea: like sorting a hand of playing cards
– Start with an empty left hand and the cards facing down on the table.
– Remove one card at a time from the table, and insert it into the correct
position in the left hand
• compare it with each of the cards already in the hand, from right to left
– The cards held in the left hand are sorted
• these cards were originally the top cards of the pile on the table
17
Insertion Sort
12
18
Insertion Sort
6 10 24 36
12
19
Insertion Sort
6 10 24 3
6
12
20
Insertion Sort
input array
5 2 4 6 1
3
at each iteration, the array is divided in two sub-arrays:
sorted unsorted
21
Insertion Sort
22
Recursive Insertion Sort
23
#include <stdio.h>
/* Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position */
while (j >= 0 && arr[j] > last)
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = last;
}
24
// A utility function to print an array of size n
void printArray(int arr[], int n)
{
for (int i=0; i < n; i++)
printf("%d ",arr[i]);
}
insertionSortRecursive(arr, n);
printArray(arr, n);
return 0;
}
25
Recursion Idea.
1.Base Case: If array size is 1 or smaller, return.
2.Recursively sort first n-1 elements.
3.Insert last element at its correct position in sorted array.
26
Iterative Insertion Sort
27
Alg.: INSERTION-SORT(A) 1 2 3 4 5 6 7 8
a1 a 2 a3 a4 a 5 a6 a7 a 8
for j ← 2 to n
do key ← A[ j ] key
Insert A[ j ] into the sorted sequence A[1 . . j -1]
i←j-1
while i > 0 and A[i] > key
do A[i + 1] ← A[i]
i←i–1
A[i + 1] ← key
n
(t j 1)
do A[i + 1] ← A[i] c6 j 2
n
i←i–1 (t j 1)
c7 j 2
T (n) c1n c2 (n 1) c4 (n 1) c5 t j c6 t j 1 c7 t j 1 c8 (n 1)
n n n
j 2 j 2 j 2
29
Best Case Analysis
• The array is already sorted “while i > 0 and A[i] > key”
– A[i] ≤ key upon the first time the while loop test is run
(when i = j -1)
– tj = 1
= an + b = (n)
T (n) c1n c2 (n 1) c4 (n 1) c5 t j c6 t j 1 c7 t j 1 c8 (n 1)
n n n
j 2 j 2 j 2
30
Worst Case Analysis
• The array is in reverse sorted order “while i > 0 and A[i] > key”
– Always A[i] > key in while loop test
– Have to compare key with all elements to the left of the j-th
position compare with j-1 elements tj = j
n
n(n 1) n
n(n 1) n
n(n 1)
using j 1
j
2
j
j 2 2
1 ( j 1)
j 2 2
we have:
n( n 1) n( n 1) n( n 1)
T ( n ) c1n c2 (n 1) c4 ( n 1) c5 1 c6 c7 c8 ( n 1)
2 2 2
an 2 bn c a quadratic function of n
T (n) c1n c2 (n 1) c4 (n 1) c5 t j c6 t j 1 c7 t j 1 c8 (n 1)
n n n
j 2 j 2 j 2 31
Average Case Analysis
32
Comparisons and Exchanges in
Insertion Sort
INSERTION-SORT(A) cost times
for j ← 2 to n c1 n
do key ← A[ j ] c2 n-1
Insert A[ j ] into the sorted sequence A[1 . . j -1] 0 n-1
n
do A[i + 1] ← A[i] c6 j 2
(t j 1)
n
i←i–1 n 2
/2 exchanges c7 j 2
(t j 1)
34
Bubble Sort
35
Bubble Sort
• Bubble Sort is a simple algorithm which is used to sort a given set of n
elements provided in form of an array with n number of elements. Bubble
Sort compares all the element one by one and sort them based on their
values.
• If the given array has to be sorted in ascending order, then bubble sort will
start by comparing the first element of the array with the second element, if
the first element is greater than the second element, it will swap both the
elements, and then move on to compare the second and the third element,
and so on.
• If we have total n elements, then we need to repeat this process for n-1
times.
• It is known as bubble sort, because with every complete iteration the largest
element in the given array, bubbles up towards the last place or the highest
index, just like a water bubble rises up to the water surface.
• Sorting takes place by stepping through all the elements one-by-one and
comparing it with the adjacent element and swapping them if required.
• Easier to implement, but slower than Insertion sort
36
Example
37
Bubble Sort
Alg.: BUBBLESORT(int arr[], int n)
for(i = 0; i < n; i++)
{
for(j = 0; j < n-i-1; j++)
{
if( arr[j] > arr[j+1])
{
// swap the elements
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
Animation: https://yongdanielliang.github.io/animation/web/BubbleSortNew.html 38
Bubble-Sort Running Time
Alg.: BUBBLESORT(A) c1
40
Selection Sort
• Idea:
Find the smallest element in the array
Exchange it with the element in the first position
Find the second smallest element and exchange it
with the element in the second position
Continue until the array is sorted
• Disadvantage:
Running time depends only slightly on the
amount of order in the file
1 4 6 9 2 3 8 1 2 3 4 6 9 8
1 2 6 9 4 3 8 1 2 3 4 6 8 9
1 2 3 9 4 6 8 1 2 3 4 6 8 9
do smallest ← j c3 n-1
n1
for i ← j + 1 to n c4 j
(n j
nj
do if A[i] < A[smallest] c5
n1
j
( )
c6
n1
then smallest ← i j1
(n j)
j1 c j 1 j 1
c 55