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

DAA Lecture 02

The document discusses the Divide and Conquer approach in algorithm design, emphasizing its recursive nature and the three main steps: dividing the problem, conquering sub-problems, and combining solutions. It provides examples of algorithms such as Merge Sort, Binary Search, and Quick Sort, detailing their processes and efficiencies. The analysis of Quick Sort highlights its best, worst, and average case time complexities.

Uploaded by

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

DAA Lecture 02

The document discusses the Divide and Conquer approach in algorithm design, emphasizing its recursive nature and the three main steps: dividing the problem, conquering sub-problems, and combining solutions. It provides examples of algorithms such as Merge Sort, Binary Search, and Quick Sort, detailing their processes and efficiencies. The analysis of Quick Sort highlights its best, worst, and average case time complexities.

Uploaded by

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

The University of Agriculture

Peshawar, Pakistan

CSC-411 Design & Analysis of Algorithms

Lecture 2

Engr. Waseem Ullah Khan

waseem@uetpeshawar.edu.pk
Divide and Conquer Approach

waseem@uetpeshawar.edu.pk 2
Divide and Conquer Approach
Many useful algorithms are recursive in structure

To solve a given problem, they call themselves recursively one or more times to deal with
closely related sub-problems

These algorithms typically follow a divide-and-conquer approach

These algorithms break the problem into several sub-problems that are similar to the
original problem but smaller in size, solve the sub-problems recursively, and then combine
these solutions to create a solution to the original problem

Examples: Merge sort, Binary Search, Quick sort

waseem@uetpeshawar.edu.pk 3
Divide and Conquer Approach
The divide-and-conquer paradigm involves three steps at each level of the recursion:
➢ Divide the problem into a number of sub-problems that are smaller instances of the same
problem

➢ Conquer the sub-problems by solving them recursively

➢ Combine the solutions to the sub-problems into the solution for the original problem

waseem@uetpeshawar.edu.pk 4
Merge Sort
Split array A[0..n-1] in two about equal halves and make copies of each half in arrays B & C

Sort arrays B and C recursively

Merge sorted arrays B and C into array A as follows:


➢ Repeat the following until no elements remain in one of the arrays:
❑ Compare the first elements in the remaining unprocessed portions of the arrays

❑ Copy the smaller of the two into A, while incrementing the index indicating the unprocessed portion of that array

➢ Once all elements in one of the arrays are processed, copy the remaining unprocessed
elements from the other array into A

waseem@uetpeshawar.edu.pk 5
Merge Sort Example

waseem@uetpeshawar.edu.pk 6
Merge Sort Example

waseem@uetpeshawar.edu.pk 7
Merge Sort

waseem@uetpeshawar.edu.pk 8
Merge Sort

waseem@uetpeshawar.edu.pk 9
Binary Search Example

waseem@uetpeshawar.edu.pk 10
Binary Search Example

waseem@uetpeshawar.edu.pk 11
Binary Search Example

waseem@uetpeshawar.edu.pk 12
Binary Search

waseem@uetpeshawar.edu.pk 13
Binary Search

waseem@uetpeshawar.edu.pk 14
Quick Sort
Select a pivot (partitioning element) – here, the first element

Rearrange the list so that all the elements in the first s positions are smaller than or equal to
the pivot and all the elements in the remaining n-s positions are larger than or equal to the
pivot (see next slide for an algorithm)

Exchange the pivot with the last element in the first (i.e., ≤) subarray — the pivot is now in
its final position

Sort the two subarrays recursively

waseem@uetpeshawar.edu.pk 15
Quick Sort

waseem@uetpeshawar.edu.pk 16
Analysis of Quick Sort
Divide-and-conquer:
➢ Partition array into two subarrays, recursively sort

➢ All of first subarray < all of second subarray

➢ No merge step needed!

Best-case time efficiency: split in the middle — Θ(n log n)

Worst-case time efficiency: sorted array! — Θ(n2)

Average case time efficiency: random arrays — Θ(n log n)

waseem@uetpeshawar.edu.pk 17

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy