Sie sind auf Seite 1von 14



JON

E


2M

"




&

"

The Sorting Problem


+











'







Internal Sorting








I

JLT W
K
M
M
KM
JLT V
K
JLT U

PQT U

!
%






"

%










 












 





#$

!
!

 
!


"






 





"

5!

"




+




%





(






&




 



&

&

&


,-











5

3
0



/10
.

6 23

)
4


)




%
(


<
,




'

&






%

 6





!

%




 




A
@ 

> 
4


?






 


"

















1/ 0
B


)





3











" 












%



!





'



 












'




/10;


:3

 798
/10
 .


%

%

! 6





]^








'

"


"






"

Z[
Y

 

%

@








%







"

&

%




"

 


"





_

 
&





 














'

/10
.

"




 

"

I

'

"


23

PQT V
X

X MM

!



 











&

!

R 


'



H I








'









PQT W
X


"


!





 
%

 

&




"



+

(

K
M




M
P 2 KM
P 7OK






PQN






"

 






 

C

!

"



3





J MK
J 7LK


%
S
K

&




*\




&

!

"

!

"on





Insertion Sort





n
%





!





k



I

0


'


h

!



m







{
v

y|
y

yz

uv

-



st


"
*

x
ew

!



qr
q

t e
v
f

e
ef

g

xv

~g e g

g ef
v

| ef

xv
e ~f
y
xv
ew y
x

s{

"

}~



ef



f
g

s|
g



!%


 




%

o
t
{
v
v
{

|
xw

&



q
q

v
v


_
 

%


v

]^

{
v

*\
)

{
~

!




Comparing Performance of Sorting


Algorithms


"








!





%

"


!

!

%



&I

a






%


!




"



%

&

!

_

%
I

%




 
"

"



+

%
  
 c !


 "

'  
%
   
"
 I %

 '


  




"
& 

!%  
 
 c 

 " 


  
b
&


 
%
"



&





%


!





%
 
 


 !
R 







&

!















!

&

!%









!



0




!

"



!%

!%

-






&

!%


!


"



%

 










 
 

!

"

!



 

g
e f


  !%
d



& 


&


_




 

%




]^
X3

 



!

!

\



h
i h

)
*

*\

%
6
&
&

 


Bubble Sort

!


R




!



!%



ef







I





-

g
ef

g
xv
~
|
u
u
u

ew

!

 



!



&


"
*

t
v










f
g



l 

'

&







"

 




&






e

e
o t

{
{t vv
v {
f v
{

f t
{

v
{ t {v

e g v
v
g e f { {

ef
y ~
e

xv
y
xv
y



_
 

%


6
]^

*\
)
*

R

]
L

L
^

^
L


_

 

%


Insertion Sort

`






!

,-

!

L
`

p
m

]
m\

`
m


`\

m^

 !



]^


>








I





*\




 






Selection Sort

!








!



R





'





b


I













 

!

!





" h
%









l 

k



!%



e f

f
g

&


 




"


+



"





&

!

"
*

t
v

ef
g e
xv

~
xf
e e
g
 g
| ef

|
~ xv
ew y
x

|
ef
x
w


_


v
v
{

|
w t
ef {
v
x v
{

 

%

{t
g v
v
ef {
y
e
g
xv
ef y
|
ef
x

|
w
ef
e x

{t
v
v
{

6
]^

*\
)
*

{
~

]
L

L
^

]
L


_

 

%


Bubble Sort


!

,-




!

L
`

p
m

]
m\

`
m


`\

m^

 !



]^


>








I





*\

Keeping Swap Costs Low













!



_



"







!






,-

R 

R 

6
]^

 

%



"




_





 








 
I

"



G





!









!




Name = Jones, Bob


ID = 3232323399
(etc.)

&










Name = Jones, Bob


ID = 3232323399
(etc.)

Name = Smith, John


ID = 9922922299
(etc.)

Name = Smith, John


ID = 9922922299
(etc.)

%



*\
)
*





]
L

L
^


_

 

%


Selection Sort


!

,-




!

L
`

p
m

]
m\

`
m


`\

m^

 !



]^


>








I





*\

!


"


'

!

Shellsort



&

 h










"




R


"

3
&

 

 
&
5 /


"






!

 

 
 


 C
 $









,-


R

 


 




 







 h



I


"

"


%


4




I

k


&

xv

&





-






6
]^

*\
)
*



/10
B








!


!%


"




I


23


&







/10






!%

3 m



/10
.




&











!%



0






 

%

{t

v t
v {
xv { v
v
y
{





_



v
{



-




e
g
ef




!%



I









"





"o


&

&





ef

g
xv
ef y



!



"


J



ew





"


 

s|

 



!

/ K

ef
g
xv
~



"
*

!

 h


-

%




"



" !
"





&



!

!


!




&

  
&







"

"

"



'




!





!





!









Exchange Sorting

I



 


ef



&

"



&

f
g

!



"



+



!

 

0



"




&


/








"




&

 


(


"







!

 0 
 




 /


m
m


_
3



!










]^



,-

)



 

%

*\



&
%



 I


"

"

&



Quicksort




"
>

" I


Shellsort

>

&


&



&

 I



 
I





"








7
4

I

%


"



%


'

2
4

%


"

"

/

3"

&

"











%


6
]^


m




2h

&







 

%







_










%

*\



)
*

m\



_

 ^
^ p 
]


 m 
 m




/10;





!

,-

]^
)


\ 
`\

 

%



 2 3
)



&

m 
p 







?



h
 


 
*





 
I



 m^
` 
] 
m\



2
"





`




&

!




%


m

:3
798
/10
.

&




&

!


I






'



"

"





!

 



" I












"

" I


&
"




!

!%
h



"


&

*\

Quicksort




Quicksort




!

{
g

"
*

v
xv
~

g
s
e

g
s
e

v
g
y
|

t
{
v
v
{
g
x
e

v
v

g
s
e

v
g

y
|

ef
g

ef

g t
s {
v
e v
t
{ g
{
s
v
v
g xf
v w x e e
{ ef
g
y e e v g
g y
g

v
ef
|
g {t { {t
g x
v v
v
xv e v
{ {

g
g
{
{ y
ef
e
ew e f
x
~
~

g
y
|

{

{
g

v
v

xv v

~ y
e

{
g

v
v

{
g

g
ef


g
ef

xv

ef

g
ef

e f

g
x
e
e

ef

g
s
e

ef

v
g
|
v

g
e
g
{


%

*\

)
*

I
R

x
g
w
ef

t
v
v

{
v

m^

g
x
e

xf

e
f

g
x
e

6
]^



I
R



 

%




_

e
t












g
y
|

g
ef

ef

g
s
e

%


"




&

 I

k





"



%

t
{
v
v v
{

{t

v v
v
{
|
e
s {
~


_


v v

t
{
v
| v
e {
s
{

 

%

]^

v
g
|
v

)
*

*\



"








 

!






Mergesort









!




"




a




ef

g g
e~ e~

g ef

xv s
g
~|
f
v |
|

g y
e
e~

!



"

s{
g

g
e~

f
v
g
|
v

Cost of Quicksort



"
&




1/ 0
.





>




3


23




/10
.



d














RI




c
!

k


1/ 0
.

 I

%


&

`\
`

m^
`\

`
m

m\
m

v
g
|
v



p

!

,-

m
m^

\
\

p
`
m\

*\
)
*

]
m

6
]^


]

 

%

m\



_

m
m\

%
]
m
!




