0% found this document useful (0 votes)
6 views38 pages

DAA Unit-2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views38 pages

DAA Unit-2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Analyzing Control

Statements
For Loop
# Input : int A[n], array of n integers
# Output : Sum of all numbers in array A

Algorithm: int Sum(int A[], int n)


{
int s=0; n+1
for (int i=0; i<n; i++)
1 s = s + A[i];
return s;
} n

Total time taken =


n+1+n+2 = 2n+3
Time Complexity f(n) =
2
Running Time of Algorithm
 The time complexity of the algorithm is :
 Estimated running time for different values of 𝑛 :

𝒏=𝟏𝟎 steps

𝒏=𝟏𝟎𝟎 steps

𝒏=𝟏𝟎𝟎𝟎 steps

𝒏=𝟏𝟎𝟎𝟎𝟎 steps

 As 𝑛 grows, the number of steps grow in linear proportion to 𝑛 for the


given algorithm Sum.
 The dominating term in the function of time complexity is : As gets large,
the becomes insignificant.
 The time is linear in proportion to .
3
Analyzing Control Statements
Exampl Exampl
e 1: e 3:
𝐜 𝒄 𝟏 ( 𝒏 +𝟏 )
 Statement is executed once 𝒄 𝟐 𝒏 ( 𝒏 +𝟏 )
only
 So, The execution time is 𝒄 𝟑 ∗ 𝒏∗𝒏
some constant  Analysis
Exampl
e 2:
𝐜
∗ (𝒏+𝟏 )
𝟏

𝐜
∗ (𝒏 ) 𝟐
 Total time is denoted as,

4
Analyzing Control Statements
Exampl Exampl
e 4: e 6:
𝜽 ( 𝒏𝟐 )

𝜽 (𝒏)

𝒕 ( 𝒏 )=𝜽 ( 𝒏𝟑 )
𝜽 ( 𝟏)
printf(“sum is now %d”,)
Exampl
e 5:

𝒕 ( 𝒏 )=𝜽 ( 𝒏𝟔 )
5
Analyzing Control Statements
Exampl Exampl
e 7: e 9:

𝒕 ( 𝒏 )=𝜽 (𝒍𝒐𝒈𝒏) 𝒕 ( 𝒏 )=𝜽 ( 𝒍𝒐𝒈𝒍𝒐𝒈𝒏 )

Exampl Example
e 8: 10:

(where f(n) is any sqrt or cuberoot


function)

𝒕 ( 𝒏 )=𝜽 ( 𝒍𝒐𝒈𝒏 ) 𝒕 ( 𝒏 )=𝜽 ( 𝒍𝒐𝒈𝒍𝒐𝒈𝒏 )


6
Recursion

7
Example:
int fun(int x)
{
if(x < 1)
return;
fun(x - 1);
printf("%d ",x);
}

8
Example:
int fun(int n) int fun(int n)
{ {
// wrong base case (it may cause stack overflow)
if (n < = 1) // base case
if (n == 100)
return 1; return 1;
else
else
return n + fun(n-1);
return n + fun(n-1);
} }

9
Algorithm to find factorial using recursion
Factorial of n
 Factorial of any number n is denoted as n! and is equal to
 n! = 1 x 2 x 3 x ... x (n – 2) x (n – 1) x n
 Factorial of 3:
3! = 1 x 2 x 3
=6 int factorial(int n)
{
int fact = 1, i;

for (i = 2; i <= n; i++)


fact = fact * i;

return fact;
}

10
Algorithm: FACTORIAL
Step 1: Start
Step 2: Read number n
Step 3: Call factorial(n)
Step 4: Print factorial f
Step 5: Stop

factorial(n)
Step 1: If n==1 then return 1
Step 2: Else
f=n*factorial(n-1)
Step 3: return f

11
FACTORIAL
int fun(int n)
{
if (n < = 1) // base case
return 1;
else
return n * fun(n-1);
}

