You are on page 1of 44

B.

TECH V SEM
CSE
ACADEMIC YEAR: 2021-2022

Course Name: Design and Analysis of Algorithm


Topic: Decrease and Conquer, Analysis of Insertion, Bubble, Selection Sort

Course code : CS 3102


Credits : 4
Mode of delivery : Hybrid (Power point presentation)
Faculty : Mr. Tarun Jain
Email-id : tarun.jain@jaipur.manipal.edu
1
Assignment

quiz Assessment
Mid term examination criteria’s

End term Examination

11/26/21 CS3102 (DAA), Dept. of CSE 2


Decrease and Conquer,
Analysis of Insertion, Bubble,
Selection Sort

11/26/21 CS3102 (DAA), Dept. of CSE 3


Course Information

• Course Handout
• Communicate through eMail
• Office hours
• To be communicated
• Grading policy
• Will be communicated as per university guidelines

CS3102 (DAA), Dept. of CSE


11/26/21 4
Syllabus
• Introduction: Fundamentals of Algorithms, Important Problem Types,
Analysis of algorithm efficiency. Analysis Framework: Asymptotic Notations
and Basic Efficiency Classes. Mathematical Analysis of Nonrecursive and
Recursive Algorithms: Brute force Techniques, Divide and Conquer. Decrease
and Conquer: Insertion Sort, Depth First Search, Breadth First Search,
Topological Sorting. Transform and Conquer: Presorting, BST, Heapsort. Space
and Time tradeoffs: Input Enhancement in String Matching. Dynamic
Programming: Warshall's and Floyd's Algorithms, The Knapsack Problem.
Greedy Techniques: Prim's, Kruskal's and Dijkstra's Algorithm, Huffman Trees.
Coping with limitations of algorithmic power. Backtracking: nQueens problem,
Hamiltonian Circuit Problem, Subset Sum Problem. Branch and Bound:
Assignment Problem, Knapsack Problem, TSP. P, NP, and NP-complete
Problems.

CS3102 (DAA), Dept. of CSE


11/26/21 5
More Information

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

CS3102 (DAA), Dept. of CSE


11/26/21 6
Course Objectives

• CS1501.1 Analyse the running times of algorithms using asymptotic analysis.


• CS1501.2 Demonstrate and Design algorithms using
divide-and-conquer paradigm to solve business problems hence enhance
skills.
• CS1501.3 Illustrate the concept of greedy and dynamic-programming
approach to solve real life problems to enhance entrepreneurship capabilities.
• CS1501.4 Demonstrate the concept of backtracking
and branch & bound algorithms.
• CS1501.5 Synthesize and analyse various advanced algorithms concept such as
graphs, string matching, approximation algorithms and complexity classes to
enhance employability.

CS3102 (DAA), Dept. of CSE


11/26/21 7
Introduction
• The decrease-and-conquer technique is based
on exploiting the relationship between a solution
to a given instance of a problem and a solution
to a smaller instance of the same problem.

• Once such a relationship is established, it can


be exploited either top down (recursively) or
bottom up (without a recursion).

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

This usually results in a recursive algorithm.


Decrease by a constant
• In the decrease-by-a-constant variation, the
size of an instance is reduced by the same
constant on each iteration of the algorithm.
• Typically, this constant is equal to 1

10
Decrease by a Constant

11
Decrease by a constant

• Consider, as an example, the exponentiation problem of computing an


for positive integer exponents. The relationship between a solution to
an instance of size n and an instance of size n - 1 is obtained by the

obvious formula: an = an-1 x a


• So the function f (n) = an can be computed either “top down” by using
its recursive definition

• or “bottom up” by multiplying a by itself n - 1 times.

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.

• In most applications, this constant factor is equal


to two.

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.

• But since we consider instances of the exponentiation problem with


integer exponents only, the former works only for even n. If n is odd, we
have to compute an-1 by using the rule for even-valued exponents and
then multiply the result by

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.

• Euclid’s algorithm for computing the greatest common divisor provides a


good example of such a situation.

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

To insert 12, we need to


make room for it by moving
first 36 and then 24.
6 10 24 36

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:

left sub-array right sub-array

sorted unsorted

21
Insertion Sort

22
Recursive Insertion Sort

23
#include <stdio.h>

// Recursive function to sort an array using insertion sort


void insertionSortRecursive(int arr[], int n)
{
// Base case
if (n <= 1)
return;

// Sort first n-1 elements


insertionSortRecursive( arr, n-1 );

// Insert last element at its correct position in sorted array.


int last = arr[n-1];
int j = n-2;

/* 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]);
}

/* Driver program to test insertion sort */


int main()
{
int arr[] = {12, 11, 13, 5, -2};
int n = sizeof(arr)/sizeof(arr[0]);

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

• Insertion sort – sorts the elements in place 28


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
i←j-1 c4 n-1

n
t
while i > 0 and A[i] > key c5 j 2 j


n
(t j  1)
do A[i + 1] ← A[i] c6 j 2


n
i←i–1 (t j  1)
c7 j 2

A[i + 1] ← key c8 n-1


tj: # of times the while statement is executed at iteration j

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

• T(n) = c1n + c2(n -1) + c4(n -1) + c5(n -1) + c8(n-1) =


(c1 + c2 + c4 + c5 + c8)n + (c2 + c4 + c5 + c8)

= 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) = (n2) order of growth in n 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 31
Average Case Analysis

• Instead of an input of a particular type (as in best


case or worst case), all the inputs of the given size
are equally probable in such an analysis.
– E.g. coming back to our insertion sort, if the elements in
the array A[0..j-1] are randomly chosen. We can
assume that half the elements are greater than A[j]
while half are less. On the average, thus tj=j/2. Plugging
this value into T(n) still leaves it quadratic. Thus, in this
case average case is equivalent to a worst case run of
the algorithm.
– Does this always occur? NO. The average case may tilt
towards the best case also.

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

i←j-1  n2/2 comparisons c4 n-1



n

while i > 0 and A[i] > key c5 j 2 j


t


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)

A[i + 1] ← key c8 n-1


33
Insertion Sort - Summary
• Advantages
– Good running time for “almost sorted” arrays (n)
• Disadvantages
 (n2) running time in worst and average case
  n2/2 comparisons and exchanges

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

for (i =0; i < n; i++) c2

for (j = 0; j < n-i-1; j++) c3

Comparisons:  n2/2 if A[j] > A[j + 1]


c4

Exchanges:  n2/2 then exchange A[j]  A[j+1]


n
T(n) = c1(n+1) + c2  ( n  i )  c3
i 1
+ c4
n

= (n) + (c2 + c3 + c4)  (n  i )


i 1
n n n
n ( n  1) n 2
n
where  (n  i )  n   i  n 2   
i 1 i 1 i 1 2 2 2
Thus,T(n) = (n2)
39
Selection Sort

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

CS1501, Dept of CSE,SCIT, MUJ


41
Example
8 4 6 9 2 3 1 1 2 3 4 9 6 8

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

CS1501, Dept of CSE,SCIT, MUJ


42
Selection Sort
Alg.: SELECTION-SORT(A)
8 4 6 9 2 3 1
n ← length[A]
for j ← 1 to n - 1
do smallest ← j
for i ← j + 1 to n
do if A[i] < A[smallest]
then smallest ← i
exchange A[j] ↔ A[smallest]

CS1501, Dept of CSE,SCIT, MUJ


43
Analysis of Selection Sort
cost times
Alg.: SELECTION-SORT(A)
c1 1
n ← length[A]
for j ← 1 to n - 1 c2 n

do smallest ← j c3 n-1

n1
for i ← j + 1 to n c4 j
(n  j
nj
do if A[i] < A[smallest] c5 
n1
j
( )

c6 
n1
then smallest ← i j1
(n  j)

exchange A[j] ↔ A[smallest] c7 n-1


n1 n1 n1
T (n)  c1  c2 n  c3 (n 1)  4  (n  j 1)  5 (n  j)  c  (n  j)  c (n 1)  (n )
6 7
2

j1 c j 1 j 1
c 55

You might also like