Download presentation
Presentation is loading. Please wait.
Published byBetty Sybil Wood Modified over 9 years ago
1
CSS106 Introduction to Elementary Algorithms M.Sc Askar Satabaldiyev Lecture 05: MergeSort & QuickSort
3
“divide and conquer” algorithm Divide the problem into subproblems Conquer the subproblems by solving them recursively. If small - solve as base cases. Combine the solutions to the subproblems into the solution for the original problem.
4
…more recursive calls
5
MergeSort 1. Divide: find middle position q 2. Conquer: recursively sort two subarrays [p..q] and [q+1..r] 3. Combine: two sorted subarrays into single sorted array [p..r]
6
Linear-time merging
7
Analysis of Merge Sort Θ(n lgn) time Not “in place”
8
Quick Sort Divide: by choosing pivot between [p..r] This is called partitioning Conquer: by recursively sorting subarrays [p..q-1] and [q+1..r] A i <= pivot, at the left A i > pivot, at the right Combine: do nothing…
9
Partitioning (less or equal)
10
Partitioning (greater)
11
Example If A J <= A r, swap(A J, A q ) Increment j, q If A J > A r, Increment j
12
Analysis of Quick Sort Worst-case Best-case
13
Average case of Quick Sort
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.