problem solvingcslabcms.nju.edu.cn/problem_solving/images/3/36/2019-2-9...2019/02/09 · tc problem...
TRANSCRIPT
Problem Solving2-9 Sorting and Selection
MA Jun
Institute of Computer Software
April 28, 2020
. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .
Contents
1 TC 7.2-2
2 TC 7.3-2
3 TC 7.4-2
4 TC Problem 7-5
5 TC 8.1-4
6 TC 8.1-4
7 TC 8.2-4
8 TC 8.3-4
9 TC Problem 8-2
10 TC 9.1-1
11 TC 9.3-7. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 7.2-2
TC 7.2-2What is the running time of QuickSort when all elements of array Ahave the same value?
T(n) = T(0) + T(n− 1) + cn = Θ(n2)
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 1 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 7.2-2
TC 7.2-2What is the running time of QuickSort when all elements of array Ahave the same value?
T(n) = T(0) + T(n− 1) + cn = Θ(n2)
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 1 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 7.3-2
TC 7.3-2When Randomized-QuickSort runs, (Q1) how many calls are madeto the random number generator Random in the worst case? (Q2) Howabout in the best case? Give your answer in terms of Θ notation.
Q1: X(n) = T(n− 1) + 1 = Θ(n)Q2: X(n) = T(⌊(n− 1)/2⌋) + T(⌈(n− 1)/2⌉) + 1 = Θ(n)
How can you prove that they are worst/best cases?
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 2 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 7.3-2
TC 7.3-2When Randomized-QuickSort runs, (Q1) how many calls are madeto the random number generator Random in the worst case? (Q2) Howabout in the best case? Give your answer in terms of Θ notation.
Q1: X(n) = T(n− 1) + 1 = Θ(n)Q2: X(n) = T(⌊(n− 1)/2⌋) + T(⌈(n− 1)/2⌉) + 1 = Θ(n)
How can you prove that they are worst/best cases?
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 2 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 7.3-2
TC 7.3-2When Randomized-QuickSort runs, (Q1) how many calls are madeto the random number generator Random in the worst case? (Q2) Howabout in the best case? Give your answer in terms of Θ notation.
Q1: X(n) = max0≤q≤n−1
(X(q) + X(n− q− 1)) + 1
Q2: X(n) = min0≤q≤n−1
(X(q) + X(n− q− 1)) + 1
Prove by Substitution!
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 3 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 7.3-2
TC 7.3-2When Randomized-QuickSort runs, (Q1) how many calls are madeto the random number generator Random in the worst case? (Q2) Howabout in the best case? Give your answer in terms of Θ notation.
Q1: X(n) = max0≤q≤n−1
(X(q) + X(n− q− 1)) + 1
Q2: X(n) = min0≤q≤n−1
(X(q) + X(n− q− 1)) + 1
Prove by Substitution!
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 3 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 7.4-2
TC 7.4-2Show that Quicksort’s best-case running time is Ω(n lg n).
What is the best-case for Quicksort?I Always partition in the half?I Can you prove it?
Assume T(n) is the best-case running time, then
T(n) = min0≤q≤n−1
(T(q) + T(n− q− 1)) + Θ(n)
Prove by Substitution!
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 4 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 7.4-2
TC 7.4-2Show that Quicksort’s best-case running time is Ω(n lg n).
What is the best-case for Quicksort?
I Always partition in the half?I Can you prove it?
Assume T(n) is the best-case running time, then
T(n) = min0≤q≤n−1
(T(q) + T(n− q− 1)) + Θ(n)
Prove by Substitution!
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 4 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 7.4-2
TC 7.4-2Show that Quicksort’s best-case running time is Ω(n lg n).
What is the best-case for Quicksort?I Always partition in the half?
I Can you prove it?Assume T(n) is the best-case running time, then
T(n) = min0≤q≤n−1
(T(q) + T(n− q− 1)) + Θ(n)
Prove by Substitution!
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 4 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 7.4-2
TC 7.4-2Show that Quicksort’s best-case running time is Ω(n lg n).
What is the best-case for Quicksort?I Always partition in the half?I Can you prove it?
Assume T(n) is the best-case running time, then
T(n) = min0≤q≤n−1
(T(q) + T(n− q− 1)) + Θ(n)
Prove by Substitution!
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 4 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 7.4-2
TC 7.4-2Show that Quicksort’s best-case running time is Ω(n lg n).
What is the best-case for Quicksort?I Always partition in the half?I Can you prove it?
Assume T(n) is the best-case running time, then
T(n) = min0≤q≤n−1
(T(q) + T(n− q− 1)) + Θ(n)
Prove by Substitution!
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 4 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 7.4-2
TC 7.4-2Show that Quicksort’s best-case running time is Ω(n lg n).
What is the best-case for Quicksort?I Always partition in the half?I Can you prove it?
Assume T(n) is the best-case running time, then
T(n) = min0≤q≤n−1
(T(q) + T(n− q− 1)) + Θ(n)
Prove by Substitution!
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 4 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 7-5
TC Problem 7-5: Median-of-3 partitionOne way to improve the Radomized-Quicksort procedure is topartition around a pivot that is chosen more carefully than by picking arandom element from the sub array.The median-of-3 method:Choose the pivot as the median (middle element) of a set of 3 elementsrandomly selected from the sub array.
For this problemAssume that the elements in the input array A [1 · · · n] are distinctand that n ≥ 3.We denote the sorted output array by A′ [1 · · · n].Using the median-of-3 method to choose the pivot element x, definepi = Prx = A′ [i].
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 5 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 7-5
Question a: Give an exact formula for pi as a function of n and i fori = 1, 2, · · · , n− 1. (Note that p1 = pn = 0)
Ans.Number of all possible 3-elements subsets,
(n3).
Number of 3-elements subsets with x = A′[i] as the median,(i− 1) · (n− i).So, pi = (i−1)(n−i)
(n3)
.
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 6 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 7-5
Question b: By what amount have we increased the likelihood ofchoosing the pivot as x = A′ [⌊(n + 1)/2⌋], the median ofA [1, 2, · · · , n], compared with the ordinary implementation? Assumethat n→∞, and give the limiting ratio of these probabilities
Ans.Original: qx = 1/nMedian-of-3: px = (⌊(n+1)/2⌋−1)(n−⌊(n+1)/2⌋)
(n3)
= (⌊(n−1)/2⌋)2
(n3)
.
limn→∞
pxqx
= 3/2
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 7 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 7-5
Question c: If we define a “good” split to mean choosing the pivotas x = A′ [i], where n/3 ≤ i ≤ 2n/3, by what amount have weincreased the likelihood of getting a good split compared with theordinary implementation?(Hint: Approximate the sum by an integral.)
Ans.
Original: Qx =2n/3∑i=n/3
1/n = (n + 3)/3n
Median-of-3: Px =2n/3∑i=n/3
(i−1)(n−i)(n
3).
limn→∞
Px = 13/27
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 8 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 7-5
Question d: Argue that in the Ω(n lg n) running time ofQuicksort, the median-of-3 method affects only the constantfactor.
Best-case of Quicksort:
T(n) = min0≤q≤n−1
(T(q) + T(n− q− 1)) + Θ(n) = Ω(n lg n)
Quicksort with Median-of-3-Partition also satisfies
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 9 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 7-5
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 10 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 8.1-4
TC 8.1-4 Sorting n/k subsequencesShow an Ω(n lg n) lower bound on the number of comparisons needed tosolve this variant of the sorting problem.
Hint: It is not rigorous to simply combine the lower bounds for theindividual subsequences.
WHY?Is that a BEST algorithm!
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 11 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 8.1-4
TC 8.1-4 Sorting n/k subsequencesShow an Ω(n lg n) lower bound on the number of comparisons needed tosolve this variant of the sorting problem.
Hint: It is not rigorous to simply combine the lower bounds for theindividual subsequences.
WHY?Is that a BEST algorithm!
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 11 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 8.1-4
TC 8.1-4 Sorting n/k subsequencesShow an Ω(n lg n) lower bound on the number of comparisons needed tosolve this variant of the sorting problem.
Hint: It is not rigorous to simply combine the lower bounds for theindividual subsequences.
WHY?Is that a BEST algorithm!
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 11 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 8.1-4
TC 8.1-4 Sorting n/k subsequencesShow an Ω(n lg n) lower bound on the number of comparisons needed tosolve this variant of the sorting problem.
Ans.
Totally (k!)n/k possible sequencesAssume that the height of the decision tree is h, then we have2h ≥ (k!)n/k
h ≥ lg (k!)n/kx = n/k · lg k! = Θ(n/k · k lg k) = Θ(n lg k)Finally, h = Ω(n lg k)
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 12 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 8.1-4
TC 8.1-4 Sorting n/k subsequencesShow an Ω(n lg n) lower bound on the number of comparisons needed tosolve this variant of the sorting problem.
Ans.Totally (k!)n/k possible sequences
Assume that the height of the decision tree is h, then we have2h ≥ (k!)n/k
h ≥ lg (k!)n/kx = n/k · lg k! = Θ(n/k · k lg k) = Θ(n lg k)Finally, h = Ω(n lg k)
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 12 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 8.1-4
TC 8.1-4 Sorting n/k subsequencesShow an Ω(n lg n) lower bound on the number of comparisons needed tosolve this variant of the sorting problem.
Ans.Totally (k!)n/k possible sequencesAssume that the height of the decision tree is h, then we have2h ≥ (k!)n/k
h ≥ lg (k!)n/kx = n/k · lg k! = Θ(n/k · k lg k) = Θ(n lg k)Finally, h = Ω(n lg k)
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 12 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 8.1-4
TC 8.1-4 Sorting n/k subsequencesShow an Ω(n lg n) lower bound on the number of comparisons needed tosolve this variant of the sorting problem.
Ans.Totally (k!)n/k possible sequencesAssume that the height of the decision tree is h, then we have2h ≥ (k!)n/k
h ≥ lg (k!)n/kx = n/k · lg k! = Θ(n/k · k lg k) = Θ(n lg k)
Finally, h = Ω(n lg k)
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 12 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 8.1-4
TC 8.1-4 Sorting n/k subsequencesShow an Ω(n lg n) lower bound on the number of comparisons needed tosolve this variant of the sorting problem.
Ans.Totally (k!)n/k possible sequencesAssume that the height of the decision tree is h, then we have2h ≥ (k!)n/k
h ≥ lg (k!)n/kx = n/k · lg k! = Θ(n/k · k lg k) = Θ(n lg k)Finally, h = Ω(n lg k)
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 12 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 8.2-4
TC 8.2-4Describe an algorithm that, given n integers in the range 0 to k,preprocesses its input and then answers any query about how many of then integers fall into a range [a..b] in O(1) time. Your algorithm should useO(n + k) preprocessing time.
Algorithm 1 PreProcess1: procedure PreProcess(A[1, .., n])2: let C [0, .., k + 1] be a new array3: for i = 0 to k + 1 do4: C [i]← 05: end for6: for j = 0 to A.length do7: C [A [j] + 1]← C [A [j] + 1] + 18: end for9: for i = 1 to k + 1 do
10: C [i]← C [i] + C [i− 1]11: end for12: end procedure
C [b + 1]− C [a]
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 13 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 8.2-4
TC 8.2-4Describe an algorithm that, given n integers in the range 0 to k,preprocesses its input and then answers any query about how many of then integers fall into a range [a..b] in O(1) time. Your algorithm should useO(n + k) preprocessing time.
Algorithm 2 PreProcess1: procedure PreProcess(A[1, .., n])2: let C [0, .., k + 1] be a new array3: for i = 0 to k + 1 do4: C [i]← 05: end for6: for j = 0 to A.length do7: C [A [j] + 1]← C [A [j] + 1] + 18: end for9: for i = 1 to k + 1 do
10: C [i]← C [i] + C [i− 1]11: end for12: end procedure
C [b + 1]− C [a]
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 13 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 8.2-4
TC 8.2-4Describe an algorithm that, given n integers in the range 0 to k,preprocesses its input and then answers any query about how many of then integers fall into a range [a..b] in O(1) time. Your algorithm should useO(n + k) preprocessing time.
Algorithm 3 PreProcess1: procedure PreProcess(A[1, .., n])2: let C [0, .., k + 1] be a new array3: for i = 0 to k + 1 do4: C [i]← 05: end for6: for j = 0 to A.length do7: C [A [j] + 1]← C [A [j] + 1] + 18: end for9: for i = 1 to k + 1 do
10: C [i]← C [i] + C [i− 1]11: end for12: end procedure
C [b + 1]− C [a]MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 13 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 8.3-4
TC 8.3-4Show how to sort n integers in the range 0 to n3 − 1 in O(n) time.
Lemma (8.3)Given n d-digit numbers in which each digit can take on up to k possiblevalues, Radix-Sort correctly sorts these numbers in Θ(d(n + k)) time ifthe stable sort it uses takes Θ(n + k) time.
Ans.let k = n, then d = 3Θ(d(n + k)) = Θ(3(n + n)) = Θ(n)
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 14 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear timeSuppose that we have an array of n data records to sort and that the keyof each record has the value 0 or 1. An algorithm for sorting such a set ofrecords might possess some subset of the following three desirablecharacteristics:
1 The algorithm runs in O(n) time.2 The algorithm is stable.3 The algorithm sorts in place, using no more than a constant
amount of storage space in addition to the original array.
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 15 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time1 The algorithm runs in O(n) time.2 The algorithm is stable.3 The algorithm sorts in place, using no more than a constant
amount of storage space in addition to the original array.
O(n) Stable In PlaceCounting-Sort X XRadix-Sort X XBucket-Sort X XInsertion-Sort X XBubble-Sort X XMerge-Sort X XHeap-Sort XQuick-SortPartition X X
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 16 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time1 The algorithm runs in O(n) time.2 The algorithm is stable.3 The algorithm sorts in place, using no more than a constant
amount of storage space in addition to the original array.
O(n) Stable In PlaceCounting-Sort X XRadix-Sort X XBucket-Sort X XInsertion-Sort X XBubble-Sort X XMerge-Sort X XHeap-Sort XQuick-Sort ?Partition X X
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 16 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time1 The algorithm runs in O(n) time.2 The algorithm is stable.3 The algorithm sorts in place, using no more than a constant
amount of storage space in addition to the original array.
O(n) Stable In PlaceCounting-Sort X XRadix-Sort X XBucket-Sort X XInsertion-Sort X XBubble-Sort X XMerge-Sort X XHeap-Sort XQuick-Sort 7
Partition X X
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 16 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question d: Can you use any of your sorting algorithms from parts(a)-(c) as the sorting method used in line 2 of Radix-Sort, so thatRadix-Sort sorts n records with b-bit keys in O(bn) time? Explain howor why not.
Counting-Sort
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 17 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question e: Suppose that the n records have keys in the range from 1to k. Show how to modify counting sort so that it sorts the records inplace in O(n + k) time. You may use O(k) storage outside the input array.Is your algorithm stable? (Hint: How would you do it for k = 3?)
Stable
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 18 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question e: Suppose that the n records have keys in the range from 1to k. Show how to modify counting sort so that it sorts the records inplace in O(n + k) time. You may use O(k) storage outside the input array.Is your algorithm stable? (Hint: How would you do it for k = 3?)
Stable
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 18 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
7
keys VS records
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 19 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
7
keys VS records
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 19 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
7
keys VS records
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 19 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?
Step-0: k← k = nStep-1: Select the k-th element and swap it with the element thattakes its correct position.I This step is repeated with the swapped element until we hit a element
that belongs in the k-th position.Step-2: k← k− 1, repeat Step-1 until k = 1.
Notice!We have to find a way to determine if an element at a certainposition has been moved there correctly already.If an element lies between the original starting position and theposition for the next element of a bin, it has been already touched.I Introduce a new array D = C to remember original starting positions.
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 20 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?Step-0: k← k = nStep-1: Select the k-th element and swap it with the element thattakes its correct position.I This step is repeated with the swapped element until we hit a element
that belongs in the k-th position.Step-2: k← k− 1, repeat Step-1 until k = 1.
Notice!We have to find a way to determine if an element at a certainposition has been moved there correctly already.If an element lies between the original starting position and theposition for the next element of a bin, it has been already touched.I Introduce a new array D = C to remember original starting positions.
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 20 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?Step-0: k← k = nStep-1: Select the k-th element and swap it with the element thattakes its correct position.I This step is repeated with the swapped element until we hit a element
that belongs in the k-th position.Step-2: k← k− 1, repeat Step-1 until k = 1.
Notice!We have to find a way to determine if an element at a certainposition has been moved there correctly already.If an element lies between the original starting position and theposition for the next element of a bin, it has been already touched.I Introduce a new array D = C to remember original starting positions.
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 20 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
C 2 2 4 7 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 21 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=8
1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
C 2 2 4 7 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
3
4 7
8 /∈ (4, 7], so A[8] is at a wrong position.
7
0 swap
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 22 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=8
1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
C 2 2 4 7 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
3
4 7
8 /∈ (4, 7], so A[8] is at a wrong position.
7
0 swap
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 22 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=8
1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
C 2 2 4 7 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
3
4 7
8 /∈ (4, 7], so A[8] is at a wrong position.
7
0 swap
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 22 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=8
1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
C 2 2 4 7 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
3
4 7
8 /∈ (4, 7], so A[8] is at a wrong position.
7
0 swap
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 22 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=8
1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
C 2 2 4 7 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
3
4 7
8 /∈ (4, 7], so A[8] is at a wrong position.
7
0
swap
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 22 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=8
1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
C 2 2 4 7 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
3
4 7
8 /∈ (4, 7], so A[8] is at a wrong position.
7
0 swap
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 22 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=8
1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
C 2 2 4 7 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
0
4 7
6
3
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 23 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=8
1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 3 0
0 1 2 3 4 5
C 2 2 4 6 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
0
2
8 /∈ (0, 2], so A[8] is at a wrong position.
2
5 swap
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 24 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=8
1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 3 0
0 1 2 3 4 5
C 2 2 4 6 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
0
2
8 /∈ (0, 2], so A[8] is at a wrong position.
2
5 swap
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 24 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=8
1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 3 0
0 1 2 3 4 5
C 2 2 4 6 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
0
2
8 /∈ (0, 2], so A[8] is at a wrong position.
2
5 swap
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 24 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=8
1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 3 0
0 1 2 3 4 5
C 2 2 4 6 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
0
2
8 /∈ (0, 2], so A[8] is at a wrong position.
2
5
swap
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 24 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=8
1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 3 0
0 1 2 3 4 5
C 2 2 4 6 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
0
2
8 /∈ (0, 2], so A[8] is at a wrong position.
2
5 swap
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 24 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=8
1 2 3 4 5 6 7 8
A 2 0 3 0 2 3 3 5
0 1 2 3 4 5
C 1 2 4 6 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
50
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 25 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=8
1 2 3 4 5 6 7 8
A 2 0 3 0 2 3 3 5
0 1 2 3 4 5
C 1 2 4 6 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
5
7 8
8 /∈ (7, 8], so A[8] is at a correct position.
8
3
8 = C[A[8]], first time! Decrease C[A[8]] by 1.
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 26 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=8
1 2 3 4 5 6 7 8
A 2 0 3 0 2 3 3 5
0 1 2 3 4 5
C 1 2 4 6 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
5
7 8
8 /∈ (7, 8], so A[8] is at a correct position.
8
3
8 = C[A[8]], first time! Decrease C[A[8]] by 1.
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 26 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=8
1 2 3 4 5 6 7 8
A 2 0 3 0 2 3 3 5
0 1 2 3 4 5
C 1 2 4 6 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
5
7 8
8 /∈ (7, 8], so A[8] is at a correct position.
8
3
8 = C[A[8]], first time! Decrease C[A[8]] by 1.
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 26 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=8
1 2 3 4 5 6 7 8
A 2 0 3 0 2 3 3 5
0 1 2 3 4 5
C 1 2 4 6 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
55
7
7 8
3
8 /∈ (7, 8], so A[8] is at a correct position.8 = C[A[8]], first time! Decrease C[A[8]] by 1.
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 27 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=7
1 2 3 4 5 6 7 8
A 2 0 3 0 2 3 3 5
0 1 2 3 4 5
C 1 2 4 6 7 7
0 1 2 3 4 5
D 2 2 4 7 7 8
3
4 7
7 ∈ (4, 7], so A[7] might be at a correct position.
6
7 > C[A[7]] = 6, so A[7] has been moved once (to a correct position).
3
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 28 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=7
1 2 3 4 5 6 7 8
A 2 0 3 0 2 3 3 5
0 1 2 3 4 5
C 1 2 4 6 7 7
0 1 2 3 4 5
D 2 2 4 7 7 8
3
4 7
7 ∈ (4, 7], so A[7] might be at a correct position.
6
7 > C[A[7]] = 6, so A[7] has been moved once (to a correct position).
3
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 28 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=7
1 2 3 4 5 6 7 8
A 2 0 3 0 2 3 3 5
0 1 2 3 4 5
C 1 2 4 6 7 7
0 1 2 3 4 5
D 2 2 4 7 7 8
3
4 7
7 ∈ (4, 7], so A[7] might be at a correct position.
6
7 > C[A[7]] = 6, so A[7] has been moved once (to a correct position).
3
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 28 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=7
1 2 3 4 5 6 7 8
A 2 0 3 0 2 3 3 5
0 1 2 3 4 5
C 1 2 4 6 7 7
0 1 2 3 4 5
D 2 2 4 7 7 8
3
4 7
7 ∈ (4, 7], so A[7] might be at a correct position.
6
7 > C[A[7]] = 6, so A[7] has been moved once (to a correct position).
3
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 28 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=6
1 2 3 4 5 6 7 8
A 2 0 3 0 2 3 3 5
0 1 2 3 4 5
C 1 2 4 6 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
3
4 7
6 ∈ (4, 7], so A[6] might be at a correct position.
3
6
6 = C[A[6]], first time! Decrease C[A[6]] by 1.
5
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 29 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=6
1 2 3 4 5 6 7 8
A 2 0 3 0 2 3 3 5
0 1 2 3 4 5
C 1 2 4 6 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
3
4 7
6 ∈ (4, 7], so A[6] might be at a correct position.
3
6
6 = C[A[6]], first time! Decrease C[A[6]] by 1.
5
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 29 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=6
1 2 3 4 5 6 7 8
A 2 0 3 0 2 3 3 5
0 1 2 3 4 5
C 1 2 4 6 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
3
4 7
6 ∈ (4, 7], so A[6] might be at a correct position.
3
6
6 = C[A[6]], first time! Decrease C[A[6]] by 1.
5
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 29 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=6
1 2 3 4 5 6 7 8
A 2 0 3 0 2 3 3 5
0 1 2 3 4 5
C 1 2 4 6 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
3
4 7
6 ∈ (4, 7], so A[6] might be at a correct position.
3
6
6 = C[A[6]], first time! Decrease C[A[6]] by 1.
5
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 29 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC Problem 8-2
TC Problem 8-2: Sorting in place in linear time
Question : How to perform the final step in-place?k=6
1 2 3 4 5 6 7 8
A 2 0 3 0 2 3 3 5
0 1 2 3 4 5
C 1 2 4 6 7 8
0 1 2 3 4 5
D 2 2 4 7 7 8
3
4 7
6 ∈ (4, 7], so A[6] might be at a correct position.
3
6
6 = C[A[6]], first time! Decrease C[A[6]] by 1.
5
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 29 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 9.1-1
TC 9.1-1Show that the second smallest of n elements can be found withn + ⌈lg n⌉ − 2 comparisons in the worst case.(Hint: Also find the smallest element.)
Basic ideaI Round-1: Find the smallest element ex in A: n− 1 comparisonsI Round-2: Find the smallest element within
L = y|y is compared greater than ex:|L| − 1 comparisonsI Finally, (n− 1) + (|L| − 1)
lower bound of |L|?
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 30 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 9.1-1
TC 9.1-1Show that the second smallest of n elements can be found withn + ⌈lg n⌉ − 2 comparisons in the worst case.(Hint: Also find the smallest element.)
Basic idea
I Round-1: Find the smallest element ex in A: n− 1 comparisonsI Round-2: Find the smallest element within
L = y|y is compared greater than ex:|L| − 1 comparisonsI Finally, (n− 1) + (|L| − 1)
lower bound of |L|?
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 30 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 9.1-1
TC 9.1-1Show that the second smallest of n elements can be found withn + ⌈lg n⌉ − 2 comparisons in the worst case.(Hint: Also find the smallest element.)
Basic ideaI Round-1: Find the smallest element ex in A: n− 1 comparisons
I Round-2: Find the smallest element withinL = y|y is compared greater than ex:|L| − 1 comparisons
I Finally, (n− 1) + (|L| − 1)
lower bound of |L|?
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 30 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 9.1-1
TC 9.1-1Show that the second smallest of n elements can be found withn + ⌈lg n⌉ − 2 comparisons in the worst case.(Hint: Also find the smallest element.)
Basic ideaI Round-1: Find the smallest element ex in A: n− 1 comparisonsI Round-2: Find the smallest element within
L = y|y is compared greater than ex:|L| − 1 comparisons
I Finally, (n− 1) + (|L| − 1)
lower bound of |L|?
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 30 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 9.1-1
TC 9.1-1Show that the second smallest of n elements can be found withn + ⌈lg n⌉ − 2 comparisons in the worst case.(Hint: Also find the smallest element.)
Basic ideaI Round-1: Find the smallest element ex in A: n− 1 comparisonsI Round-2: Find the smallest element within
L = y|y is compared greater than ex:|L| − 1 comparisonsI Finally, (n− 1) + (|L| − 1)
lower bound of |L|?
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 30 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 9.1-1
TC 9.1-1Show that the second smallest of n elements can be found withn + ⌈lg n⌉ − 2 comparisons in the worst case.(Hint: Also find the smallest element.)
Basic ideaI Round-1: Find the smallest element ex in A: n− 1 comparisonsI Round-2: Find the smallest element within
L = y|y is compared greater than ex:|L| − 1 comparisonsI Finally, (n− 1) + (|L| − 1)
lower bound of |L|?
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 30 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 9.1-1
lower bound of |L|?
Adversary Strategy:Each element ei ∈ A (i = 1, 2..n) is initially assigned a weight wi = 1ei ? ejI if wi ≥ wj, return ei ≥ ej and set wi = wi + wj, wj = 0I otherwise, return ei ≥ ej and set wj = wi + wj, wi = 0
We have:Finally wx = n, and the wx’s weight can be increased by up to 2 timesin each comparison.So, there must be at least ⌈lg n⌉ elements in L
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 31 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 9.1-1
lower bound of |L|?Adversary Strategy:
Each element ei ∈ A (i = 1, 2..n) is initially assigned a weight wi = 1ei ? ejI if wi ≥ wj, return ei ≥ ej and set wi = wi + wj, wj = 0I otherwise, return ei ≥ ej and set wj = wi + wj, wi = 0
We have:Finally wx = n, and the wx’s weight can be increased by up to 2 timesin each comparison.So, there must be at least ⌈lg n⌉ elements in L
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 31 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 9.1-1
lower bound of |L|?Adversary Strategy:
Each element ei ∈ A (i = 1, 2..n) is initially assigned a weight wi = 1ei ? ejI if wi ≥ wj, return ei ≥ ej and set wi = wi + wj, wj = 0I otherwise, return ei ≥ ej and set wj = wi + wj, wi = 0
We have:Finally wx = n, and the wx’s weight can be increased by up to 2 timesin each comparison.So, there must be at least ⌈lg n⌉ elements in L
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 31 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 9.3-7
TC 9.3-7Describe an O(n) -time algorithm that, given a set S of n distinct numbersand a positive integer k ≤ n, determines the k numbers in S that areclosest to the median of S.
How to measure closeness?position?value?
X
Ans.1 Select the median m of S.
O(n)
2 Create a new array B, B[i] = |S [i]−m|.
O(n)
3 Find the k-th smallest element x in B by calling Select.
O(n)
4 Pick up all elements in A[i] with B[i] ≤ x, form C.
O(n)
I If |C| > k, remove |C| − k elements, whose B[i] = x, from C.
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 32 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 9.3-7
TC 9.3-7Describe an O(n) -time algorithm that, given a set S of n distinct numbersand a positive integer k ≤ n, determines the k numbers in S that areclosest to the median of S.
How to measure closeness?
position?value?
X
Ans.1 Select the median m of S.
O(n)
2 Create a new array B, B[i] = |S [i]−m|.
O(n)
3 Find the k-th smallest element x in B by calling Select.
O(n)
4 Pick up all elements in A[i] with B[i] ≤ x, form C.
O(n)
I If |C| > k, remove |C| − k elements, whose B[i] = x, from C.
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 32 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 9.3-7
TC 9.3-7Describe an O(n) -time algorithm that, given a set S of n distinct numbersand a positive integer k ≤ n, determines the k numbers in S that areclosest to the median of S.
How to measure closeness?position?value?
XAns.
1 Select the median m of S.
O(n)
2 Create a new array B, B[i] = |S [i]−m|.
O(n)
3 Find the k-th smallest element x in B by calling Select.
O(n)
4 Pick up all elements in A[i] with B[i] ≤ x, form C.
O(n)
I If |C| > k, remove |C| − k elements, whose B[i] = x, from C.
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 32 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 9.3-7
TC 9.3-7Describe an O(n) -time algorithm that, given a set S of n distinct numbersand a positive integer k ≤ n, determines the k numbers in S that areclosest to the median of S.
How to measure closeness?position?value? X
Ans.1 Select the median m of S.
O(n)
2 Create a new array B, B[i] = |S [i]−m|.
O(n)
3 Find the k-th smallest element x in B by calling Select.
O(n)
4 Pick up all elements in A[i] with B[i] ≤ x, form C.
O(n)
I If |C| > k, remove |C| − k elements, whose B[i] = x, from C.
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 32 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 9.3-7
TC 9.3-7Describe an O(n) -time algorithm that, given a set S of n distinct numbersand a positive integer k ≤ n, determines the k numbers in S that areclosest to the median of S.
How to measure closeness?position?value? X
Ans.1 Select the median m of S. O(n)2 Create a new array B, B[i] = |S [i]−m|.
O(n)
3 Find the k-th smallest element x in B by calling Select.
O(n)
4 Pick up all elements in A[i] with B[i] ≤ x, form C.
O(n)
I If |C| > k, remove |C| − k elements, whose B[i] = x, from C.
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 32 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 9.3-7
TC 9.3-7Describe an O(n) -time algorithm that, given a set S of n distinct numbersand a positive integer k ≤ n, determines the k numbers in S that areclosest to the median of S.
How to measure closeness?position?value? X
Ans.1 Select the median m of S. O(n)2 Create a new array B, B[i] = |S [i]−m|. O(n)3 Find the k-th smallest element x in B by calling Select.
O(n)
4 Pick up all elements in A[i] with B[i] ≤ x, form C.
O(n)
I If |C| > k, remove |C| − k elements, whose B[i] = x, from C.
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 32 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 9.3-7
TC 9.3-7Describe an O(n) -time algorithm that, given a set S of n distinct numbersand a positive integer k ≤ n, determines the k numbers in S that areclosest to the median of S.
How to measure closeness?position?value? X
Ans.1 Select the median m of S. O(n)2 Create a new array B, B[i] = |S [i]−m|. O(n)3 Find the k-th smallest element x in B by calling Select.O(n)4 Pick up all elements in A[i] with B[i] ≤ x, form C.
O(n)
I If |C| > k, remove |C| − k elements, whose B[i] = x, from C.
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 32 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 9.3-7
TC 9.3-7Describe an O(n) -time algorithm that, given a set S of n distinct numbersand a positive integer k ≤ n, determines the k numbers in S that areclosest to the median of S.
How to measure closeness?position?value? X
Ans.1 Select the median m of S. O(n)2 Create a new array B, B[i] = |S [i]−m|. O(n)3 Find the k-th smallest element x in B by calling Select.O(n)4 Pick up all elements in A[i] with B[i] ≤ x, form C. O(n)
I If |C| > k, remove |C| − k elements, whose B[i] = x, from C.
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 32 / 33
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
TC 9.3-7
Thank You!Questions?
Office [email protected]
MA Jun (Institute of Computer Software) Problem Solving April 28, 2020 33 / 33