_

!





 

%











 !
,




m^

&

`\








'



g
e~



g
e~

`\

|

| |

m^
v

xf

ef




e ~v

&





e~

~|

ef

y
x
g
y
xv

s{ ~
|

ef

xv

g
e~

g
e~



"
>







!%



"



!







"


"

"
 



"


%

]^
*

*\


%

h


"

Heapsort




"



















!

!






{
g|
|

0


"

>

w

*


60

42

6
57
42

 

%

73

{











*\
)
*

-

!

"
*



_

e
t
v


e
g
ef

xv

 

%

f
ef f

g t
{
xv v
v
~ {
{
s|
{
ew s |
x
g

6
]^

72
83
85

73

60
48
6

72

57

48

48

83
88

88

85

60

ef




_

"

"
 %
&

f
g







Array contents

88






73

60

 %
&




>

72

\]


"

88




42

73

] 

Tree (not a heap)

Heapsort Example

Array contents

83

85

p 

72

42

48

57

` 

Heap


\

85

83

` 
]

57

{
g|
|

|
w

]^

)
*

*\






%




h

& 







!


 !



'



!



d



r

R 
4






!

I


 

%


*\
)

!
R



m
`


6

R

6
]^



48


_

42

 

%


60
85
6

Heap




88

60
73

"w

72

60

{
g|
|

72
83

88
48

83

57
42

Array contents

73



85


_



'

5!




& 




'

a

42
w

72

73

60

{
g|
|

!%


&

r

R 



!









 




!

"

"



0

 

-







88

xv

/10

+



48

83
85


Heap

Heapsort Example (cont.)

57



H I

"




&








f
e

]^

Array contents



"

!

e


%
!



48




88

!

42

Binsort

"

r








"

!

 


%

 6







85

"I



!%



72



&


57

"

73

<



83

57



)
*

*\





%








"

 c













Bucket Sort


& 


 







"

!



& 


,

!





R



Po



&

&

!%



h



<



I

!
,-



H I


I
%



"




_
 

%




(





0








 











& 




(







(





> 


I



%







& 
p
m


&


!%




R

*\

6
]^

"

)
m


!




g
|
f

g~

e~

y
{
v

f q

Improved Binsort


"
*

ef

|
r
g {
ef t
|
g
xv {

e ~f
u g
e
ew ~
x

v
{

g
e~

x
t

r
w

g|
e
g

ef

xv

f
|

f
e~

f
|
e
|

t
|
| g

{
{ e ~v

g e

e |
~
e

g

x
e


xv
y
xv
y

|
{

`\





