DAA Unit-2
DAA Unit-2
Statements
For Loop
# Input : int A[n], array of n integers
# Output : Sum of all numbers in array A
𝒏=𝟏𝟎 steps
𝒏=𝟏𝟎𝟎 steps
𝒏=𝟏𝟎𝟎𝟎 steps
𝒏=𝟏𝟎𝟎𝟎𝟎 steps
𝐜
∗ (𝒏 ) 𝟐
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:
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;
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
16
Bubble Sort – Example
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 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
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 :
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
Step 5 :
𝒋 𝒊=𝟓 , 𝒙=𝟏𝟔 𝒋=𝒊 – 𝟏 𝒂𝒏𝒅 𝒋>𝟎
-5 1 5 1 1 2 1 1 while do
1 2 3 2
4 6
5 6 2
7 4
8
-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)
36
Comparison counting
3
4
5
6
2
3
1
2
37
Thank You!