SlideShare a Scribd company logo
Algorithms
By Mohamed Gamal
© Mohamed Gamal 2024
Algorithms Analysis & Design - Lecture 9
Divide N Conquer
Merge Sort Algorithm
– Merge Sort uses the divide-and-conquer approach to break the problem into
smaller sub-problems, solve each sub-problem recursively, and then combine the
results.
– Merge Sort is a comparison-based sorting algorithm that repeatedly compares
pairs of elements to determine their order.
– Merge Sort is typically implemented using recursion, although it can also be
implemented iteratively.
Merge Sort Algorithm
1) Divide the array into two halves.
2) Recursively apply Merge Sort to each half.
3) Merge the two sorted halves back together:
a) Compare the elements of the two halves and arrange them in order.
b) Continue merging elements until all elements from both halves are merged into a single sorted array.
4) Repeat the process until the entire array is sorted.
2 8 5 3 9 4 1 7
Algorithms Analysis & Design - Lecture 9
Algorithms Analysis & Design - Lecture 9
Algorithms Analysis & Design - Lecture 9
Algorithms Analysis & Design - Lecture 9
Complexity:
▪ Worst Case: 𝑂(𝑛 log 𝑛)
▪ Average Case: 𝑂(𝑛 log 𝑛)
▪ Best Case: 𝑂(𝑛 log 𝑛)
Advantages:
▪ Merge Sort is well-suited for sorting large datasets that do not fit into memory (external sorting)
because it accesses data sequentially and has good performance with large datasets.
▪ Merge Sort is a stable sort, meaning that it preserves the relative order of equal elements in the
sorted array.
Disadvantages:
▪ Merge Sort is not in-place sorting algorithm since it requires additional space proportional to the
size of the array being sorted.
Let 𝐶(𝑛) be the number of comparisons made by the merge-sort algorithm.
Recurrence relation equation: 𝐶 𝑛 = 2𝐶
𝑛
2
+ 𝑛 and 𝐶 1 = 0
• Using backward substitution:
𝐶(𝑛) = 2𝐶
𝑛
2
+ 𝑛
= 2 2𝐶
𝑛
4
+
𝑛
2
+ 𝑛 = 4𝐶
𝑛
4
+ 2𝑛
= 4 2𝐶
𝑛
8
+
𝑛
4
+ 2𝑛 = 8𝐶
𝑛
8
+ 3𝑛
𝐶 𝑛 = 2𝑖 𝐶
𝑛
2𝑖 + 𝑖 𝑛
= 2log2 𝑛
𝐶
𝑛
2log2 𝑛 + log2 𝑛 𝑛
= 𝑛 log2 𝑛
𝑛
2𝑖
= 1
2𝑖 = 𝑛
𝑖 = log2 𝑛
Basic Operation
n represents the number of comparisons required
to merge the two sorted halves
Divide N Conquer
Quick Sort Algorithm
– Quick Sort uses the divide-and-conquer approach to break the problem into
smaller sub-problems, sort each sub-problem recursively, and then combine the
results.
– Quick Sort is typically implemented using recursion, although it can also be
implemented iteratively.
– The choice of the pivot element is crucial for the performance of Quick Sort.
Good pivot selection strategies can significantly improve its efficiency.
Algorithms Analysis & Design - Lecture 9
Note: Pivots shown in bold.
Algorithms Analysis & Design - Lecture 9
Complexity:
▪ Worst Case: 𝑂(𝑛2) – sorted array!
▪ Average Case: 𝑂(𝑛 log 𝑛) – random arrays
▪ Best Case: 𝑂 𝑛 log 𝑛 – split in the middle
Advantages:
▪ Quick Sort is in-place sorting algorithm, meaning it requires only a small, constant amount of additional
memory space.
▪ Despite its worst-case scenario, Quick Sort is often faster in practice compared to other 𝑂 𝑛 log 𝑛
algorithms like Merge Sort due to its good cache performance and low overhead.
Disadvantages:
▪ The worst-case time complexity is 𝑂(𝑛2), which occurs when the pivot selection consistently results in
highly unbalanced partitions.
▪ Quick Sort is not stable, which means it may not preserve the relative order of equal elements.
Which algorithm would Obama pick?
Source: http://www.youtube.com/watch?v=k4RRi_ntQc8
ThankYou!

More Related Content

