0% found this document useful (0 votes)
13 views12 pages

Searching, Sorting, Comparison Model and Lower Bound

Uploaded by

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

Searching, Sorting, Comparison Model and Lower Bound

Uploaded by

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

searching

sorting
-

>
-

comparison
&
model lower
bound
search
Binary :

that have distinct


Assume we n> I integers that

sorted and stored


are
already in the
array
list .

That is list[o] < list[1] ...


- List [n-I
, .

we must
figure out if an
integerBarchnum
is in this list.

If it is
,
we should return an indew I such
, ,

that list [i] = searchnum .

If rearchneem is not present ,


we should return-p.

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 :
,

(1) searchnum - lint[middle]


searchnem
If is
prevent ,
then it must be in
the
positions between o and middle-I
&
not
=> we
right to middle-1 .

search num list middle)


(2)
Inthiscanhea
=
.

(3) searchnum > list[middle] .


In this care
,
it

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 ;

while (left right)


& middle =
(left + right)/2
(1) searchnum - list[middle]
searchnem
If is
prevent ,
then it must be in
the
positions between and middle-1
o
,
not
=> we
right to middle-1 I

binaryearch (searchnum ,
left , right
(2) searchnum = lieffmiddle) In this care , we
return middle .

(3) searchnum > list[middle] .


In this care
,
it

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()

Divide break-the into smaller


:
problem
nuspaoblems
Conquer
.

Y T( , ) ,
+ (2) .
-
-
Tx)
No lue smaller nub problems
Drmall
individually enough-base
S case

> Combine
- the solutions into rolu for n
Binarysearch
-
!

Divide " O(1) one

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 -

Recurrence : T(n) = 2T() + n

Exercis : solve this recurrance


-

Quick sort
-
: ↑

and
Divide : pivoting positioning
OG) time
>

require

T((c) TC
Conques !
,

trivial
combine :
,
/action &

Recurrend : TG) = 2T(1/2) + n


.
model
Comparison
- -
:

- all input items are block boo (ADTr)


-

only operations allowed are


comparisons [S ,

[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

For n30 allume n in some of 2


.
any , power

I11 I . .

OI Z
X?
I
diy ~ YES

. : .

Eco XA[]
,
- . .
En-B
X] <
-

-
n
possible outcome

decision tree and must have


-
in
binary In leave ,

one for each anwer


.

= 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

root-to-leaf-> algorithm execution


time
path length- > running
of tree worst free
height -> case
running

You might also like