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

01 CS251 Ch2 Getting Started

This document discusses the merge sort algorithm. It begins with an outline of topics to be covered, including insertion sort, analyzing algorithm complexity, and the divide and conquer approach used in merge sort. It then provides details on the insertion sort algorithm, analyzing its time complexity, and describing asymptotic notation. The document explains the divide and conquer technique and provides pseudocode for the merge sort algorithm. It analyzes the time complexity of merge sort using a recurrence relation, showing its runtime is O(n log n).

Uploaded by

Ahmad Alaraby
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

01 CS251 Ch2 Getting Started

This document discusses the merge sort algorithm. It begins with an outline of topics to be covered, including insertion sort, analyzing algorithm complexity, and the divide and conquer approach used in merge sort. It then provides details on the insertion sort algorithm, analyzing its time complexity, and describing asymptotic notation. The document explains the divide and conquer technique and provides pseudocode for the merge sort algorithm. It analyzes the time complexity of merge sort using a recurrence relation, showing its runtime is O(n log n).

Uploaded by

Ahmad Alaraby
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

CS251 – Algorithms

Ch2 - Getting Started

Instructor : Ameera jaradat


Introduction to Algorithm –MIT
Outline
 Insertion sort
 Analyzing algorithms – complexity of insertion sort
 Designing algorithms – divide and conquer – merge sort
Chapter objectives
 Start using frameworks for describing and analyzing
algorithms.
 Examine two algorithms for sorting: insertion sort and
merge sort.
 Learn how to prove the correctness of an algorithm.
 Begin using asymptotic notation to express running‐time
 analysis.
 Learn the technique of “divide and conquer” in the
context of merge sort.
Algorithm
 Algorithm: a well‐defined computational procedure that
takes some value as input and produces some value as output.
 Major concerns:
 Correctness
 Time complexity
For example: The sorting problem
 Input: A sequence of n numbers <a1, a2, . . . , an>.
 Output: A permutation <a1, a2, . . . , an> of the input
sequence
 such that a1 ≤ a2 ≤ . . . ≤ an .
 Given the input sequence 31, 41, 59, 26, 41, 58, a sorting
 algorithm returns as output the sequence 26, 31, 41, 41, 58, 59.
INSERTION‐SORT
 Insertion sort: an efficient algorithm for sorting a small
number of elements.
 INSERTION‐SORT(A)
1. for j ← 2 to length[A]
2. do key ← A[j]
3. /* Insert A[j] into the sorted sequence A[1… j − 1]*/
4. i ← j −1
5. while i > 0 and A[i] > key
6. do A[i + 1] ← A[i]
7. i ← i −1
8. A[i + 1] ← key
Time complexity of INSERTION‐SORT
Time complexity of INSERTION‐SORT
 Best‐case: The array is already sorted.
 A linear function of n.

 Worst‐case: The array is in reverse sorted order.


 A quadratic function of n.
Worst‐case and average‐case analysis
 We shall usually concentrate on finding only the worst‐
case.
 The worst‐case running time gives us a guarantee that the
 algorithm will never take any longer.
 For some algorithms, the worst case occurs fairly often.
 The "average case" is often roughly as bad as the worst
case.
 For example:
 Consider the insertion sort, on average, we check half of the
subarray A[1… j ‐ 1], so tj = j/2.
 The average‐case running time is still a quadratic function of n.
Order of growth
 Another abstraction to ease analysis and focus on the
important features.
 Look only at the leading term of the formula for running time.
 Drop lower‐order terms.
 Ignore the constant coefficient in the leading term.
 For example:
 The worst‐case running time of insertion sort is an2 + bn + c.
 Drop lower‐order terms ⇒ an2.
 Ignore constant coefficient ⇒ n2.
 We say that the running time is O(n2) to capture the notion that
the
 order of growth is n2.
Designing algorithms
There are many ways to design algorithms.
 Incremental:
 For example of insertion sort, having sorted subarray
A[1…j −1] and then yielding the sorted array A[1…j].
 Divide and conquer
 Divide the problem into a number of subproblems.
 Conquer the subproblems by solving them recursively.
 If the subproblems sizes are small enough, just solve them in a
straightforward manner.
 Combine the subproblem solutions to give a solution to the
original problem.
Merge sort
 Divide by splitting into two subarrays A[p…q] and A[q+1…r ], where q is
the halfway point of A[p…r ].
 Conquer by recursively sorting the two subarrays A[p…q] and A[q+1…r ].
 Combine by merging the two sorted subsequences to produce the sorted
answer.

MERGE‐SORT (A, p, r)
1. if p < r //Check for base case
2. then q ← [( p + r) / 2] //Divide
3. MERGE‐SORT(A, p, q) //Conquer
4. MERGE‐SORT(A, q+1, r) //Conquer
5. MERGE(A, p, q, r) //Combine

 Initial call  MERGE‐SORT(A, 1, n)


An example for MERGE‐SORT
Linear‐time merging
Analyzing divide‐and‐conquer algorithms
 Use a recurrence equation to describe the running time of
a divide‐and‐conquer algorithm.

 T (n) = the running time on a problem of size n.


 If n <= c for some constant c, the solution takes O(1) time.
 We divide into a subproblems, each (1/b) the size of the original.
 D(n) = the time to divide a size‐n problem.
 C(n) = the time to combine solutions.
Analyzing merge sort
 For simplicity, assume that n is a power of 2.

 The base case occurs when n = 1.


 Divide: compute the middle of the subarray, D(n) = (1).
 Conquer: Recursively solve 2 subproblems, each of size n/2
 ⇒ a =2 and b = 2.
 Combine: MERGE on an n‐element subarray takes O(n) time
 ⇒ C(n) = O(n).
Analyzing merge sort
 Let c be a constant that describes
 the running time for the base case
 the time per array element for the divide and combine steps.
 Then, we can rewrite the recurrence as

 The next slide shows successive expansions of the recurrence.


 level i : 2i nodes, each has a cost of c(n/2i).
 So, ith level has a cost of 2i c(n/2i) = cn.
 At the bottom level, a tree with h levels has 2h+1 = n nodes.
 Therefore, h = lgn + 1.
 The total cost is cn(lgn + 1) = cnlgn + cn = O(nlgn).

You might also like