Week 2
Week 2
Analysis
Ms. Malak Abdullah Alsabban
Department of Computer Engineering, College of Computer and information
Science, Majmaah University, Majmaah, KSA
2
The Sorting
Problem
The Sorting Problem
Expected
Output: The permutation of the input [26, 31, 41, 41, 58 , 59].
3
4
Insertion Sort
is an efficient algorithm for sorting a small number of
elements.
Example: like sorting a hand of playing cards
– Start with an empty left hand and the cards facing down on
the table.
– Remove one card at a time from the table, and insert it into
the correct position in the left hand
• compare it with each of the cards already in the hand, from right
to left
– The cards held in the left hand are sorted
• these cards were originally the top cards of the pile on the table
Insertion Sort
5
Insertion Sort
6
Insertion Sort
7
Insertion Sort
8
11
Pseudocode
Is an informal high-level description of the program or
other algorithm.
In pseudocode, we employ whatever expressive
method is most clear and concise to specify a given
algorithm.
Pseudocode is not concerned with issues of software
engineering. Issues of data abstraction, modularity,
and error handling are often ignored in order to convey
the essence of the algorithm more concisely.
pseudocode for insertion sort
procedure parameter
12
Proving Loop Invariants
13
Loop Invariant for Insertion Sort
Initialization:
– Just before the first iteration, j = 2:
the subarray A[1 . . j-1] = A[1], (the
element originally in A[1]) – is sorted
14
Loop Invariant for Insertion Sort
Maintenance:
– the while inner loop moves A[j -1], A[j -2], A[j -3], and so
on, by one position to the right until the proper position for
key (which has the value that started out in A[j]) is found
– At that point, the value of key is placed into this position.
15
Loop Invariant for Insertion Sort
Termination:
– The outer for loop ends when j = n + 1 j-1 = n
– Replace n with j-1 in the loop invariant:
• the subarray A[1 . . n] consists of the elements originally in
A[1 . . n], but in sorted order
j-1 j
T (n) c1n c2 (n 1) c4 (n 1) c5 t j c6 t j 1 c7 t j 1 c8 (n 1)
n n n
j 2 j 2 j 2
19
Best Case Analysis
The array is already sorted “while i > 0 and A[i] > key”
– A[i] ≤ key upon the first time the while loop test is run (when
i = j -1)
– tj = 1
20
Worst Case Analysis
The array is in reverse sorted order“while i > 0 and A[i] > key”
– Always A[i] > key in while loop test
– Have to compare key with all elements to the left of the j-th
position compare with j-1 elements tj = j
n
n(n 1) n
n(n 1) n
n(n 1)
using
j 1
j
2
j
j 2 2
1 ( j 1)
j 2 2
we have:
an 2 bn c a quadratic function of n
24
Designing Algorithm
Merge Sort
– Design approach: divide and conquer
– Sorts in place: No
– Running time: Let’s see!!
Divide-and-Conquer Approach
26
Merge Sort
Alg.: MERGE-SORT(A, p, r) 5 2 4 7 1 3 2 6
MERGE-SORT(A, p, q) Conquer
MERGE-SORT(A, q + 1, r) Conquer
MERGE(A, p, q, r) Combine
28
Example – n Power of 2
1 2 3 4 5 6 7 8
Divide 5 2 4 7 1 3 2 6 q=4
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
29
Example – n Power of 2
1 2 3 4 5 6 7 8
Conquer 1 2 2 3 4 5 6 7
and
Merge 1 2 3 4 5 6 7 8
2 4 5 7 1 2 3 6
1 2 3 4 5 6 7 8
2 5 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
30
Example – n Not a Power of 2
1 2 3 4 5 6 7 8 9 10 11
4 7 2 6 1 4 7 3 5 2 6 q=6
Divide
1 2 3 4 5 6 7 8 9 10 11
q=3 4 7 2 6 1 4 7 3 5 2 6 q=9
1 2 3 4 5 6 7 8 9 10 11
4 7 2 6 1 4 7 3 5 2 6
1 2 3 4 5 6 7 8 9 10 11
4 7 2 6 1 4 7 3 5 2 6
1 2 4 5 7 8
4 7 6 1 7 3
31
Example – n Not a Power of 2
1 2 3 4 5 6 7 8 9 10 11
Conquer 1 2 2 3 4 4 5 6 6 7 7
and
1 2 3 4 5 6 7 8 9 10 11
Merge 1 2 4 4 6 7 2 3 5 6 7
1 2 3 4 5 6 7 8 9 10 11
2 4 7 1 4 6 3 5 7 2 6
1 2 3 4 5 6 7 8 9 10 11
4 7 2 1 6 4 3 7 5 2 6
1 2 4 5 7 8
4 7 6 1 7 3
32
Merging
p q r
1 2 3 4 5 6 7 8
2 4 5 7 1 2 3 6
33
Merging
p q r
Idea for merging: 1 2 3 4 5 6 7 8
2 4 5 7 1 2 3 6
– Two piles of sorted cards
• Choose the smaller of the two top cards
• Remove it and place it in the output pile
– Repeat the process until one pile is empty
– Take the remaining input pile and place it face-down onto
the output pile
A1 A[p, q]
A[p, r]
A2 A[q+1, r]
34
Example: MERGE(A, 9, 12, 16)
p q r
35
Example: MERGE(A, 9, 12, 16)
36
Example (cont.)
37
Example (cont.)
38
Example (cont.)
Done!
39
Merge - Pseudocode
p q r
1 2 3 4 5 6 7 8
2 4 5 7 1 2 3 6
n1 n2
p q
L 2 4 5 7
q+1 r
R 1 2 3 6
40
41
Loop invariant of merge sorting algorithm
Running Time of Merge (assume last for loop)
42
Analyzing Divide-and Conquer Algorithms
(1) if n ≤ c
T(n) = aT(n/b) + D(n) + C(n) otherwise
43
MERGE-SORT Running Time
Divide:
– just computes the middle of the subarray, which takes
constant time. compute q as the average of p and r:
D(n) = (1)
Conquer:
– recursively solve 2 subproblems, each of size n/2
2T (n/2)
Combine:
– MERGE on an n-element subarray takes (n) time
C(n) = (n)
T(n) = c if n = 1
2T(n/2) + cn if n > 1
Advantages:
– Guaranteed to run in (nlgn)
Disadvantage
– Requires extra space N
47
Readings
Chapter 2
48