comp3600/6466 –algorithms · •in-place sorting algorithm: re-arrange the elements inside the...

16
COMP3600/6466 – Algorithms More on Sorting Cont. [CLRS ch. 8] Hanna Kurniawati https://cs.anu.edu.au/courses/comp3600/ [email protected]

Upload: others

Post on 10-Oct-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMP3600/6466 –Algorithms · •In-place Sorting Algorithm: Re-arrange the elements inside the array, with only a constant number of input array being stored outside the array at

COMP3600/6466 – Algorithms More on Sorting Cont.

[CLRS ch. 8]

Hanna Kurniawati

https://cs.anu.edu.au/courses/comp3600/[email protected]

Page 2: COMP3600/6466 –Algorithms · •In-place Sorting Algorithm: Re-arrange the elements inside the array, with only a constant number of input array being stored outside the array at

TopicsüLower bound on worst case time

complexity of comparison-based sorting• Non-sorting-based sortingüCounting sort• Radix sort• Bucket sort

Page 3: COMP3600/6466 –Algorithms · •In-place Sorting Algorithm: Re-arrange the elements inside the array, with only a constant number of input array being stored outside the array at

Radix Sort• Sort d-digit numbers

• i=1 is the least significant digit –the last digit of the numbers • Usually, line 2 uses Counting Sort• Example:

• Complexity: Θ(𝑑(𝑛 + 𝑘)), assuming Counting Sort is used

325457657839431

Input431325457657839

i=1325431839457657

i=2325431457657839

i=3

Page 4: COMP3600/6466 –Algorithms · •In-place Sorting Algorithm: Re-arrange the elements inside the array, with only a constant number of input array being stored outside the array at

TopicsüLower bound on worst case time

complexity of comparison-based sorting• Non-sorting-based sortingüCounting sortüRadix sort• Bucket sort

Page 5: COMP3600/6466 –Algorithms · •In-place Sorting Algorithm: Re-arrange the elements inside the array, with only a constant number of input array being stored outside the array at

Bucket Sort • Assumption: Sorting numbers in the interval [0,1)

that are drawn independently from a uniform distribution • The idea is to transform the problem of sorting n

elements into n problems of sorting k elements, where k is much smaller than n. • It starts by assigning similar elements to the same

bucket, and then sort the elements within the bucket

Page 6: COMP3600/6466 –Algorithms · •In-place Sorting Algorithm: Re-arrange the elements inside the array, with only a constant number of input array being stored outside the array at

Bucket Sort

Page 7: COMP3600/6466 –Algorithms · •In-place Sorting Algorithm: Re-arrange the elements inside the array, with only a constant number of input array being stored outside the array at

Bucket Sort: Illustration

0.780.170.390.260.720.940.210.120.230.68

123456789

10

A0123456789

B

0.78

0.17

0.390.26

0.72

Page 8: COMP3600/6466 –Algorithms · •In-place Sorting Algorithm: Re-arrange the elements inside the array, with only a constant number of input array being stored outside the array at

Bucket Sort: Illustration

Page 9: COMP3600/6466 –Algorithms · •In-place Sorting Algorithm: Re-arrange the elements inside the array, with only a constant number of input array being stored outside the array at