_

"

3

/10


&





*

]^




 

%


"

)
*

*\




& 











"














97
93

87
82

77

55
54

48
46

36

87

46

23

*\

"

p


3


!

&

!

/ 

> 

" 




> 

m
!

&










 
%

!

,-

\ 

!

 
` ^



\ 
 ^
^
] 
!
` 
,

"

& 
p
m

"

p
m

!



'

'
p

77
97
7

/ h



!

'

"




3p
m

"


_
 

%


!


p
m

b 


"

`^

!


3p


 h

48

36
26
6



!



& 


"





'


"

& 






'

55

54
4

] 

m^




93
3

3
82
2

15




& 
!

%

"

J


] 



!%


"


+

Radix Sort



&

&

&




`\

 "
 




2










m




"
h



23

15

26

Second pass (left digit)




 

%
]^



'

!
,-


_

First pass (right digit)

List after second pass: 3, 15, 23, 26, 36, 46, 54, 55, 77, 82, 87, 93, 97

Initial list: 26, 93, 3, 97, 15, 77, 23, 48, 82, 87, 55, 36, 54, 46

Radix Sort



'
/
"

]^
)
J


!

& 

*\

General Lower Bound for Sorting

/10
B


"
%












I








"

%


&


!







 




I


!

3
/10
B





&




 6




5

 




 



%


'


1/ 0
.


3
0



/10
B



 




A


&




@ 

?





5










b

!

Empirical Comparison









I

 %

!

I







d




R

"
&

3
"


%

!


)

!

 





!


5
!




I


3
1/ 0
B




]^




I

!


%

 

%

h ` h m h h
]

*\
)
*

m h m h h
p

p
p

\m
^

h `
^ \
p

h `\
^

^
`

`\

p
]

p
p

p 
m

p^

h ^
h
\
h h \ h
^
\ \
]
` p
m
\ `
^

h h h h p\ h p h
^ ^
k


 
' '
&
&
   
%   % %
> ) )

`
p h ] h h
`\



5

p h \p h \` h



=

" 


"

3



/

!%

"

"



%


I


 


"







_


 

%

"


'



















!





"




 




p






"

p
]


` h
p
^ m

!


&

"




 




"

&


_

p h
m



]h 

%  

 b  

!
 !



!



m\

 
"

"
/



m\



^ h ] h
]
p h
` h
^ ^
p
p
6 6 6
\
p











h ` h
p\ m



& 

!




/10 &
B
!%


I 
 

!


0 



 



"


&

&

I

]^

%
(




d

)
*

*\