PPTX
DSA- Merge Sort-a sorting technique.pptx
PPTX
Data structure Merge Sort implementation
PPTX
Merge Sort with real example using animation
PDF
advanced algo
PPT
MergeSort algoritmos de complegidada (1).ppt
PPT
MergeSort.ppt algorithm History and Examples
PPT
MergeSort.ppt algorithm History and Examples
PPT
MergeSort_DATA_strucute_dsa_algorithm.ppt
DSA- Merge Sort-a sorting technique.pptx
Data structure Merge Sort implementation
Merge Sort with real example using animation
advanced algo
MergeSort algoritmos de complegidada (1).ppt
MergeSort.ppt algorithm History and Examples
MergeSort.ppt algorithm History and Examples
MergeSort_DATA_strucute_dsa_algorithm.ppt

Similar to Algorithms Analysis & Design - Lecture 9 (20)

PPT
MergeSortfbsjbjsfk sdfik k
PPTX
Module 2_ Divide and Conquer Approach.pptx
PPT
MergeSort.ppt shows how merge sort is done
PPTX
Data Structure Marge sort Group 5 pptx so
PPTX
Merge sort and quick sort
PDF
Analysis and design of algorithms part2
PPTX
Presentation merge sort.pptx
PPTX
Lecture -16-merge sort (slides).pptx
PPTX
Merge sort analysis and its real time applications
PPTX
Algorithm - Mergesort & Quicksort
PPTX
merg sort slide created by anas sanan.pptx
PPTX
ADA_Module 2_MN.pptx Analysis and Design of Algorithms
PPTX
Merj sort
PPTX
Merge sort
PPTX
quick and merge.pptx
PPTX
presentation.pptx
PPTX
MergeSort presentation dgdfgdfgdfgg.pptx
PPTX
Quick Sort in data structure.pptx
PPTX
Chapter 4.2 - ADTree_Divide_n_Conquer 2021
PDF
Algorithms lecture 3
MergeSortfbsjbjsfk sdfik k
Module 2_ Divide and Conquer Approach.pptx
MergeSort.ppt shows how merge sort is done
Data Structure Marge sort Group 5 pptx so
Merge sort and quick sort
Analysis and design of algorithms part2
Presentation merge sort.pptx
Lecture -16-merge sort (slides).pptx
Merge sort analysis and its real time applications
Algorithm - Mergesort & Quicksort
merg sort slide created by anas sanan.pptx
ADA_Module 2_MN.pptx Analysis and Design of Algorithms
Merj sort
Merge sort
quick and merge.pptx
presentation.pptx
MergeSort presentation dgdfgdfgdfgg.pptx
Quick Sort in data structure.pptx
Chapter 4.2 - ADTree_Divide_n_Conquer 2021
Algorithms lecture 3
Ad

More from Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt (20)

PDF
PDF
PDF
PDF
Understanding Convolutional Neural Networks (CNN)
PDF
PDF
Complier Design - Operations on Languages, RE, Finite Automata
PDF
Object Oriented Programming (OOP) using C++ - Lecture 5
PDF
Object Oriented Programming (OOP) using C++ - Lecture 2
PDF
Object Oriented Programming (OOP) using C++ - Lecture 1
PDF
Object Oriented Programming (OOP) using C++ - Lecture 3
PDF
Object Oriented Programming (OOP) using C++ - Lecture 4
Understanding Convolutional Neural Networks (CNN)
Complier Design - Operations on Languages, RE, Finite Automata
Object Oriented Programming (OOP) using C++ - Lecture 5
Object Oriented Programming (OOP) using C++ - Lecture 2
Object Oriented Programming (OOP) using C++ - Lecture 1
Object Oriented Programming (OOP) using C++ - Lecture 3
Object Oriented Programming (OOP) using C++ - Lecture 4
Ad

Recently uploaded (20)

PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Digital Strategies for Manufacturing Companies
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
medical staffing services at VALiNTRY
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Transform Your Business with a Software ERP System
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Essential Infomation Tech presentation.pptx
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
AI in Product Development-omnex systems
Upgrade and Innovation Strategies for SAP ERP Customers
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
How Creative Agencies Leverage Project Management Software.pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
How to Migrate SBCGlobal Email to Yahoo Easily
Digital Strategies for Manufacturing Companies
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
medical staffing services at VALiNTRY
Operating system designcfffgfgggggggvggggggggg
Transform Your Business with a Software ERP System
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Which alternative to Crystal Reports is best for small or large businesses.pdf
Essential Infomation Tech presentation.pptx
Adobe Illustrator 28.6 Crack My Vision of Vector Design
CHAPTER 2 - PM Management and IT Context
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
wealthsignaloriginal-com-DS-text-... (1).pdf
AI in Product Development-omnex systems