Bucket Sort: Running-time• 𝑇 𝑛 = Θ 𝑛 + ∑()*+,-𝑂(𝑛(.)

• Average case:𝐸[𝑇 𝑛 ] = 𝐸[Θ 𝑛 + ∑()*+,-𝑂 𝑛(. ]

Using linearity of expectation, we have𝐸[𝑇 𝑛 ] = 𝐸[Θ 𝑛 ] + ∑()*+,-𝐸[𝑂 𝑛(. ]𝐸[𝑇 𝑛 ] = Θ 𝑛 + ∑()*+,-𝑂 𝐸[𝑛(

.]

Cost of Insertion Sort in bucket-𝑖

Page 10: COMP3600/6466 –Algorithms · •In-place Sorting Algorithm: Re-arrange the elements inside the array, with only a constant number of input array being stored outside the array at

Bucket Sort: Illustration Running-time• Now, we need to compute 𝐸[𝑛!" ] where 𝑛! is the

expected number of elements in bucket- 𝑖• Use Indicator Random Variable

• 𝑋!,# = 𝐼 𝐴 𝑗 𝑖𝑛 𝑏𝑢𝑐𝑘𝑒𝑡 𝑖 = :1 𝐴 𝑗 𝑖𝑛 𝑏𝑢𝑐𝑘𝑒𝑡 𝑖0 𝑂𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

• Since the input are independent and uniformly distributed samples, 𝑃(𝑋!,# = 1) = $

%

Page 11: COMP3600/6466 –Algorithms · •In-place Sorting Algorithm: Re-arrange the elements inside the array, with only a constant number of input array being stored outside the array at

Bucket Sort: Running-time• Now, we can compute: 𝐸[𝑛!

" ] = 𝐸 ∑#$%& 𝑋!,#"

= 𝐸 ∑#$%& ∑($%& 𝑋!,#𝑋!,(= 𝐸 ∑#$%& 𝑋!,#" +∑#$%& ∑%)()&,(*#𝑋!,#𝑋!,(= ∑#$%& 𝐸[𝑋!,#" ] + ∑#$%& ∑%)()&,(*#𝐸[𝑋!,#𝑋!,(]

𝐸 𝑋!,#" = 1". %&+ 0" 1 − %

&= %

&

𝐸 𝑋!,#𝑋!,( = 𝐸 𝑋!,# 𝐸 𝑋!,( = %&%&= %

&&

Page 12: COMP3600/6466 –Algorithms · •In-place Sorting Algorithm: Re-arrange the elements inside the array, with only a constant number of input array being stored outside the array at

Bucket Sort: Running-time• Now, we can plug the expected values back to

𝐸[𝑛!" ] = ∑#$%& 𝐸[𝑋!,#" ] + ∑#$%& ∑%)()&,(*#𝐸[𝑋!,#𝑋!,(]

= ∑#$%& %&+∑#$%& ∑%)()&,(*#

%&&

= 𝑛 %&+ 𝑛 𝑛 − 1 %

&&= 1 + (&,%)

&= 2 − %

&

• Returning to 𝑇 𝑛 :𝑇 𝑛 = Θ 𝑛 +∑!$.&,%𝑂(𝑛!")

= Θ 𝑛 + 𝑛.𝑂 2 − %&

= Θ 𝑛

Page 13: COMP3600/6466 –Algorithms · •In-place Sorting Algorithm: Re-arrange the elements inside the array, with only a constant number of input array being stored outside the array at

Notes-1• Comparison-based methods can be used to sort any

type of data, as long as there’s a comparison function• Its running-time complexity is asymptotically lower bounded

by 𝑛 log 𝑛 --that is, Ω(𝑛 log 𝑛), where n is the input size• Non comparison-based can run in linear time. But, it

assumes the data to be sorted is of a certain type.• Tips: More efficient algorithms can be developed by

focusing on a sub-class of a problem (in the sorting case, a particular data type of the input).

Page 14: COMP3600/6466 –Algorithms · •In-place Sorting Algorithm: Re-arrange the elements inside the array, with only a constant number of input array being stored outside the array at

Notes-2• All the non-comparison based methods we

discussed are not in-place, which means it takes more memory• In-place Sorting Algorithm: Re-arrange the elements inside

the array, with only a constant number of input array being stored outside the array at any given time

• There are comparison-based algorithms that are not in-place (e.g., merge sort whose worst case run-time is Θ(𝑛 log 𝑛)• Insertion Sort, Bubble Sort, and Rand Quick Sort are in-

place. But, the worst case run-time of all these in-place algorithms are Θ(𝑛')

Page 15: COMP3600/6466 –Algorithms · •In-place Sorting Algorithm: Re-arrange the elements inside the array, with only a constant number of input array being stored outside the array at

Notes-2• Tips: Pay attention to Speed vs Memory tradeoff• Usually we can make an algorithm faster by using more

memory and vice versa• Tips: Randomization sometimes help speed-up

while keeping memory requirement low• Example: Rand Quick Sort allows average case running-

time to be Θ(𝑛 log 𝑛) and in practice tend to be close to this average case rather than the worst case Θ(𝑛')

Page 16: COMP3600/6466 –Algorithms · •In-place Sorting Algorithm: Re-arrange the elements inside the array, with only a constant number of input array being stored outside the array at

TopicsüLower bound on worst case time

complexity of comparison-based sortingüNon-sorting-based sortingüCounting sortüRadix sortüBucket sort

Next: Binary Search Tree