Searching, Sorting, Comparison Model and Lower Bound
Searching, Sorting, Comparison Model and Lower Bound
sorting
-
>
-
comparison
&
model lower
bound
search
Binary :
we must
figure out if an
integerBarchnum
is in this list.
If it is
,
we should return an indew I such
, ,
sorted
Since the list in
,
we
may
we the
following
method to
search for the value
.
left< left and of the list
and of the list
right I right
initially ,
left = o
, right =
n -1
let middle =
(left-right)/2
If list [middle) with
we
compare
rearchnum we obtain one of three resulte :
,
searchnerm is
present ,
it must be in the
positions
between middle + 1 and n-1 So we set left
.
to middle #1 .
left = o
,
right = n - 1 ;
binaryearch (searchnum ,
left , right
(2) searchnum = lieffmiddle) In this care , we
return middle .
searchnerm is
present ,
it must be in the
positions
between middle + 1 and n-1 So we set left
.
to middle 1 -
binaryearch (searchnum ,
left , right
3
If left
I right
return -
1
List o .
-n]
T() = T((27) + 1
TG) E T(2() + 1 + 1
T() = T(22) + 2
i
i
T() [ T
(i) +
T(n) = T() + i
i 1
=
i T() =
logn + 1
n = 2
=
Ologn)
i
logn count cale
o
Algorithm design paradigm
Divide and Conquer
- -
problem size : n
subproblems :
1
M
---
E
UK I M
n ,
na .
...,
T() = T( ) +. T() . -
+ T()
+ O()
Y T( , ) ,
+ (2) .
-
-
Tx)
No lue smaller nub problems
Drmall
individually enough-base
S case
> Combine
- the solutions into rolu for n
Binarysearch
-
!
comparison
Conquer I one smaller
unsproblem of
size *
combine : trivial /no action
enorrance : T() =
T(ET) +
TG) =
O(logn)
-
Mergelorf
ok time
-
Divide .
trivial ,
Conquer -
T((2) +(2)
,
Combine : O() ,
merging
proces -
Quick sort
-
: ↑
and
Divide : pivoting positioning
OG) time
>
require
T((c) TC
Conques !
,
trivial
combine :
,
/action &
[7 , , )
=
-main
the
&
problem) can be viewed as a tree of
and their outcomes
all
possible comparisons ,
and resulting answer
,
for any particular n
.
lower bound
searching
-
-
-
h
preprocessed items
finding a
given item
among
them in
comparison
model
require (logn) in
worst care
Proof :
binarysearch for n = 3
Search key
,
= X ,
11
0 1 2
⑫ ?
My -
YES
⑫? X ?
M
/ (YES
Exactly
-
NO
#cost
I11 I . .
OI Z
X?
I
diy ~ YES
. : .
Eco XA[]
,
- . .
En-B
X] <
-
-
n
possible outcome
= height of the
binary tree log ,
%
]n-1
I
when internal moder
%%
all the
levels of
the free % 66/2 % n leaves
are full /
# moder = 2n -1
I
Leight ollogn)
=
otherwise &
Leight =
log (2n-1) logn
Leight logn
Hence
, any comparisonbard search algorithm on a
sorted rize
of a
requires atleast. - logn) comparisons
in the wors case . #
tree
Decision
-
orithm
&
internal node->
binary decision
(comparison)
leaf >
-
found answer