Algorithms Analysis & Design - Lecture 9

  • 1. Algorithms By Mohamed Gamal © Mohamed Gamal 2024
  • 4. Merge Sort Algorithm – Merge Sort uses the divide-and-conquer approach to break the problem into smaller sub-problems, solve each sub-problem recursively, and then combine the results. – Merge Sort is a comparison-based sorting algorithm that repeatedly compares pairs of elements to determine their order. – Merge Sort is typically implemented using recursion, although it can also be implemented iteratively.
  • 5. Merge Sort Algorithm 1) Divide the array into two halves. 2) Recursively apply Merge Sort to each half. 3) Merge the two sorted halves back together: a) Compare the elements of the two halves and arrange them in order. b) Continue merging elements until all elements from both halves are merged into a single sorted array. 4) Repeat the process until the entire array is sorted. 2 8 5 3 9 4 1 7
  • 10. Complexity: ▪ Worst Case: 𝑂(𝑛 log 𝑛) ▪ Average Case: 𝑂(𝑛 log 𝑛) ▪ Best Case: 𝑂(𝑛 log 𝑛) Advantages: ▪ Merge Sort is well-suited for sorting large datasets that do not fit into memory (external sorting) because it accesses data sequentially and has good performance with large datasets. ▪ Merge Sort is a stable sort, meaning that it preserves the relative order of equal elements in the sorted array. Disadvantages: ▪ Merge Sort is not in-place sorting algorithm since it requires additional space proportional to the size of the array being sorted.
  • 11. Let 𝐶(𝑛) be the number of comparisons made by the merge-sort algorithm. Recurrence relation equation: 𝐶 𝑛 = 2𝐶 𝑛 2 + 𝑛 and 𝐶 1 = 0 • Using backward substitution: 𝐶(𝑛) = 2𝐶 𝑛 2 + 𝑛 = 2 2𝐶 𝑛 4 + 𝑛 2 + 𝑛 = 4𝐶 𝑛 4 + 2𝑛 = 4 2𝐶 𝑛 8 + 𝑛 4 + 2𝑛 = 8𝐶 𝑛 8 + 3𝑛 𝐶 𝑛 = 2𝑖 𝐶 𝑛 2𝑖 + 𝑖 𝑛 = 2log2 𝑛 𝐶 𝑛 2log2 𝑛 + log2 𝑛 𝑛 = 𝑛 log2 𝑛 𝑛 2𝑖 = 1 2𝑖 = 𝑛 𝑖 = log2 𝑛 Basic Operation n represents the number of comparisons required to merge the two sorted halves
  • 13. Quick Sort Algorithm – Quick Sort uses the divide-and-conquer approach to break the problem into smaller sub-problems, sort each sub-problem recursively, and then combine the results. – Quick Sort is typically implemented using recursion, although it can also be implemented iteratively. – The choice of the pivot element is crucial for the performance of Quick Sort. Good pivot selection strategies can significantly improve its efficiency.
  • 15. Note: Pivots shown in bold.
  • 17. Complexity: ▪ Worst Case: 𝑂(𝑛2) – sorted array! ▪ Average Case: 𝑂(𝑛 log 𝑛) – random arrays ▪ Best Case: 𝑂 𝑛 log 𝑛 – split in the middle Advantages: ▪ Quick Sort is in-place sorting algorithm, meaning it requires only a small, constant amount of additional memory space. ▪ Despite its worst-case scenario, Quick Sort is often faster in practice compared to other 𝑂 𝑛 log 𝑛 algorithms like Merge Sort due to its good cache performance and low overhead. Disadvantages: ▪ The worst-case time complexity is 𝑂(𝑛2), which occurs when the pivot selection consistently results in highly unbalanced partitions. ▪ Quick Sort is not stable, which means it may not preserve the relative order of equal elements.
  • 18. Which algorithm would Obama pick? Source: http://www.youtube.com/watch?v=k4RRi_ntQc8