12
#include <stdio.h>
int SUM(int x, int y)
{
if (x==y)
return(1);
else if (x>y)
return(0);
else
return (x + SUM(x+2, y));
}

int main()
{
int y;
y=SUM(1,9);
printf("%d",y);

return 0;
}
Examples:

Output: 199

Output: 0 1 2 0 3 0 1
Output: 13

14
Sorting Algorithms

• Bubble Sort, Selection Sort, Insertion Sort


Introduction
 Sorting is any process of arranging items systematically or arranging
items in a sequence ordered by some criterion.
 Applications of Sorting
1. Phone Bill: the calls made are date wise sorted.
2. Bank statement or Credit card Bill: transactions made are date wise sorted.
3. Filling forms online: “select country” drop down box will have the name of countries
sorted in Alphabetical order.
4. Online shopping: the items can be sorted price wise, date wise or relevance wise.
5. Files or folders on your desktop are sorted date wise.

16
Bubble Sort – Example

Sort the following array in Ascending order


45 34 56 23 12

Pass 1 :
3
4 swap
3 3 3
5
4
3 4 4 4
4
5 5 5
2 5
2

swap
6
2 6
2 6
3
2
5 3
5
1

swap
3
1 3
1 3
6
1 6
2
1
5
2 2 2 2
6

17
Bubble Sort – Example
Pass 2 : Pass 3 : Pass 4 :
3 3 3 2
3 2 1
2

swap
swap
4 4
2 4
2 4
3
2 3
1 3
2
1

swap
swap
5
2 5
3
2
4 3
4
1 3
4
1 4
2
1
3 2
3

swap
3
1 3
5
1 5
2
1
4 2
4 2
4 4
2
5 2
5 2
5 5 5 5
6 6 6 6 6 6

18
Bubble Sort - Algorithm
# Input: Array A
# Output: Sorted array A

