Quicksorter V2

20
The best programmers I’ve meet knew how to sort a deck of cards pretty d*mn well Tony Tran

description

 

Transcript of Quicksorter V2

Page 1: Quicksorter V2

The b

est

pro

gra

mm

ers

I’ve m

eet

knew

how

to

sort

a d

eck o

f card

s

pre

tty d

*mn

well

Tony T

ran

Page 2: Quicksorter V2

Sto

ry T

ime ☺

Page 3: Quicksorter V2

Ima

ge

so

urc

e:

http

://w

ww

.eye

bro

wm

aga

zin

e.c

om

/__da

ta/a

sse

ts/im

age

/001

0/1

00

45

/me

gan_

fox_na

ked_5

.jp

g

Page 4: Quicksorter V2

Wh

at

if

•G

iven

:D

eck o

f pla

yin

g c

ard

s

•G

oal:

To r

etr

ieve a

ny c

ard

as fast as

possib

le!

•C

on

str

ain

ts:

No

real constr

ain

ts,

do w

ha

tever

you

want

with the

card

s! Ju

st

do

n’t

be a

sm

art

@s*!

WH

AT

WO

UL

D Y

OU

DO

!?!

Page 5: Quicksorter V2

Fin

d m

e:

Appro

aches:

1. D

o n

oth

ing to the d

eck (

Cost: 5

2 lookups)

Co

st

is a

naly

zed

in

wo

rst

case s

cen

ari

o.

"An

yth

ing t

hat

can g

o w

rong w

ill g

o w

ron

g.“

–M

urp

hy’s

La

w

Page 6: Quicksorter V2

Appro

aches:

1.

Do n

oth

ing t

o t

he d

eck (

Cost:

52 lookups)

2.

Gro

up c

ard

s b

y s

uits (

Cost:

4+

12 =

16 looku

ps)

3.

Gro

up c

ard

s b

y f

aces (

Cost:

12+

4 =

16 lookups)

Page 7: Quicksorter V2

Image s

ourc

e:

htt

p:/

/ww

w.f

lickr.

com

/photo

s/6

0601

862@

N00

/94247

611

6/

Page 8: Quicksorter V2

Wh

at

if

•G

iven

:99 n

ote

card

s w

ith a

uniq

ue

num

ber

written o

n e

ach c

ard

.

•G

oal:

To r

etr

ieve a

ny c

ard

as fast as

possib

le!

•C

on

str

ain

ts:

No

real constr

ain

ts,

do w

ha

tever

you

want

with the

card

s! Ju

st

do

n’t

be a

sm

art

@s*!

WH

AT

WO

UL

D Y

OU

DO

!?!

Page 9: Quicksorter V2

Fin

d m

e:

Appro

aches:

1.

Do n

oth

ing to the d

eck (

Cost: 9

9 lookups)

2.

Cre

ate

bin

s for

gro

ups o

f 100 a

nd just th

row

the c

ard

s into

the b

in.

Exam

ple

:

Co

st

is a

naly

zed

in

wo

rst

case s

cen

ari

o.

"An

yth

ing t

hat

can g

o w

rong w

ill g

o w

ron

g.“

–M

urp

hy’s

La

w

bin

s

(Cost:

99 looku

ps)

When g

ive

n c

ard

s:

1,2

,3,D

,99

Page 10: Quicksorter V2

Fin

d m

e:

Appro

aches:

1.

Do n

oth

ing to the d

eck (

Cost: 9

9 lookups)

2.

Cre

ate

bin

s for

gro

ups o

f 100 a

nd just th

row

the c

ard

s into

the b

in. (C

ost: 9

9 lookups)

3.

So

rt t

he

de

ck!

Th

en

pe

rfo

rm b

ina

ry s

ea

rch

to

fin

d

ca

rd.

(co

st:

lo

g2(9

9)

loo

ku

ps ~

= 7

lo

ok u

ps)

Co

st

is a

naly

zed

in

wo

rst

case s

cen

ari

o.

"An

yth

ing t

hat

can g

o w

rong w

ill g

o w

ron

g.“

–M

urp

hy’s

La

w

Dep

ictio

n o

f b

ina

ry s

earc

h (

do

es n

ot

sho

w e

ntire

sea

rch

)

Page 11: Quicksorter V2

Fin

d m

e:

Appro

aches:

1.

Do n

oth

ing to the d

eck (

Cost: 9

9 lookups)

2.

Cre

ate

bin

s for

gro

ups o

f 100 a

nd just th

row

the c

ard

s into

the b

in. (C

ost: 9

9 lookups)

3.

So

rt t

he

de

ck!

Th

en

pe

rfo

rm b

ina

ry s

ea

rch

to

fin

d

ca

rd.

(co

st:

lo

g2(9

9)

loo

ku

ps ~

= 7

lo

ok u

ps)

Co

st

is a

naly

zed

in

wo

rst

case s

cen

ari

o.

"An

yth

ing t

hat

can g

o w

rong w

ill g

o w

ron

g.“

–M

urp

