Docsity

Dynamic Programming and Sorting Algorithms: Quicksort and Mergesort, Lecture notes of Biostatistics

Partial preview of the text

Download Dynamic Programming and Sorting Algorithms: Quicksort and Mergesort and more Lecture notes Biostatistics in PDF only on Docsity!

More Quicksort

Mergesort Biostatistics 615

Lecture 10

Scheduling … z^

I will hand out sample midterm next week

z^

Revision Q & A•

February 17

z^

Mid-term Exam•

February 19• Take Home

Problem 1 z

Using top-down dynamic programming,evaluate: z Must initialize matrix or results could bewrong

⎞ ⎟ ⎟ ⎠

⎛ ⎜⎜ ⎝

− −

⎞ ⎟⎟ ⎠

⎛^ ⎜⎜ ⎝

−

= ⎞ ⎟⎟ ⎠ ⎛ ⎜⎜ ⎝

(^11)

1

N^ k

k N

N k

Initializing Matrix in R z^

matrix(nrow = 10, ncol = 10)^ •

Creates a matrix with 10 rows and columns

z^

matrix(data = 0, nrow = 10, ncol = 10)^ •

The optional parameter allows the matrix tobe pre-initialized with an element of choice,which could even be a vector!

Problem 2 z

Using bottom-up dynamic programming,evaluate: z Calculation is still slow, due to nestedsum… but this can be simplified

⎧ ⎪⎪⎨ ⎪ ⎪⎩

≥ ≤

−

−

=

1

0

2

)

( ) 1 (

1

) (^

1

N^ N k N C k C N N N C

N k

Speedy Solution … int

comparisons

[Nmax

];

double

inner_sum

=

0.0;

comparisons

[^1

]^

=^

comparisons

[^0

]^

=^

0;

for

(

i^

=^

2 ;

i

<

Nmax

;^

i++)

{ inner_sum

+=

2

comparisons

[i

1

];

comparisons

[i

]^

=^

i^

+^

inner_sum

/

i;

}

Today z

Further improvements to Quick Sort

-^

Maintaining explicit stacks

z Merge Sort

-^

Another

N log N

sort

-^

Fastest stable sort

Quick Sort: The Idea z

Divide array into smaller sub-arrays

-^

Sort right and left sub-arrays independently

z Find a partitioning element such that:

-^

All elements to the right are greater

-^

All elements to the left are smaller

C Code: Partitioning int

partition

(Item

a

,^

int

start

,^

int

stop

{ int

up

start, down

stop

part

a

[stop

];

while

{ while

isLess

(a

[up

],

part

up

while

isLess

(part

,^

a[

down

])

up

down

down

if

up

down

)^

break;

Exchange

(a

[up

],

a

[down

]);

up

down

Exchange

(a

[up

],

a

[stop

]);

return

up;

}

Improvements We Considered z

Delaying sort for small sub-arrays z Using median-of-three partitioning

-^

Random partitioning element can also help!

z Avoiding deep recursion

The Problem z

The computer stack has a limited size z Quick Sort can call itself up to N-1 times

-^

Unlikely, but very deep recursion is possible!

z Can we provide a guarantee on depth ofrecursion?

The Solution z

After partitioning, handle smaller half first

-^

At most,

log

N

smaller halves!

z Keep track of sections to be solved in“explicit” stack

Explicit Stacks z

A common feature in computer programs z A simple way to avoid recursion

-^

More effort for the programmer

z Another application is in graph traversal

Quick Sort Summary z

Divide and Conquer Algorithm

-^

Recursive calls can be “hidden”

z Optimizations

-^

Choice of median

-^

Threshold for brute-force methods

-^

Limiting depth of recursion