Algorithm: Bubble_Sort(A)
for i ← 1 to n-1 do 𝛉 (𝐧)
for j ← 1 to n-i do
if A[j] > A[j+1] then
temp ← A[j] A[j+1])
swap(A[j], 𝛉 ( 𝐧 𝟐)
A[j] ← A[j+1]
A[j+1] ← temp

19
Bubble Sort
 It is a simple sorting algorithm that works by comparing each pair of
adjacent items and swapping them if they are in the wrong order.
 The pass through the list is repeated until no swaps are needed, which
indicates that the list is sorted.
 As it only uses comparisons to operate on elements, it is a comparison
sort.
 Although the algorithm is simple, it is too slow for practical use.
 The time complexity of bubble sort is

20
Bubble Sort Algorithm – Best Case Analysis
# Input: Array A Pass 1 : i = 1
# Output: Sorted array A 1 j = 1
Algorithm: Bubble_Sort(A) 2 j = 2
int flag=1; 3
Condition j = 3
never 4
for i ← 1 to n-1 do j = 4
becomes 5
for j ← 1 to n-i do true 9
if A[j] > A[j+1] then Best case time
flag = 0; complexity =
swap(A[j],A[j+1])
if(flag == 1)
cout<<"already sorted"<<endl
break;
21
Selection Sort – Example 1
Sort the following elements in
Ascending order5 1 12 -5 16 2 12 14

Step 1 :
Unsorted Array
5 1 12 -5 16 2 12 14
1 2 3 4 5 6 7 8

Step 2 :  Minj denotes the current index and Minx is


Unsorted Array (elements the value stored at current index.
2 to 8)  So, Minj = 1, Minx = 5
1 1 1 1
-5
5 1 -5
5 2  Assume that currently Minx is the smallest
2 6 2 4 value.
1 2 3 4 5 6 7 8  Now find the smallest value from the remaining
Swap Index = 4, entire Unsorted array.
value = -5
22
Selection Sort – Example 1

Step 3 :
Unsorted Array  Now Minj = 2, Minx = 1
(elements 3 to 8)  Find min value from
1 1 1 1
-5 1 5 2 remaining unsorted array
2 6 2 4
1 2 3 4 5 6 7 8
Index = 2,
value = 1
No Swapping as min value is already at right place
Step 4 :
Unsorted Array  Minj = 3, Minx = 12
(elements 4 to 8)  Find min value from
remaining unsorted array
1 1 1 1 1
-5 1 2 5 2 Index = 6,
2 6 2 2 4
1 2 3 4 5 6 7 8 value = 2

Swap

23
Selection Sort – Example 1

Step 5 : Unsorted Array  Now Minj = 4, Minx = 5


(elements 5 to 8)  Find min value from
1 1 1 1 remaining unsorted array
-5 1 2 5
6 2 2 4 Index = 4,
1 2 3 4 5 6 7 8
value = 5
No Swapping as min value is already at right place
Step 6 :
 Minj = 5, Minx = 16
 Find min value from
Unsorted Array remaining unsorted array
(elements 6 to 8)

1 1 1 1 Index = 6,
-5 1 2 5 value = 12
6
2 2
6 2 4
1 2 3 4 5 6 7 8

Swap
24
Selection Sort – Example 1

Step 7 :
Unsorted Array  Now Minj = 6, Minx = 16
(elements 7 to 8)  Find min value from
1 1 1 1 remaining unsorted array
-5 1 2 5
2 6
2 2
6 4 Index = 7,
1 2 3 4 5 6 7 8
value = 12
Swap

Step 8 :
Unsorted Array  Minj = 7, Minx = 16
(element 8)  Find min value from
remaining unsorted array
1 1 1 1
-5 1 2 5
2 2 6
4 4
6 Index = 8,
1 2 3 4 5 6 7 8 value = 14
Swap
The entire array is
sorted now.
25
Selection Sort
 Selection sort divides the array or list into two parts,
1. The sorted part at the left end
2. and the unsorted part at the right end.
 Initially, the sorted part is empty and the unsorted part is the entire list.
 The smallest element is selected from the unsorted array and swapped
with the leftmost element, and that element becomes a part of the sorted
array.
 Then it finds the second smallest element and exchanges it with the
element in the second leftmost position.
 This process continues until the entire array is sorted.
 The time complexity of selection sort is

26
Selection Sort - Algorithm
# Input: Array A
# Output: Sorted array A

Algorithm: Selection_Sort(A)
for i ← 1 to n-1 do 𝛉 (𝐧)
minj ← i;
minx ← A[i];
for j ← i + 1 to n do
if A[j] < minx then
minj ← j; 𝛉 ( 𝐧 𝟐
)
minx ← A[j];
A[minj] ← A[i];
A[i] ← minx;

27
Selection Sort – Example 2
Algorithm: Selection_Sort(A)
Pass 1 :
for i ← 1 to n-1 do
minj ← i; minx ← A[i]; i = 1
for j ← i + 1 to n do minj ← 21
if A[j] < minx then
minx ← 34
45 No
minj ← j ; minx ← A[j]; Change
A[minj] ← A[i]; j = 2 3
A[i] ← minx; A[j] = 5
34 6
Sort in Ascending
order
45 34 56 23 12
1 2 3 4 5

28
Selection Sort – Example 2
Algorithm: Selection_Sort(A)
Pass 1 :
for i ← 1 to n-1 do
minj ← i; minx ← A[i]; i = 1
for j ← i + 1 to n do 42
minj ← 5
if A[j] < minx then
minx ← 12
34
23
minj ← j ; minx ← A[j];
A[minj] ← A[i]; j = 2 3 4 5
A[i] ← minx; A[j] = 1
23 2
Sort in Ascending Unsorted Array
order
45 34 56 23 12 45 34 56 23 45
12 12
1 2 3 4 5 1 2 3 4 5

Swap
45
12 34
23 56
34 23
34
45
56 45
56
29
Insertion Sort – Example
Sort the following elements in
Ascending order5 1 12 -5 16 2 12 14

Step 1 :
Unsorted Array
5 1 12 -5 16 2 12 14
1 2 3 4 5 6 7 8

Step 2 :

𝒋 𝒊=𝟐 , 𝒙=𝟏 𝒋=𝒊 – 𝟏 𝒂𝒏𝒅 𝒋>𝟎


5
1 1 1 -5 1 2 1 1 while do
1 2 2
3 4 6
5 6 2
7 4
8

Shift
down
30
Insertion Sort – Example

Step 3 :
𝒋 𝒊=𝟑 , 𝒙=𝟏𝟐 𝒋=𝒊 – 𝟏 𝒂𝒏𝒅 𝒋>𝟎
1 5 1 -5 1 2 1 1 while do
1 2 2
3 4 6
5 6 2
7 4
8

No Shift will take


place
Step 4 :
𝒊=𝟒 , 𝒙=− 𝟓 𝒋=𝒊 – 𝟏 𝒂𝒏𝒅 𝒋>𝟎
while do
𝒋 𝒋
-5
1 5 1 -5 1 2 1 1
1 2 2
3 4 6
5 6 2
7 4
8
ShiftShiftShift
downdown down
31
Insertion Sort – Example

Step 5 :
𝒋 𝒊=𝟓 , 𝒙=𝟏𝟔 𝒋=𝒊 – 𝟏 𝒂𝒏𝒅 𝒋>𝟎
-5 1 5 1 1 2 1 1 while do
1 2 3 2
4 6
5 6 2
7 4
8

No Shift will take


place
Step 6 :
𝒊=𝟔 , 𝒙=𝟐 𝒋=𝒊 – 𝟏 𝒂𝒏𝒅 𝒋>𝟎
𝒋 𝒋 while do

-5 1 5
2 1 1 2 1 1
1 2 3 2
4 6
5 6 2
7 4
8

ShiftShiftShift
downdown down
32
Insertion Sort – Example

Step 7 :
𝒋 𝒊=𝟕 , 𝒙=𝟏𝟐 𝒋=𝒊 – 𝟏 𝒂𝒏𝒅 𝒋>𝟎
-5 1 2 5 1 1 1 1 while do
1 2 3 4 2
5 2
6
6 2
7 4
8

Shift
down
Step 8 :
𝒊=𝟖 , 𝒙=𝟏𝟒 𝒋=𝒊 – 𝟏 𝒂𝒏𝒅 𝒋>𝟎
𝒋 while do

-5 1 2 5 1 1 1 1
1 2 3 4 2
5 2
6 4
6
7 4
8
The entire array is
Shift
down sorted now.
33
Insertion Sort - Algorithm
# Input: Array T
# Output: Sorted array T

Algorithm: Insertion_Sort(T[1,…,n])
for i ← 2 to n do
x ← T[i];
𝛉 (𝐧)
j ← i – 1;
while x < T[j] and j > 0 do
T[j+1] ← T[j];
𝛉 ( 𝐧 𝟐)
j ← j – 1;
T[j+1] ← x;

34
Insertion Sort Algorithm – Best Case Analysis
# Input: Array T Pass 1 :
# Output: Sorted array T 1
2
i=2 x=23 T[j]=1
Algorithm: Insertion_Sort(T[1,…,n]) 3 2
i=3 x=34 T[j]=23
for i ← 2 to n do 4
x ← T[i];
𝛉 (𝐧) 5
i=4 x=45 T[j]=34
i=5 x=59 T[j]=45
j ← i – 1; 9
while x < T[j] and j > 0 do The best case time
T[j+1] ← T[j]; complexity of Insertion
j ← j – 1; sort is
T[j+1] ← x; The average and worst
case time complexity of
Insertion sort is

35
Analysis of Insertion Sort(alternate)

Best case: elements already sorted;


tj=1, running time = f(n)

Worst case: elements are sorted


in inverse
order; tj=j, running time = f(n 2)
Average case: tj=j/2, running time = f(n 2

Total time = n(c1 +c2+c3+c7) + – (c2+c3+c5+c6+c7 )

36
Comparison counting
3
4
5
6
2
3
1
2

37
Thank You!

You might also like