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

Algo Lab5- Revision and Quicksort

The document provides an overview of recursion in algorithms, detailing the structure of recursive functions, including base cases and recursive calls. It explains how to analyze recursive code using recurrence relations and methods such as substitution, recursion-tree, and master methods. Additionally, it covers the QuickSort algorithm, illustrating its steps and pseudocode for sorting arrays using a divide-and-conquer approach.

Uploaded by

hikalandsomeone
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)
2 views

Algo Lab5- Revision and Quicksort

The document provides an overview of recursion in algorithms, detailing the structure of recursive functions, including base cases and recursive calls. It explains how to analyze recursive code using recurrence relations and methods such as substitution, recursion-tree, and master methods. Additionally, it covers the QuickSort algorithm, illustrating its steps and pseudocode for sorting arrays using a divide-and-conquer approach.

Uploaded by

hikalandsomeone
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/ 33

Theory of Algorithms

Lab 5
Recursion Recap
Function calls itself for one or more times
Consists of two parts:
- Recursive part
- Base case (stopping condition)
Fun(…)
General form: ….
Base Case
….
Fun(…), Fun(…), …etc Recursive Code
Normal Code
….
….
Theory of algorithms Lab4
How to Analyze Recursive
Code?
1. Calculate Running Time T(N) [Recurrence]
T(N) = Time of Normal Code + T(Recursive Code) + … etc
T(Base) = Time of Base Case

2. Solve it to Get the Order ( or O)


1. Substitution method
2. Recursion-tree method
3. Master method

Theory of algorithms Lab4


1. Calculate Recurrence
Relation T(N)
Example1: Factorial
Fact(N)
If (N == 0)
return 1
else
return N × Fact(N-1)

Theory of algorithms Lab4


1. Calculate Recurrence
Relation T(N)
Example1: Factorial
Fact(N)
Base Case If (N == 0)
return 1
Normal Code
else
return N × Fact(N-1) Recursive Code

T(N) = (1) + T(N – 1)


T(0) = (1)
Theory of algorithms Lab4
1. Calculate Recurrence
Relation T(N)
Example2: Fibonacci
Fib(N)
If (N == 1 or N == 2)
return 1
else
return Fib(N-1) + Fib(N-2)
Theory of algorithms Lab4
1. Calculate Recurrence
Relation T(N)
Example2: Fibonacci
Fib(N)
Base Case If (N == 1 or N == 2)
return 1
Normal Code
else
return Fib(N-1) + Fib(N-2) Recursive Code

T(N) = (1) + T(N – 1) + T(N – 2)


T(1) = (1)
Theory of algorithms Lab4
1. Calculate Recurrence
Relation T(N)
Example3: Binary Search
BinSrch(A, I, S, E)
If (S > E)
return False
M = (S + E) / 2
If (I == A[M]) return true
else If (I > A[M])
return BinSrch(A, I, M+1, E)
else If (I < A[M])
return BinSrch(A, I, S, M-1)
Theory of algorithms Lab4
1. Calculate Recurrence
Relation T(N)
Example3: Binary Search
BinSrch(A, I, S, E) T(N) = (1) + T(N / 2)
If (S > E)
Base Case return False T(1) = (1)
M = (S + E) / 2
If (I == A[M]) return true
Normal Code else If (I > A[M])
return BinSrch(A, I, M+1, E) Recursive Code
else If (I < A[M]) OR
return BinSrch(A, I, S, M-1) Recursive Code

Theory of algorithms Lab4


How to Analyze Recursive
Code?
1. Calculate Running Time T(N) [Recurrence]
T(N) = Time of Normal Code + T(Recursive Code) + … etc
T(Base) = Time of Base Case

2. Solve it to Get the Order ( or O)


1. Substitution Method
2. Recursion-tree Method
3. Master Method

Theory of algorithms Lab4


Substitution
Method
The Substitution Method is a powerful technique for solving recurrence
relations by guessing a solution and proving it correct using mathematical
induction. This method helps us find the time complexity of recursive
algorithms.

Theory of algorithms Lab4


Substitution
Method
Example:
Solution:
step 1
step 2
step 3
step k
T(1) will be reached when n = 2k ➔ k =

Theory of algorithms Lab4


Master
Method
Suitable ONLY for recurrences of the following form:

T(N) = 2 T(N/2) + cN2 T


T(N) = T(N/3) + O(1) T
T(N) = 2 T(3N/2) + √N F (b = 2/3 < 1)
T(N) = T(N-1) + T(N-2) + c F (not on the form)
Theory of algorithms Lab4
Master
Method
log 𝒃 𝒂
𝒇 ( 𝑵 ) 𝒗𝒔 𝑵
Theory of algorithms Lab4
Master
Method
Case 1: If :
-
Case 2: If :
-
Case 3: If : ,.
AND
-
Theory of algorithms Lab4
Master
Method
 

  n 
