Professional Documents
Culture Documents
2017-18 Q3
14 3 8 11 2 24 35 28 16 5 20 21
Max-Heap-Insert(A, key) 14
3. heap-size[A] = heap-size[A] + 1
4. A[heap-size[A] ] = -∞ 3 8
5. Increase-Key(A, heap-size[A], key)
11
Lower bound worst case running time?
A[i] moves up until it reaches
the correct position
Building a heap
Build-Max-Heap2(A)
1. heap-size[A] = 1
2. for i = 2 to A.length do Max-Heap-Insert(A, A[i])
Worst case running time: Θ(1) + ∑2≤ i ≤ n Ω(log i) = Ω(1 + ∑2≤ i ≤ n log i)
= Ω (n log n)
since ∑2≤ i ≤ n log i ≥ ∑n/2≤ i ≤ n log (n/2) = n/2 log (n/2)
Quiz
2. √n = Ω(log4 n) ? yes
3. 2log n = Ω(n2) ? no
4. 2n = Ω(n2) ? yes
InsertionSort: Θ(n2)
How do you show that a problem (for example sorting) can be solved in
Θ(f(n)) time?
➨ give an algorithm that solves the problem in Θ(f(n)) time.
Lower bound
How do you show that a problem (for example sorting) cannot be solved
faster than in Θ(f(n)) time?
➨ prove that every possible algorithm that solves the problem
needs Ω(f(n)) time.
Lower bounds
Lower bound
How do you show that a problem (for example sorting) can not be solved
faster than in Θ(f(n)) time?
➨ prove that every possible algorithm that solves the problem
needs Ω(f(n)) time.
Bit-manipulations?
Random-access (array indexing) vs. pointer-machines?
Comparison-based sorting
InsertionSort(A)
1. for j = 2 to A.length
2. do begin
3. key = A[ j ] ; i = j -1
4. while i > 0 and A[ i ] > key
5. do begin A[ i+1 ] = A[ i ]; i = i-1 end
6. A[ i+1 ] = key
7. end
exchange of elements,
assignments, etc. … or ≤, =, >, ≥
A[.] < A[.]
A[.] < A[.] A[.] < A[.] A[.] < A[.] A[.] < A[.]
Proving comparison-based lower bound
Easy approach
Count number of different inputs (requiring different outputs)
Every different input must correspond to a distinct leaf
Hard approach
Maintain set of possible inputs corresponding to comparisons
Show that at least two inputs remain after f(n) – 1 comparisons
Cannot choose comparisons, can choose results
Comparison-based sorting
every permutation of the input follows a different path in the decision tree
➨ the decision tree has at least n! leaves
Theorem
Any comparison-based sorting algorithm requires Ω(n log n) comparisons
in the worst case.
1. CountingSort
2. RadixSort
3. BucketSort
Main idea: count for every A[i] the number of elements less than A[i]
➨ position of A[i] in the output array
Beware of elements that have the same value!
5 3 10 5 4 5 7 7 9 3 10 8 5 3 3 8
3 3 3 3 4 5 5 5 5 7 7 8 8 9 10 10
numbers < 5
third 5 from left
position: (# less than 5) + 3
CountingSort
position(i) = number of elements less than A[i] in A[1..n]
+ number of elements equal to A[i] in A[1..i]
Lemma
If every element A[i] is placed on position(i), then the array is sorted and
the sorted order is stable.
Theorem
CountingSort is a stable sorting algorithm that sorts an array of n integers
in the range 0..k in Θ(n+k) time.
RadixSort
Input: array A[1..n] of numbers
Assumption: the input elements are integers with d digits
RadixSort(A, d)
1. for i = 1 to d
2. do use a stable sort to sort array A on digit i
RadixSort: example
329 720 720 329
457 355 329 355
657 436 436 436
839 457 839 457
436 657 355 657
720 329 457 720
355 839 657 839
Theorem
Given n d-digit numbers in which each digit can take up to k possible values,
RadixSort correctly sorts these numbers in
Θ(d (n + k)) time.
BucketSort
Input: array A[1..n] of numbers
Assumption: the input elements lie in the interval [0..1)
(no integers!)
1 0,792 0
2 0,1 1 0,1 0,15 0,13
0,287 0,287 0,256
0,15 0,346
0,346
0,734 0,5 0,53
0,5
0,13 0,792 0,734
0,256
n 0,53 n-1
BucketSort
Throw input elements in “buckets”, sort buckets, concatenate …
1 0,792 0
2 0,1 1 0,1 0,15 0,13 0,1 0,13 0,15
0,287 0,287 0,256 0,256 0,287
0,15 0,346 0,346
0,346
0,734 0,5 0,53 0,5 0,53
0,5
0,13 0,792 0,734 0,734 0,792
0,256
n 0,53 n-1
BucketSort
BucketSort(A)
► Input: array A[1..n] of numbers with 0 ≤ A[i] < 1
► Output: sorted list, which contains the elements of A
1. n = A.length
2. initialize auxiliary array B[0..n-1]; each B[i] is a linked list of numbers
3. for i = 1 to n
4. do insert A[i] into list B[ ⌊n∙A[i]⌋ ]
5. for i = 0 to n-1
6. do sort list B[i], for example with InsertionSort
7. concatenate the lists B[0], B[1], …, B[n-1] together in order
BucketSort
Running time?
worst case: all numbers fall into the same bucket ➨ Θ(n2)
(some math with indicator random variables – see book for details)
➨ E [ni2] = 2 - 1/n = Θ(1)
➨ expected running time = Θ(n)
Linear time sorting
Sorting in linear time
Only if assumptions hold!
CountingSort
Assumption: input elements are integers in the range 0 to k
Running time: Θ(n+k) ➨ Θ(n) if k = O(n)
RadixSort
Assumption: input elements are integers with d digits
Running time: Θ(d (n+k))
Can be Θ(n) for bounded integers with good choice of base
BucketSort
Assumption: input elements lie in the interval [0..1)
Expected Θ(n) for uniform input, not for worst case!