[go: up one dir, main page]

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

AAL5 Heap Quick Sort

The document contains algorithms for sorting and partitioning, including Quicksort and Selection-Sort, along with their complexities. It discusses both deterministic and randomized approaches to partitioning. Additionally, it presents recurrence relations for analyzing the performance of these algorithms.

Uploaded by

engineeringengtr
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 views17 pages

AAL5 Heap Quick Sort

The document contains algorithms for sorting and partitioning, including Quicksort and Selection-Sort, along with their complexities. It discusses both deterministic and randomized approaches to partitioning. Additionally, it presents recurrence relations for analyzing the performance of these algorithms.

Uploaded by

engineeringengtr
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/ 17

!

"
#!
$% & ' (
%
% % !
)( " *
" +( !+ !
!
, +
+ %+
- &(.

. %
% +
- (./ % 0
- / % 0

)( " *
1 (
2 2
" ( 3 + ! Ω/ 0
+
"
4 & &+!++
5 6 ( 2 Θ/ 708 6%

5 6 ( 2 Θ/ 0&
Θ/ 0 + !! 8

#! "

4 +

9 % ! ! +
% . :/ 03
% . :/ 7 0
#! "

; 6 6 <!
% ( + 2
% ( % (
( 2
/ ! 07

$ /=0
> % % !
#! " /=0
2 (

$ /70
> % % !
Partition(A,p,r) i i j j
01 x←A[r] =? =7 @ =A 7B C D =E
02 i←p-1
03 j←r+1 ≤ FG=E ≤ i j
04 while TRUE
05 repeat j←j-1
=E =7 @ =A 7B C D =?
06 until A[j] ≤x
i j
07 repeat i←i+1
08 until A[i] ≥x =E D @ =A 7B C =7 =?
09 if i<j
10 then exchange A[i]↔A[j]
j i
11 else return j
=E D @ C 7B =A =7 =?
#! " /70
2 (
Quicksort(A,p,r)
01 if p<r
02 then q←Partition(A,p,r)
03 Quicksort(A,p,q)
04 Quicksort(A,q+1,r)

#!
2 ++ . !!
+!
9 2 % ( +
H
$ + 2
+ T (n) = 2T (n / 2) + Θ(n)

5 H
$ ( + +
%
T (n) = T (1) + T (n − 1) + Θ(n)
= T (n − 1) + Θ(n)
n
= Θ( k )
k =1
n
= Θ( k)
k =1

= Θ( n 2 )
5 H /70

5 H /B0
2 !! 3 !2
!
2
2
5 !! !
! . %
2 ( +
!
H ;!! !
- /2 6 ! 0 L (n) = 2U ( n / 2) + Θ( n) lucky
/2 6
U (n) = L(n − 1) + Θ(n) unlucky
! ! 0 !!
we consequently get
+ 2
L (n) = 2( L( n / 2 − 1) + Θ(n / 2)) + Θ( n)
= 2 L( n / 2 − 1) + Θ ( n)
n Θ(n) = Θ( n log n)

n Θ( n)
1 n-1

(n-1)/2 (n-1)/2 (n-1)/2+1 (n-1)/2

H ;!! !
2 !! (
: / I
70 + % *
J ( + + %
/% ( 20

J
9 2 2 +
3 6 !! ! ( + 2+ 2 !
3 6 !! !
. +
J #!
. ! ! ! +!

J (
+
+ /= 6=&7 67& & 6= =0
!! =I 2
%
J ( 6
!! ! ! 2

J #! /70
Randomized-Partition(A,p,r)
01 i←Random(p,r)
02 exchange A[r] ↔A[i]
03 return Partition(A,p,r)

Randomized-Quicksort(A,p,r)
01 if p<r then
02 q←Randomized-Partition(A,p,r)
03 Randomized-Quicksort(A,p,q)
04 Randomized-Quicksort(A,q+1,r)
" "
Selection-Sort(A[1..n]):
For i → n downto 2
A: Find the largest element among A[1..i]
B: Exchange it with A[i]

Θ/ 0 Θ/=0
% % Θ/ 70
( 2

%"
+ + % %
;
H % + +
- 2 (
!!
J (
' +
!+ ' + % %
%"
/0
! I
7
!/ 0
! 7
" /0
! 7 L=
%
= 7 B K D @ ? C A =E
M$ / 0N ≥ M N =@ =D =E C ? A B 7 K =
> B 7 = E

%.
& +
>./ 0 J /0 ' !+ '
% %
- MN ' (
! %
# $! % ! MN 2
! 2 % !2!!
%. /70

%. O
%. 9 2 "
%. ( 2 '
!
% + ! !+ !+ (
- 2 +! Θ/=0
- !! + !+ . 7 I B
' % !! (
2 ! .

T (n) ≤ T (2n / 3) + Θ(1) T (n) = O(log n)

%P %
M= N G M N +
%' 2
M/ I
7 L =0 N
% !! !
%
%

%P %
H Q
'! ' %
2
J! I
7 %. ( 2
:/ 0
%P %
R
' % !!
'
!

%. G :/ '
! ! !+ / 00
G7 S = !/ % + G 0

R/ 0 G :/ 0

%"

O(n)

% % ( 2
:/ 0L ! 6 %/ 0
! 6 % :/ 0 T
%
"

. O

#! % % . 2 2
+ % $ 2
!2 + !2!!
- 2
( % +!! + &3
( $ HU
+ %

You might also like