log b a
f (n) O n 
log b a   


 
   0
T (n)  n log b a
log n  f (n)  n 
log b a

  c 1
 
 f (n)  f (n)  n 
log b a 
AND  
 
 af (n / b)  cf (n) for large n
Theory of algorithms Lab4
Master
Method
Example 1:
Solution:

Since where =0.2, case 3 applies:


Check the condition:

Thus the solution is


Theory of algorithms Lab4
Master
Method
Example 2:
Solution:

CASE 2: f (n) = Q(n2lg0n), that is, k = 0.


 T(n) = Q(n2lg n).

Theory of algorithms Lab4


Master
Method
Example 3:
Solution:

Since where =1.5, case 3 applies:


Check the condition:

Thus the solution is

Theory of algorithms Lab4


Master
Method
Example 4:
Solution:

Not on a form
Cannot solve by master method

Theory of algorithms Lab4


Master
Method
Example 5:
Solution:

Since where 0<<2, case 1 applies:


Thus the solution is

Theory of algorithms Lab4


Master
Method
Example 6:
Solution:

Since case 2 applies:


Thus the solution is (

Theory of algorithms Lab4


Quick Sort
• QuickSort is a sorting algorithm
• works on the principle of divide and conquer, breaking
down the problem into smaller sub-problems.
• that picks an element as a pivot and partitions the given array
around the picked pivot by placing the pivot in its correct
position in the sorted array.
How does QuickSort Algorithm
work?
There are mainly three steps in the algorithm:
1. Choose a Pivot: Select an element from the array as the pivot. The choice of pivot can
vary (e.g., first element, last element, random element, or median).

2. Partition the Array: Rearrange the array around the pivot. After partitioning, all
elements smaller than the pivot will be on its left, and all elements greater than the
pivot will be on its right. The pivot is then in its correct position, and we obtain the
index of the pivot.

3. Recursively Call: Recursively apply the same process to the two partitioned sub-arrays
(left and right of the pivot).

Base Case: The recursion stops when there is only one element left in the sub-array, as a
single element is already sorted.
Pseudocode Quicksort Function

1. if p < r → Check if there is more than one element in the array


section.
•p (Left index)
•r (Right index)
•If p is not smaller than r, the section is already sorted, so stop.

2. q = PARTITION(A, p, r) → Call the PARTITION function.


•q is the index of the pivot after partitioning. • What each letter refers to:
•The pivot is now in its final sorted position. A → The array.
p → The starting index of the array
section.
3. QUICKSORT(A, p, q - 1)
r → The ending index of the array section.
→ Recursively sort the left side of the pivot.
q → The final position of the pivot after
partitioning.
4. QUICKSORT(A, q + 1, r)
→ Recursively sort the right side of the pivot.
Pseudocode Partition Function

• A → The array.
• p → The starting index of the section
being partitioned.
• r → The last index (pivot).
• x → The pivot element (A[r]).
• i → The boundary between smaller and
larger elements.
• j → The index scanning through the
array.
Pseudocode Partition
Function(Cont.)
•x = A[r] → Choose the last element (A[r]) as the pivot.
•i = p - 1 → Set i to one position before the starting index (p).
•Loop j = p to r - 1 → Move through the array up to the pivot.
•If A[j] ≤ x → If A[j] is smaller than or equal to the pivot:
•Move the i pointer forward (i = i + 1).
•Swap A[i] with A[j].
•After the loop,
•swap A[i + 1] with A[r] → Put the pivot in its correct place.
•Return i + 1 → This is the final index of the pivot.
Summary of Quicksort
pseudocode
•QuickSort recursively sorts the left and right sides after partitioning.
•Partition rearranges the elements so that the pivot is in the correct position.
•Every letter in the pseudocode has a clear role:
•A (Array),
•p (Start),
• r (End),
•x (Pivot),
• i (Smaller boundary),
• j (Iterator),
• q (Pivot final index).
Example

We will apply Quicksort

to:[2, 8, 7, 1, 3, 5, 6, 4]
• Initial Values:

p = 0 (First index)

r = 7 (Last index)

Pivot = 4 (A[r])
Step 1: Partitioning Process
We start PARTITION(A, 0, 7) with pivot 4.

After partitioning, the pivot 4 is at index 3. After partitioning, the pivot 4 is at index 3.
2: Recursively Apply Quicksort
(Left)
Quicksort on Left Side [2, 1, 3]
Pivot: 3 (A[2])

Sorted: [1, 2, 3]
2: Recursively Apply Quicksort
(Right)
Quicksort on Right Side [7, 5, 6, 8]
Pivot: 8 (A[7])

Sorted: [5, 6, 7, 8]
Final Sorted Array [1, 2, 3, 4, 5, 6, 7, 8]

You might also like