hy’s

La

w

Dep

ictio

n o

f b

ina

ry s

earc

h (

do

es n

ot

sho

w e

ntire

sea

rch

)

Page 12: Quicksorter V2

Ho

w d

oes t

his

apply

to

pro

gra

mm

ing

?Abstraction!

Page 13: Quicksorter V2

Key p

oin

t #1 a

nd #

2:

1. B

ein

g a

ble

to a

bstr

act

a p

roble

m to

som

eth

ing less c

om

ple

x, or

more

fam

ilia

r,

will

make the p

roble

m e

asie

r!

Ima

ge

so

urc

e:

http

://w

ww

.scie

ntifica

me

rica

n.c

om

/med

ia/in

line

/3E

0F

91

60

-E7

F2

-99

DF

-35899

8A

A3

C1

A91

0F

_1

.jpg

2. A

bstr

acting a

pro

ble

m to a

pro

ble

m that

has a

lready b

een s

olv

ed is e

ven b

etter!

You d

on’t r

eally

have to d

o a

ny w

ork

!

Page 14: Quicksorter V2

•“t

he

pu

rpo

se

of

abstr

actio

n is notto

be

va

gu

e,

bu

t to

cre

ate

a n

ew

se

ma

ntic le

ve

l in

wh

ich

on

e

ca

n b

e a

bso

lute

ly p

recis

e.”

-E

dsg

er

W.

Dijk

str

a

Oth

er

fans o

f abstr

action

What

is t

he s

hort

est

path

fro

m 1

to a

ny o

ther

node

?

Page 15: Quicksorter V2

“[O

SP

F] com

pute

s t

he s

hort

est

path

tre

e f

or

each r

oute

usin

g a

meth

od b

ased o

n D

ijkstr

a's

alg

orith

m,

a s

hort

est

path

first

alg

ori

thm

.”

INT

ER

NE

T!!

!

Page 16: Quicksorter V2

Keypoin

t#3

3.

Th

ere

’s n

o s

uch t

hin

g a

s a

du

mb

pro

ble

m

Ima

ge

so

urc

e:

http

://s

fist.

co

m/a

tta

chm

en

ts/S

Fis

t_B

rock/p

an

ca

ke

s.jpg

Pro

ble

m:

Giv

en a

spatu

la a

nd a

dis

h o

f pan

cakes (

sa

y 6

for

no

w),

ho

w w

ould

yo

u f

lip t

he p

an

cakes s

uch t

ha

t th

e p

ancakes b

ecom

e s

ort

ed

with t

he larg

est

pancake

at

the b

ott

om

and s

malle

st

at

the t

op? H

ow

man

y s

teps w

ould

it

requ

ire?

Page 17: Quicksorter V2

FL

IP

Init

ial S

tate

:G

OA

L

Page 18: Quicksorter V2

Som

eone fa

mili

ar:

•B

ill G

ate

s&

Christo

s P

apadim

itriou,

Bounds F

or

Sort

ing B

y P

refix R

evers

al.

Discrete Mathematics, vol27, pp 4

7-5

7,

1979.

We r

efe

rred t

o b

oun

ds a

s “

Co

sts

SO

RT

ING

PA

NC

AK

ES

==

SO

RT

ING

BY

PR

EF

IX R

EV

ER

SA

L

Page 19: Quicksorter V2

Key P

oin

ts:

1. B

ein

g a

ble

to a

bstr

act

a p

roble

m to s

om

eth

ing less c

om

ple

x, or

more

fam

iliar,

will

make the p

roble

m e

asie

r!

2. A

bstr

acting a

pro

ble

m to a

pro

ble

m that has a

lre

ady b

een s

olv

ed is

even b

etter!

3. T

here

’s n

o s

uch thin

g a

s a

dum

b p

roble

m.

4. B

e c

rea

tive

an

d h

av

e f

un

wit

h p

rob

lem

s.

Yo

u h

ave

th

e a

bilit

y t

o c

reate

an

en

tire

wo

rld

at

yo

ur

fin

ge

rtip

s!

Page 20: Quicksorter V2

The b

est

pro

gra

mm

ers

I’ve m

eet

kne

w h

ow

to A

BS

TR

AC

T P

RO

BLE

MS

pre

tty d

*mn

we

ll

“The h

ard

est

part

of

solv

ing a

pro

ble

m is t

o c

om

ple

tely

und

ers

tand it

to

a p

oin

t w

here

you c

an s

ee it

from

man

y d

iffe

rent

pers

pective

s a

nd a

bstr

actions”

-A v

ery

wis

e T

.A.

-TH

AN

K Y

OU

-

New

tit

le p

rop

osal:

P.S

. M

y g

fw

ill p

rob

ab

ly b

e r

ead

ing

th

is,

so

th

e s

tory

at

the b

eg

inn

ing

did

n’t

reall

y h

ap

pen

0-=

) B

ut

reall

y,

it d

idn

’t ☺ ☺☺☺

Thank y

ou,

-Tony T

ran

@quic

ksort

er