This preview shows pages 1–2. Sign up to view the full content.
COMP 271: Design and Analysis of Algorithms
Fall 2007
Solution to Assignment 1
Idea of the algorithm
:
If we divide
A
into two roughly equal size subarrays, each with approximately
n/
2 elements, we observe that the maximum value of
A
[
j
]

A
[
i
] must be one
of the following:
1.
The maximum value of
A
[
j
]

A
[
i
] in
A
[1
..
b
n/
2
c
] where 1
≤
i
≤
j
≤ b
n/
2
c
.
2.
The maximum value of
A
[
j
]

A
[
i
] in
A
[
b
n/
2
c
+ 1
..n
] where
b
n/
2
c
+ 1
≤
i
≤
j
≤
n
.
3.
The maximum value of
A
[
j
]

A
[
i
] where
A
[
i
] is the minimum value of
A
[1
..
b
n/
2
c
] and
A
[
j
] is the maximum value of
A
[
b
n/
2
c
+ 1
..n
].
Details of algorithm
:
Input
:
A
is an array of positive integers;
p
is the start index of
A
;
r
is the
end index of
A
.
Output
: An integer array
[MaxAll, MinAll, ValAll]
where
MaxAll
and
MinAll
are the maximum and minimum value of the input array respectively.
ValAll
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview. Sign up
to
access the rest of the document.
 Spring '07
 ARYA
 Algorithms

Click to edit the document details