0% found this document useful (0 votes)
9 views9 pages

Chap 3 - Brute Force (Merging)

The document discusses the brute force technique for merging two sorted arrays into a single sorted array. It defines the merging problem, provides an example, and outlines the algorithm and its complexity analysis. The algorithm involves maintaining two pointers to compare elements from both arrays and append them to the result array accordingly.

Uploaded by

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

Chap 3 - Brute Force (Merging)

The document discusses the brute force technique for merging two sorted arrays into a single sorted array. It defines the merging problem, provides an example, and outlines the algorithm and its complexity analysis. The algorithm involves maintaining two pointers to compare elements from both arrays and append them to the result array accordingly.

Uploaded by

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

Chapter # 3

Lecture # 2

Brute Force Technique

Applications of Brute Force Technique:


(iii) Merging.

1 Dr. Hazem M. Bahig 03:44


Merging Problem
Definition
Problem Definition: Given two sorted arrays A=(a1, a2,…,
an) and B=(b1, b2,…, bm) of n and m elements respectively.
Merging the two sorted arrays is an array C=(c1, c2,…, cn+m)
of n+m elements such that:
(i)ci ∊ C belongs to A or B, ∀ 1 ≤ i ≤ n+m.
(ii)ai and bj appear exactly once in C, ∀ 1 ≤ i ≤ n and
1 ≤ j ≤ m.
Example
Example 1: Given A=(1,3,4,5,10) and B=(2,3,3,7,8)
Merge(A,B)=(1,2,3,3,3,4,5,7,8,10)
2 Dr. Hazem M. Bahig 03:44
Input A=(a1, a2,…, an) and B=(b1, b2,…, bm) of n and m elements respectively.

Output Sorted array C=(c1, c2,…, cn+m) of n+m elements such that:
(i)ci ∊ C belongs to A or B, ∀ 1 ≤ i ≤ n+m.
(ii)ai and bj appear exactly once in C, ∀ 1 ≤ i ≤ n and 1 ≤ j ≤ m.

3 Dr. Hazem M. Bahig 03:44


Main Idea

Main Idea: The main idea of merging algorithm as


follows:
We maintain two pointers p and q that initially point to a1
and b1 respectively.
In each step, we compare the elements ap and bq . If ap is
less than or equal bq then append ap to the array C at
position w. Then increment p and w by 1. Otherwise,
append bq to the array C at position w. Then increment q
and w by 1.
This process ends when p=n+1 or q=m+1. In case of
p=n+1, we append the remaining elements B(q..m) to
C(w..n+m).
4 In the second caseM. (q=m+1),
Dr. Hazem Bahig we append 03:44
A(p..n) to array C(w..n+m).
Main Idea

A B C
1 1 1
2 2 2

i
p
q j
n

w k

n+m

5 Dr. Hazem M. Bahig 03:44


Algorithm
Algorithm: Merging
Input: Two sorted arrays A=(a1, a2,…, an) and B=(b1, b2,…, bm) of n and m elements
respectively.
Output: Sorted array C=(c1, c2,…, cn+m) s.t. (i) ci ∊ C belongs to A or B, ∀ 1 ≤ i ≤ n+m.
(ii) ai and bj appear exactly once in C, ∀ 1 ≤ i ≤ n and 1 ≤ j ≤ m.
Begin
p=q=w=1 .1
While p ≤ n and q ≤ m do .2
if ap ≤ bq Then
cw = ap , p=p+1, w=w+1
else cw = bq , q=q+1, w=w+1
If p > n then C(cw, cw+1,…, cn+m)=B(bq, bq+1,…, bm) .3
if q> m then C(cw, cw+1,…, cn+m)=A(ap, ap+1,…, an)
.End

6 Dr. Hazem M. Bahig 03:44


Complexity Analysis
Running Time: Worst case
Algorithm: Merging
Input: Two sorted arrays A=(a1, a2,…, an) and B=(b1, b2,…, bm) of n and m elements respectively.
Output: Sorted array C=(c1, c2,…, cn+m) s.t. (i) ci ∊ C belongs to A or B, ∀ 1 ≤ i ≤ n+m. (ii) ai and bj
appear exactly once in C, ∀ 1 ≤ i ≤ n and 1 ≤ j ≤ m.
Begin
p=q=w=1 .1
While p ≤ n and q ≤ m do .2
if ap ≤ bq Then
cw = ap , p=p+1, w=w+1
else cw = bq , q=q+1, w=w+1
If p > n then C(cw, cw+1,…, cn+m)=B(bq, bq+1,…, bm) .3
if q> m then C(cw, cw+1,…, cn+m)=A(ap, ap+1,…, an)
.End
T(n)=t1+t2+t3= O(1) + O(n+m)+O(Max(n,m))
O(Max(1,n,m))=O(n)
7
if n≥m =
Dr. Hazem M. Bahig 03:44
Complexity Analysis
Storage
Algorithm: Merging
Input: Two sorted arrays A=(a1, a2,…, an) and B=(b1, b2,…, bm) of n and m elements respectively.
Output: Sorted array C=(c1, c2,…, cn+m) s.t. (i) ci ∊ C belongs to A or B, ∀ 1 ≤ i ≤ n+m. (ii) ai and bj
appear exactly once in C, ∀ 1 ≤ i ≤ n and 1 ≤ j ≤ m.
Begin
p=q=w=1 .1
While p ≤ n and q ≤ m do .2
if ap ≤ bq Then
cw = ap , p=p+1, w=w+1
else cw = bq , q=q+1, w=w+1
If p > n then C(cw, cw+1,…, cn+m)=B(bq, bq+1,…, bm) .3
if q> m then C(cw, cw+1,…, cn+m)=A(ap, ap+1,…, an)
.End
Storage: St(n)=O(1). Since the number of auxiliary variables used by the
.algorithm is constant numbers
8 Dr. Hazem M. Bahig 03:44
Tracing Algorithm: Merging
Input: Two sorted arrays A=(a1, a2,…, an) and B=(b1, b2,…, bm) of n
and m elements respectively.
1 2 3 4 5 Output: Sorted array C=(c1, c2,…, cn+m) s.t. (i) ci ∊ C belongs to A or
B, ∀ 1 ≤ i ≤ n+m. (ii) ai and bj appear exactly once in C, ∀ 1 ≤ i
A 1 4 6 9 30 ≤ n and 1 ≤ j ≤ m.

1 2 3 Begin
p=q=w=1 .1
B 2 4 4 While p ≤ n and q ≤ m do .2
if ap ≤ bq Then
cw = ap , p=p+1, w=w+1
else cw = bq , q=q+1, w=w+1
If p > n then C(cw, cw+1,…, cn+m)=B(bq, bq+1,…, bm) .3
if q> m then C(cw, cw+1,…, cn+m)=A(ap, ap+1,…, an)
.End
p q w p≤ n q≤m ap ≤ bq
1 1 1 √ √ √ C=(1)
2 2 √ √ X C=(1,2)
2 3 √ √ √ C=(1,2,4)

3 4 √ √ X C=(1,2,4,4)
3 5 √ √ X C=(1,2,4,4,4)
94 6 √ X Dr. Hazem
Terminate whileM. Bahig 03:44

You might also like